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 :
A;B
B;C
A
B
A;B;C
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(';');
else
newfieldValueList.add(newValue);
if(oldValue!=null && oldValue.contains(';'))
existingValueList = oldValue.split(';');
else
existingValueList.add(oldValue);
// 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))
AddedRemovedList(s);
}
}else if(typeOfChange == 'findRemoved' && existingValueList.size()>0 ){
For(string s: existingValueList){
if(newValue!= null && !newValue.contains(s))
AddedRemovedList(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