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
Greer Zimmerman 5Greer Zimmerman 5 

Trigger to create multiple child "sessions" on multiple days of the week from a parent "workshop"

Hi all - first of all - this is my first trigger. I have workshops and individual sessions for that workshop in Salesforce. When a workshop is created I want the sessions to be created based on the information entered on the workshop record. I understand how to do this if the workshop meets just one day a week (see code below - I think it looks okay?) but sometimes these workshops meet two or three times a week and the particular days are stored in a multi picklist. The user can select which days they meet, the start date, and the end date. 

I cannot for the life of me figure out how to create ongoing records for sessions occuring on, say, Mondays AND Thursdays AND Wednesdays or any other combo of days. Can anyone help?
Best Answer chosen by Greer Zimmerman 5
Maharajan CMaharajan C
Hi Greer,

You have to use like below:

Multi-select picklists are stored as value1;value2;value3;etc. Knowing that, you can get a List of these values using the String split() method. You can also view this string splitting example. To access it, you would then tweak your code as:

trigger AutoCreateInterviewer on Position__c (after insert)
{
List<Interviewer__c> interviewers = new List<Interviewer__c>();
//For each position processed by the trigger, add a new
//interviewer record for the specified hiring manager.
//Note that Trigger.New is a list of all the new positions //that are being created.
for (Position__c newPosition: Trigger.New)
{ if (newPosition.Hiring_Manager__c != null)
{
// split out the multi-select picklist using the semicolon delimiter
for(String hiringManager: newPosition.Hiring_Manager__c.split(';'))
{
interviewers.add(new Interviewer__c( Name = '1', Position__c = newPosition.Id, Employee__c = (Id)hiringManager, Role__c = 'Managerial'));
} } }
insert interviewers;
}

Reference Link: https://salesforce.stackexchange.com/questions/9706/how-can-i-upsert-multiple-new-records-based-on-a-multi-value-field

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Raj

All Answers

Maharajan CMaharajan C
Hi Greer,

You have to use like below:

Multi-select picklists are stored as value1;value2;value3;etc. Knowing that, you can get a List of these values using the String split() method. You can also view this string splitting example. To access it, you would then tweak your code as:

trigger AutoCreateInterviewer on Position__c (after insert)
{
List<Interviewer__c> interviewers = new List<Interviewer__c>();
//For each position processed by the trigger, add a new
//interviewer record for the specified hiring manager.
//Note that Trigger.New is a list of all the new positions //that are being created.
for (Position__c newPosition: Trigger.New)
{ if (newPosition.Hiring_Manager__c != null)
{
// split out the multi-select picklist using the semicolon delimiter
for(String hiringManager: newPosition.Hiring_Manager__c.split(';'))
{
interviewers.add(new Interviewer__c( Name = '1', Position__c = newPosition.Id, Employee__c = (Id)hiringManager, Role__c = 'Managerial'));
} } }
insert interviewers;
}

Reference Link: https://salesforce.stackexchange.com/questions/9706/how-can-i-upsert-multiple-new-records-based-on-a-multi-value-field

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Raj
This was selected as the best answer
Greer Zimmerman 5Greer Zimmerman 5
Thank you this helped a lot!