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

2 Answers 2

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

2
  • Rather than converting it back to List, I type casted it to Iterable<String> Commented Dec 7, 2019 at 18:42
  • @Monalisa Das oh nice job
    – KDH
    Commented Dec 7, 2019 at 21:05
0

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.

You must log in to answer this question.

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