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
Shubham NandwanaShubham Nandwana 

Update multiselect picklist using trigger

Hi,
I have a custom object having multi-select picklist which contains the name of all the contacts. I want to write a trigger on contact which updates values in picklist whenever a new contact is added. 
Can anyone help me provide the code for that? Any help is appreciated.

Shubham Nandwana.
 
Niraj Kr SinghNiraj Kr Singh
Hi Shubham,

Yes, its possible but make sure you have unchecked the checkbox option "Restrict picklist to the values defined in the value set" of your multiselect picklist.
Try this code:
trigger ContactTrigger on Contact(before insert) {
	
	if(trigger.isBefore && trigger.isInsert) {
        for(Contact objCon : trigger.new) {
            if(objCon.Contact_Name__c != null)
				objCon.Contact_Name__c = objCon.Contact_Name__c + '; ' + objCon.Name;
			else
				objCon.Contact_Name__c = objCon.Name;
            system.debug('--after objCon.Contact_Name__c---' + objCon.Contact_Name__c);
        }
    }	
}
If it works for you, mark your Ans
Thanks
Niraj
 
Shubham NandwanaShubham Nandwana
Hi Niraj,
I have unchecked the given "Restrict picklist to the values defined in the value set" but still, I don't understand your answer, I need to insert the name of contact newly generated to my custom object's picklist field values.
Let me elaborate my problem. My custom object name is class__c and it has a multi-select teacher__c picklist field (i.e there can be multiple teachers teaching the same class or say different sections of the same class). All those teachers are added to my contacts.
I want that whenever a new contact is added, the picklist values are updated with the new person also.
Any help is appreciated.

Shubham Nandwana.
sowmya Inturi 9sowmya Inturi 9
Hi Shubham,
Please try this code. I tried in my org and it is working fine and let me know if you have any queries.
trigger UpdateClass on Contact (after insert,after update) {    
    
    List<class__c> clsList = new List<class__c>();
    Class__c cls= new Class__c();
    
    for(Contact Con : trigger.new) {
        if(Con.LastName != null && Con.Class__c!=null){
            cls=[select id,teacher__c from class__c where id=:con.class__c];
            if(cls.teacher__c!=null)
                cls.teacher__c = cls.teacher__c + ';' + Con.FirstName +' '+Con.LastName;
            else
                cls.teacher__c =Con.FirstName +' '+Con.LastName;
            clsList.add(cls);
        }
    }
    System.debug('clsList'+clsList);
    update clsList;
}    

Thanks,
Sowmya.
Shubham NandwanaShubham Nandwana
Hi Sowmya,
Thanks for your reply. I can add a new contact to my teacher field with your solution, there is something else I would like to implement - I also want the new contact to be added to multi-select picklist values so that whenever a new class is created all the new contacts are available for teacher selection.
Can you help me extending your code to update multi-select picklist values?
Thanks and Regards,
Shubham
sowmya Inturi 9sowmya Inturi 9
Hi Shubham,
I don't think you can do it with a standard way. As of my knowledge, you need to customize the whole page using Visualforce and Apex. 


Thanks,
Sowmya.