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
Richard Rodriguez 1Richard Rodriguez 1 

Trigger to create record/s based on selections made from a multi-select picklist

I was hoping somebody could help me with the code for a Trigger, or a Schedulable Class that can run after hours, and a Test Class (if applicable?!) for creating a record, or records, based on selections made from a multi-select picklist.

Okay, so this is what we're dealing with...

On a Contact record (eg. 003000000000001) there's a multi-select picklist field called Skill Codes (ts2__Skill_Codes__c) made up of various job skills (eg. 'Auto Electrician', 'Blast Crew', 'Blast Hole Driller', 'Bobcat Op', 'Boilermaker', etc.).

On a custom object called Short List (ts2__s_UserList__c) there are multiple records named for each of the Skill Codes (eg. a0D000000000001 = 'Auto Electrician', a0D000000000002 = 'Blast Crew', a0D000000000003 = 'Blast Hold Driller', a0D000000000004 = 'Bobcat Op', a0D000000000005 = 'Boilermaker', etc.).

Connecting the two objects is another custom object called Short List Link (ts2__s_UserListLink__c) which is made up of several records (eg. a0C000000000001, a0C000000000002, a0C000000000003, etc.) that only contain two fields, a Contact lookup field (ts2__r_contact__c) and a Short List lookup field (ts2__r_user_list__c).

And this is what needs to happen...
  1. User edits Contact record 003000000000001
    1. Updates Skill Codes to 'Blast Crew' and 'Boilermaker'.
    2. Saves the Contact record.
  2. [ Code works its magic ]
    1. Creates a Short List Link record a0C000000000001.
    2. Auto-assigns the Contact (ts2__r_contact__c) field to '003000000000001'.
    3. Auto-assigns the Short List (ts2__r_user_list__c) field to the matching record id for 'Blast Crew' > 'a0D000000000002'.
    4. Saves the Short List Link record.
    5. Creates a Short List Link record a0C000000000002.
    6. Auto-assigns the Contact (ts2__r_contact__c) field to '003000000000001'.
    7. Auto-assigns the Short List (ts2__r_user_list__c) field to the matching record id for 'Boilermaker' > 'a0D000000000005'.
    8. Saves the Short List Link record.
  3. COMPLETED.
We already have rules in place preventing the same Contact from being linked to the same Short List, so if a Short List Link already exists with that combination, I'd expect the trigger to still run, but fail for the creation of that Short List Link record... but still continue to create the remaining unique Short List Link records, if applicable.

I have also thought about the possibility of deleting the Short List Link when a User REMOVES the matching Skill Code from the Contact record, but that's getting a little too complicated for what I need right now.

I would normally use Process Builder for something like this, but there are 78 options in Skill Codes multi-select picklist, so the process would be way too big.

I know practically nothing about coding, so fully coded answers would be greatly appreciated.

Thanks.
SwethaSwetha (Salesforce Developers) 
HI Richard,
For code implementation, you can engage a salesforce Architect/Accelerator(https://help.salesforce.com/articleView?id=000337601&type=1&mode=1) You need to check with your salesforce account executive on this.

To get started with writing triggers, please see
https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro
https://help.salesforce.com/articleView?id=bi_edd_wb_trigger_native.htm&type=5

The below posts do not match your exact requirement but give an idea of similar implementations:
https://salesforce.stackexchange.com/questions/269718/trigger-to-create-multiple-records-based-on-multiselect-picklist-value
https://salesforce.stackexchange.com/questions/48173/can-a-trigger-be-created-to-take-the-chosen-multi-select-picklist-values-and-dis
https://jenwlee.com/2018/02/13/passing-multi-select-values-from-one-object-to-object/
https://douglascayers.com/2017/06/23/how-to-compare-multi-select-picklist-fields-in-flow-or-apex/

If this information helps, please mark this answer as best. Thank you