Here is what I'm planning to do:

I'll be firing trigger which has logic for both 'before' and 'after' update.
'Before Update' operation could possibly 'change' a string in some field say field1

This string can't be changed manually and before update could possibly change it from predefined values

Predefined values could be say 'A', 'B' , 'C' , 'D'

So this string could be any combination of above values, including just a single value, but always semi-colon separated if have multiple values , so as an example, possible values could be :

C;D and so on

In after trigger, I've to compare old and new values of these strings and extract two lists and create records of some other object;

One list should have those values which are newly added. for e.g if string changes from A to A;B;C , then this list should have values B & C

For 2nd list If field1 changes from A;B;C to C;D, then it should have values A & B

This is 'Roughly' I've done so far. created a function in which new and old value of field will be passed and it will return either 'newlyadded' or 'removed' values in list

private List<string>  funcAddedorRemoved(string newValue , string oldValue, string typeOfChange){
      // declaring 3 lists, first and second will split new and old values passed  and store in list and  third one will store either 'newly added' or 'removed values' depending upon 3rd parameter passed in this function
      List<string> newfieldValueList = new list<string>(); 
      List<string> existingValueList= new list<string>();
      List<string> AddedRemovedList = new list<string>();
        // first split the passed, old and new values
        if(newValue!= null && newValue.contains(';'))
             newfieldValueList = newValue.split(';');

        if(oldValue!=null && oldValue.contains(';'))
             existingValueList = oldValue.split(';');

      // find if values are added/removed based on parameter(typeofChange) value
      if(typeOfChange == 'findAdded' && newfieldValue.size()>0 ){
         For(string s: newfieldValueList ){
            if(oldValue!=null && !oldValue.contains(s))
      }else if(typeOfChange == 'findRemoved' && existingValueList.size()>0 ){
        For(string s: existingValueList){
            if(newValue!= null && !newValue.contains(s))

      return AddedRemovedList;

is there any efficient way I could do . I'm thinking I've made it unnecessarily 'complicated'. Any lead will be helpful

1 Answer 1


You can use Set methods here. As an example:

String newValue = 'A;D;B';
String oldValue = 'C;A';

Set<String> newValues = new Set<String>(newValue.split(';'));
Set<String> oldValues = new Set<String>(oldValue.split(';'));

Set<String> added = newValues.clone();
Set<String> removed = oldValues.clone();

system.debug(added); // yields {B,D}
system.debug(removed); // yields {C}
  • Just coded a similar concept yesterday )): Set<Id> duplicateIds = new Set<Id>(existingRecords.keySet()); duplicateIds.retainAll( priorIdeasMapBefore.keySet() ); for( Id key : duplicateIds ) existingRecords.remove( key ); priorIdeasMapBefore.putAll( (List<Idea__c>)existingRecords.values() );
    – o-lexi
    Mar 30, 2017 at 22:30

You must log in to answer this question.

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