You need to sign in to do that
Don't have an account?
Shubham 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.
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.
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: If it works for you, mark your Ans
Thanks
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.
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.
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
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.