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:{
        //string conOccuption= con.Occupation__c;
        string conOccupationOld;
            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(';')){
            if(conOccupationOld!= NULL){
                set<string> oldOccupationList= new set<string>();
                for(String str: conOccupationOld.split(';')){
                newOccupationList.removeAll(oldOccupationList); //Filtering newly added occupation from pre existing Occupation
            //system.debug('newList '+newOccupationList);
            if(newOccupationList!= null){
                for(String str: newOccupationList){
                    Opportunity opp= new Opportunity();
                    opp.Name= con.lastName+' Opportunity For '+ str;
                    opp.StageName= 'Prospecting';
                    //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
                    oppMap.put(con.Id, oppToInsertList);
    if(oppMap.size()> 0){
        List<opportunityContactRole> oppconList= new list<opportunityContactRole>();
        for(Contact con:{
            insert oppMap.get(con.Id);
            for(Opportunity opp: oppMap.get(con.Id)){
                opportunityContactRole oppCon= new opportunityContactRole();
                oppCon.OpportunityId= opp.Id;
                oppCon.ContactId= con.Id;
        if(oppconList.size()> 0){
            insert oppconList;


If help, mark as solved