1

I have a requirement where I need to merge value in a multiselect picklist with value in some other record.

So the final record updated should have sum total of all the values among the 2 records, i.e if in record 1 values are a;b;c and record 2 has b;c;d. The updated value in record 1 will be a;b;c;d.

I know that it is easily doable by iterating over the values in record 2 and adding in the value if that value doesn't exist in record 1, but wanted to have a solution where iteration can be avoided

1

without iteration it's a hard one because of the way multi-picklists are stored as a delimited string but maybe you could use a Set? Get all values from each picklist into lists and use the Set.addAll(List<String>); method to create the set of all unique values but converting them back to the picklist is the catch you could use String.join(list, ';'); but this would require you to convert the set back to a List (eg. New List<String>(SetOfUniqueValues)) and for all that effort maybe just iterating wouldn't be so bad

| improve this answer | |
  • Rather than converting it back to List, I type casted it to Iterable<String> – Monalisa Das Dec 7 '19 at 18:42
  • @Monalisa Das oh nice job – KDH Dec 7 '19 at 21:05
-1

I found solution for the requirement and here I don't need to iterate over multipicklist values. We can do this by proper usage of set and string methods. Please see sample code below.

String a = 'a;b;c';
String b = 'b;c;d';

Set<String> setPickVals = new Set<String>();
setPickVals.addAll(a.split(';'));
setPickVals.addAll(b.split(';'));

a = String.join((Iterable<String>)setPickVals, ';');
System.assertEquals(a,'a;b;c;d');

The solution becomes even more handy when we need to merge one string with other list of strings available, like merging multiselect picklist value in one record with multipicklist value in other records.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.