0

I want to write an apex trigger action to remove a particular value from multi select picklist field based on some condition.How can I do this?

This is my trigger action:

public static void updateStopCom(map<Id, Account> accountOldMap, map<Id, Account> accountNewMap) {
   for (Id accountId : accountNewMap.keySet()) {
        Account newAccountValue = accountNewMap.get(accountId);
        Account prevAccountValue = accountOldMap.get(accountId);
        if ( (newAccountValue.Online__pc != prevAccountValue.Online__pc)){

             if(newAccountValue.Online__pc == true){
                newAccountValue.StopCom__c = ???; // how?
             }

        }
   }
}

StopCom__c is my multi select picklist field.

Could anyone please help me to resolve this?

3
  • Is that, newAccountValue.StopCom__c = ''; not working? Commented May 31, 2019 at 6:40
  • No, I want to remove particular value
    – JURY
    Commented May 31, 2019 at 10:39
  • Quick tip for other people looking at this question: if(value == true) can always be simplified to if(value). Likewise, if(value == false) simplifies to if(!value). Just a way to save a bit of typing.
    – Derek F
    Commented Sep 4, 2023 at 13:49

3 Answers 3

2

As multiselect picklist fields are stored as semi colon separated values, you will have to follow below steps to add/remove values to/from it.

  • Split the values by semi colon and convert the string into List
  • Add/Remove the element to/from the list
  • Convert the list back to semi colon separated values using String.Join method

This is how your code should look like

string strPickListValue = newAccountValue.StopCom__c;
List<string> lstPicklistValues = strPickListValue.split(';');
string strValueToRemove ='abc';
if(lstPicklistValues.contains(strValueToRemove)){
    lstPicklistValues.remove(lstPicklistValues.indexOf(strValueToRemove));
}
newAccountValue.StopCom__c = String.join(lstPicklistValues,';');
2
  • Thank you Vijay !!
    – JURY
    Commented May 31, 2019 at 10:40
  • Glad to help you !! Commented May 31, 2019 at 12:03
1

You have to split your multi-picklist value into List of string and iterate over it and do your execution(adding a value or removing it).

To split a multi-picklist value, In your case use like this:-

List<String> listofmultipicklisValues= String.valueOf(newAccountValue.StopCom__c).split(';');

To iterate over the string, A sample one would be like this:-

String Interests = '';
Boolean Start = true;
for(String Str : listofmultipicklisValues) {
    if(Start) {
       Interests = Str;
       Start = false;
    }else {               
       Interests = Interests + ';' + Str;
     }
 }

To remove a value directly in the List, Use like this:-

for(String Str : listofmultipicklisValues) {
   if(some condition){
      listofmultipicklisValues.remove(listofmultipicklisValues.get(str));
   }
}

At the end, add the list of string into single string again and update your muti-picklist field with that string.

1
  • Thank you sanket !!
    – JURY
    Commented May 31, 2019 at 10:41
1

Vijay Ganji's answer is pretty good, though I would prefer to use a Set<String> here instead of a List<String>.

It won't make a difference in performance (or the list approach might be ever so slightly faster) given the number of values in a multipicklist field are usually very small, but it is slightly less typing.

This takes advantage of the fact that we have a constructor for Set that takes a list and that we have a constructor for List that takes a set.

// Split the multipicklist value on semicolons
String strPickListValue = newAccountValue.StopCom__c;
Set<String> picklistValuesSet = new Set<String>(strPickListValue.split(';'));

// Remove the target value
string strValueToRemove = 'abc';
// If the multipicklist doesn't have our target value, the set will simply just
//   not be modified.
// Thus, we can remove the if() from Vijay's solution
// This is what saves us some typing (even though we have a little additional
//   typing elsewhere)
picklistValuesSet.remove(strValueToRemove);

// Join everything back together again
newAccountValue.StopCom__c = String.join(new List<String>(lstPicklistValues),';');

This is 291 characters vs Vijay's 340, though perhaps the more important thing is that we eliminated 2-3 lines of code (depending on how you count).

As of Summer '23 (API v58.0), the Set collection type implements the iterable interface, so we can just feed the set directly to String.join() instead of converting it back to a List.

newAccountValue.StopCom__c = String.join(lstPicklistValues,';');

This makes the final character count 273

You must log in to answer this question.

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