0

I have a case where I need to update the options in a multi-select custom field from an Apex Trigger. Is this possible?

My full use case is on a Create/Update of a Custom Object, if a pre-defined condition is met, update a particular custom field (on another Custom Object) with an additional multi-select option.

-Clarification-

I am looking to add options to a select list custom field, not just select values that already exist in the select list.

1
  • Post your code whatever you have tried Commented Jan 24, 2017 at 0:02

1 Answer 1

0

Yes it is possible. The options are semicolon (;) delimited. You will first have to make sure the field doesn't already contain the option you are trying to add. Also make sure at least one option is selected before performing any such checks.

if (String.isNotBlank(triggerRecord.Multi_Select__c))
{
    Set<String> selectedOptions = new Set<String>(triggerRecord.Multi_Select__c.split(';'));
    if (!selectedOptions.contains(optionToAdd))
        triggerRecord.Multi_Select__c += ';' + optionToAdd;
}

Honestly though, unless you have a Validation Rule acting on this field, you should just use Process Builder.

2
  • Adrian -- doesn't split() return a list? and you are then assigning to a set. I don't think this will compile
    – cropredy
    Commented Jan 24, 2017 at 1:14
  • Thanks all, I might need to clarify my question a little more. The trigger that I am looking to add should add options to the custom field, meaning making it a selectable option in the custom field for everyone. The code above I think assumes that the option exists for the custom field and simply needs to be set. I am trying to add the option to the custom field first. Commented Jan 27, 2017 at 17:53

You must log in to answer this question.

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