Update existing multipicklist data from 'UK;London;US' to 'London;New York'





// Assuming the object is called `YourObject__c` and the MultiPicklist field is `MultiPicklistField__c`
public class MultiPicklistUpdate {
    public static void updateMultiPicklist(Id recordId) {
        // Query the existing record and retrieve the MultiPicklist field value
        YourObject__c obj = [SELECT MultiPicklistField__c FROM YourObject__c WHERE Id = :recordId LIMIT 1];

        // Get the MultiPicklist values as a List
        List picklistValues = new List();
        if (obj.MultiPicklistField__c != null) {
            picklistValues = new List(obj.MultiPicklistField__c.split(';'));
        }

        // Remove 'UK' and 'US' and replace 'US' with 'New York'
        if (picklistValues.contains('UK')) {
            picklistValues.remove('UK');
        }
        if (picklistValues.contains('US')) {
            picklistValues.remove('US');
            picklistValues.add('New York');
        }

        // Convert the updated list back into a semicolon-separated string
        obj.MultiPicklistField__c = String.join(picklistValues, ';');

        // Update the record
        update obj;
    }
}



Update existing multipicklist data from 'UK;London;US' to 'London;New York' Update existing multipicklist data from 'UK;London;US' to 'London;New York' Reviewed by dasfrogpractice on 03:15 Rating: 5

No comments:

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe