[SalesForce] trigger to create multiple records based on multiselect picklist value

I am working on one trigger which is based on Lead object, on lead I have two fields

1) What_is_your_primary_program_of_interest__c – Picklist field

2) Other_Programs__c – Multiselect picklist field.

When these two fields are not null then I need to create records in hed__Affiliation__c object.
for picklist fields I am able to achieve creating record in Affiliation object but failed to achieve for multi select picklist field.
If 5 values are selected in multi select picklist field then I need to create 5 records in hed__Affiliation__c object.
Once user select values then I need to match that value names with account names then populate Account__c field on hed__Affiliation__c object with Account Id

Can anyone help me out in this issue please

My code is below:

trigger ContactCreationFromLead on Lead (After insert, after update) {
//trigger to create contact, relationship, affiliation records from lead.
List<Contact> conInsertList = new List<Contact>();
List<hed__Affiliation__c> affiliation = new List <hed__Affiliation__c>();
List<hed__Relationship__c> relation = new List<hed__Relationship__c>();
List<String> listEmail = new List<String>();
List<String> listname = new List<String>();
if(trigger.isInsert || trigger.isUpdate){
for (Lead em : Trigger.new) {
    if(em.Email != null){
        listEmail.add(em.Email);
    }
    if(em.What_is_your_primary_program_of_interest__c != null ){
        listname.add(em.What_is_your_primary_program_of_interest__c);
    }
}
}
//to catch the account Id that matches the lead primary program of interest
//account name should match with primary program of interest picklist values
List<Account> account = [SELECT Id,Name from Account WHERE Name = :listname];
String record;
for(Account acc : account){
    record = acc.Id; 
}

List<Contact> cem = [SELECT Id, Email FROM Contact WHERE Email = :listEmail];
String cemail;
for(Contact ce : cem){
    cemail = ce.Email;
}
//code to create contact for student if form is filled by student
for(Lead ld : Trigger.new) {
    if (ld.Email != cemail && ld.Parent_or_guardian__c == false) {

        Contact cnt = new Contact();

        cnt.FirstName = ld.FirstName;
        cnt.LastName = ld.LastName;
        cnt.Email = ld.Email;
        cnt.RecordTypeId = '012P00000005ZDpIAM';
        conInsertList.add(cnt);
    }
    //code to create contacts for parent and student if form is filled by parent
    else{
        if(ld.Email != cemail && ld.Parent_or_guardian__c == true ){
            Contact cnt1 = new Contact();
            cnt1.RecordTypeId = '012P00000005ZDuIAM';
            cnt1.FirstName = ld.Parent_First_Name__c;
            cnt1.LastName = ld.Parent_Last_Name__c;
            cnt1.Email = ld.Parent_Email__c;
            conInsertList.add(cnt1);
            Contact cnt2 = new Contact();
            cnt2.RecordTypeId = '012P00000005ZDpIAM';
            cnt2.FirstName = ld.FirstName;
            cnt2.LastName = ld.LastName;
            cnt2.Email = ld.Email;
            conInsertList.add(cnt2);
        }
    }
}

if(conInsertList.size()>0){
    INSERT conInsertList;
    List<Id> conInsert = new List<Id>();
    //catch the inserted contact ids to create relationship record
    if(conInsertList.size()>1){
    for(Integer i = 0, s = conInsertList.size(); i < s; i += 2){
        //creates relation record by catching current student and parent record id's
        hed__Relationship__c hedrel = new hed__Relationship__c();
        hedrel.hed__Contact__c = conInsertList[i + 1].Id;
        hedrel.hed__RelatedContact__c = conInsertList[i].Id;
        hedrel.hed__Type__c = 'Parent';
        relation.add(hedrel);
        //creates affiliation record if filled by parent
        hed__Affiliation__c hedaff = new hed__Affiliation__c();
        hedaff.hed__Account__c = record;
        hedaff.hed__Contact__c = conInsertList[i + 1].Id;
        hedaff.hed__Primary__c = true;
        affiliation.add(hedaff);

    }
    }
    else{
        //creates Affiliation record if filled by student only
        if(conInsertList.size()==1){
        for(Integer i = 0, s = conInsertList.size(); i < s; i += 2){
        hed__Affiliation__c hedaff = new hed__Affiliation__c();
        hedaff.hed__Account__c = record ;
        hedaff.hed__Contact__c = conInsertList[i].Id;
        hedaff.hed__Primary__c = true;
        affiliation.add(hedaff);
    }
    }
    }
    insert relation;
    insert affiliation;
}
}

Best Answer

In any one of the loop that you are iterating on Lead, you can add this logic :

  1. Get all values in a set from the multiselect field. This will make the values unique.
  2. Query account object with these names and fetch ids.
  3. Iterate over account list obtained form above query and create new Affiliation records.
Set<String> otherProgramsValueSet = new Set<String>();
for (Lead em : Trigger.new) {
    if(em.Email != null){
        listEmail.add(em.Email);
    }
    if(em.What_is_your_primary_program_of_interest__c != null ){
        listname.add(em.What_is_your_primary_program_of_interest__c);
    }
    if(em.Other_Programs__c != null){
    // this will construct a set of values selected in multiselect. Set will maintain unique values.
    // Multiselect values are semi-colon separated, hence the split function.
        otherProgramsValueSet.addAll(em.Other_Programs__c .split(';')); 
    }
}

//Query account records with matching names.
//Iterate over this list to create new Affiliation records
List<hed__Affiliation__c> affiliationListToBeInserted = new List<hed__Affiliation__c>();
for(Account acc : [select id from account where name in : otherProgramsValueSet]){
    affiliationListToBeInserted.add(new hed__Affiliation__c(Account__c= acc.Id));
    //Add more required fields in above statement, if any.
}
insert affiliationListToBeInserted;
Related Topic