[SalesForce] Remove Comma from String

I am doing bulk upserts from a flat file and concatenating two fields using a Comma.
However, if the either one or both of the fields in the flat file are blank or null i don't want the comma to show up on Salesforce side.

Is this achievable after insert?

Best Answer

Write a before insert trigger. for example you object is account

trigger accountTrigger on Account(before insert)
{
    for(Account acc : trigger.New)
    {
        if(acc.custom_Field__c != null)
        {
            List<String> tempstr = String.valueOf(acc.custom_Field__c).split(',');
            String temp = '';
            for(string str : tempstr)
            {
                if(Str.trim().length() > 0)
                    temp = temp+str+',';
            }
            acc.custom_Field__c = temp.removeEnd(','); // if you want to remove from end
        }
    }
}

This is a sample code for your reference.

Related Topic