Search code examples
insertsalesforceapexcheckboxlistpicklist

Salesforce apex how to save checkbox multiple values in database


I am trying to save the values from checkbox group in apex. I have converted the values from comma-delimited to a list of strings. How can I save the values into the picklist type column? I have checkbox group of colors and there is a picklist field in the custom object which saves color options you choose at the front end.

In Lightning page

get acc_status_options() {
        return [
            { label: 'Black', value: 'Black' },
            { label: 'White', value: 'White' },
            { label: 'Red', value: 'Red' },
            { label: 'Yellow', value: 'Yellow' },
            { label: 'Green', value: 'Green' },
        ];
    }

In apex, I am receiving the parameter as comma delimited string and converting it to the list of String.

List<String> selectedColors = colors.split(',');

How to save this multiple selected list values into the database column?

MyObj obj = new MyObj();
obj.yourcolors__c = selectedColors;

insert obj;

I am getting error and can not save the record.


Solution

  • Multipicklists store the values separated by semicolons, not commas. So either a replaceall or string.join(colors, ';'); should do the trick.