function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shekhar 46shekhar 46 

wriet a trigger for multiselect picklist

  1. On both account & contact we have role field which is multiselect picklist, having value as  a, b, c, d .  When ever any contact is created/updated  with role value ,  we need to maintain unique role on related parent account of contact. Means  have created account, at that time the role field of account is blank,after that we created 3 contact with role value like ab, bc, cd. Unique value in these three is a.b.c value and these value will be updated on parent.and d will be removed.  write a trigger for thsi condition
AnkaiahAnkaiah (Salesforce Developers) 
Hi Shekhar,

Try with below code.
trigger AccountRoleUpdate on contact(after insert,after update) {
    Map<Id, Set<String>> mp = new Map<Id, Set<String>>();
    for (Contact o : {
        if (o.AccountId != null && String.isNotBlank(o.Role__c)) {
            //This will take care of multiple opportunities being associated with one account
            if (!mp.containsKey(o.AccountId)) {
                mp.put(o.AccountId, new Set<String>());
            // Add all Roles here from all child opportunities

    List<Account> acctLst = [SELECT Id, Role__c FROM Account WHERE ID IN :mp.KeySet()];

    for (Account a : acctLst) {
        if (mp.ContainsKey(a.Id)) {
            Set<String> Roles = mp.get(a.Id);
            if (String.isNotBlank(a.Role__c)) {
                // Add existing account Roles so that they are not lost.
            //Now update the field.
            List<String> RolesList = new List<String>();
            a.Role__c = String.join(RolesList, ';');
    update acctLst;

If this helps, Please mark it as best answer.