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.

  • Post your code whatever you have tried – Santanu Boral Jan 24 '17 at 0:02
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.

| improve this answer | |
  • Adrian -- doesn't split() return a list? and you are then assigning to a set. I don't think this will compile – cropredy Jan 24 '17 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. – Jesse Miller Jan 27 '17 at 17:53

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.