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
Linda 98Linda 98 

Iterate over list and create records based on another list

I am having a list of Occupations, Driver, Mechanic, Plumber, Carpenter, Chef.
I have contact records with above occupations. One contact can have multiple occupations too.
So I have a trigger which creates Opportunities with these contacts when another field is updated.
Currently i am creating only one opportunity for each contact, now I am trying to create one opportunity for each contact based on occupation. For instance,

contact 1 - name:contact1,occupation:chef,carpenter
contact 2-name:contact2,occupation:Chef,driver
contact 3 -name:contact3,occupation:driver,mechanic,Plumber

My occupation list contains :  Chef,Driver,mechanic

So it should create 5 opportunities( now it creates only 3 opportunities using 3 contacts) like
1.Name-opp1,contact(lookup)-contact1,Contact occ-chef
2.Name-Opp2,contact(lookup)-contact2,contact occu-Chef
3.Name-Opp3,contact(lookup)-contact2,contact occu-driver
4.Name-Opp4,contact(lookup)-contact3,contact occu-Driver
5.Name-Opp5,contact(lookup)-contact3,contact occu-mechanic
I have to iterate over the list of occupations,and create opportunity records.How can i get this change.Thanks all.
Piyush Gautam 6Piyush Gautam 6
Hi Linda,

Please try the below code:
 
trigger createOppFromContact on Contact (after insert, after Update) {
    
    List<Opportunity> oppToInsertList= new list<Opportunity>();
    Map<string, List<Opportunity>> oppMap= new Map<string, List<Opportunity>>();
    
    for(Contact con: trigger.new){
        
        //string conOccuption= con.Occupation__c;
        string conOccupationOld;
        if(trigger.isUpdate){
            conOccupationOld= trigger.oldMap.get(con.Id).Occupation__c;
        }
        
        if(con.Occupation__c!= null){
            
            set<string> newOccupationList= new set<string>();
            
            for(String str: con.Occupation__c.split(';')){
                newOccupationList.add(str);
            }
            
            if(conOccupationOld!= NULL){
                set<string> oldOccupationList= new set<string>();
                
                for(String str: conOccupationOld.split(';')){
                    oldOccupationList.add(str);
                }
                newOccupationList.removeAll(oldOccupationList); //Filtering newly added occupation from pre existing Occupation
            }
            
            
            //system.debug('newList '+newOccupationList);
            //system.debug('con.Occupation__c'+con.Occupation__c);
            
            if(newOccupationList!= null){
                
                for(String str: newOccupationList){
                    
                    Opportunity opp= new Opportunity();
                    opp.Name= con.lastName+' Opportunity For '+ str;
                    opp.StageName= 'Prospecting';
                    opp.CloseDate= system.today()+10;
                    //Add Data to all Fields, If a custom lookup field of contact is in use that populate it here with con.Id 
                    //Else create a contactRole record and relate it with opportunity 
                    //Before adding to list or creating Opportunity object check whether record already exist as per the scenario
                    system.debug('oppToInsertList'+oppToInsertList);
                    oppToInsertList.add(opp);
                    oppMap.put(con.Id, oppToInsertList);
                    system.debug(oppMap);
                }
            }
            
        }
    }
    system.debug('oppMap'+oppMap);
    system.debug('Size'+oppMap.size());
    if(oppMap.size()> 0){
        
        system.debug(oppMap);
        List<opportunityContactRole> oppconList= new list<opportunityContactRole>();
        for(Contact con: trigger.new){
            insert oppMap.get(con.Id);
            
            for(Opportunity opp: oppMap.get(con.Id)){
                
                opportunityContactRole oppCon= new opportunityContactRole();
                oppCon.OpportunityId= opp.Id;
                oppCon.ContactId= con.Id;
                oppconList.add(oppCon);
            }
        }
        
        if(oppconList.size()> 0){
            insert oppconList;
        }
    }

}

If help, mark as solved
Thanks