1

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?

4
  • You could create a workflow rule that looks for empty space before or after a comma and then removes the comma if it finds it.
    – crmprogdev
    Jan 11, 2016 at 21:02
  • Are you asking how to update the record after the upsert is done or are you asking how to do this in a trigger?
    – Jagular
    Jan 11, 2016 at 21:02
  • Just the part of the code in apex that removes comma
    – SfdcDev14
    Jan 11, 2016 at 21:13
  • why would you not do this in excel before submitting the flat file?
    – cropredy
    Jan 12, 2016 at 0:48

3 Answers 3

1

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.

1
// My custom numeric setting
TS_Settings__c tsSettings = TS_Settings__c.getValues('Test');
if (tsSettings != null)
{
    // You can't just do String.valueOf(tsSettings.InvoiceNumber__c) because it will then include the decimal, even with 0 specified as the number of decimal places.
    // You must first convert it to a whole number
    integer curInvoiceNum = (Integer)tsSettings.InvoiceNumber__c;
    // When converting from integer to string, it strips off the comma
    string curInvNum = String.valueOf(curInvoiceNum);

    System.debug('Number with Comma: ' + curInvoiceNum.format());

    System.debug('Number witout Comma: ' + curInvNum);
}
0

Incorporate something like this in some trigger code:

String farmyward_noises = 'meow, , , woof, oink, , neigh, , ';
System.debug('noise_list: ' + farmyward_noises);

List<String> noise_list = farmyward_noises.split(',');

for (Integer i=noise_list.size()-1; i>=0; i--){
    noise_list.set(i, noise_list.get(i).trim());
    if (String.isBlank(noise_list.get(i))){
        noise_list.remove(i);
    }
}

farmyward_noises = String.join(noise_list, ', ');
System.debug('noise_list: ' + farmyward_noises);

This seems like it would be best done before insert.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .