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
Rupali Jain 11Rupali Jain 11 

Need help in writing trigger without nested loop.

Hi everyone!

I have a use case for apex trigger but i could not write it without a nested for loop. Just need help to find a solution without nested for loop.

Use Case: when a contact gets created i need to take field named- 'Description' of contact and update it on a field- Description present on the opportunity records (where stage='closed won')  associated with the contact's Account Id. so the problem is:

1.create contact. Take the account id and description field values.

2. search for all the opportunities associated with that account id with stage='closed won' and update the description on opportunity with description on contact.



here goes my code:
trigger contactTrigger on Contact (before insert) {

    if (trigger.isInsert && trigger.isbefore){

        

        List<Opportunity> optoUpdate=new List<Opportunity>();

        Set<Id> accId =new Set<Id>();

        for(Contact c:trigger.new){

            accId.add(c.AccountId);

        }

        List <Opportunity> oppToUpdate=new List <Opportunity>();

        oppToUpdate=[select AccountId,Description from Opportunity where StageName='Closed Won' and AccountId in :accId];

        Map<Id,List <Opportunity>> testmap=new Map<Id,List<Opportunity>>();

        for(Opportunity op:oppToUpdate){

            if (testmap.containsKey(op.AccountId)){

                testMap.get(op.AccountId).add(op);

            }

            else{

                testmap.put(op.AccountId,new list<Opportunity>{op});

            }

        }

        

        for(Contact c:trigger.new){

            if (testmap.containsKey(c.AccountId)){

                for(opportunity op:testmap.get(c.AccountId)){

                    if(op.description ==''){

                    op.description=c.description;

                    }

                    else{

                       op.description=op.description+c.description; 

                    }

                        

                    optoUpdate.add(op);

                    

                }

            }

        }

        

       update optoUpdate;        

    }

}
thanks in advance!
Best Answer chosen by Rupali Jain 11
ANUTEJANUTEJ (Salesforce Developers) 
I tried the below code with a single for loop it worked for me can you try checking:
 
trigger c_trigger on Contact (before insert) {
    if (trigger.isInsert && trigger.isbefore){
        List<Opportunity> optoUpdate=new List<Opportunity>();
        Map<id,String> contactDescMap = new Map<id,String>();
        Set<Id> accId =new Set<Id>();
        
        for(Contact c:trigger.new){
            accId.add(c.AccountId);
            contactDescMap.put(c.AccountId,c.Description);
            
        }
        
        List <Opportunity> OpList=new List <Opportunity>();
        OpList=[select AccountId,Description from Opportunity where StageName='Closed Won' and AccountId in :accId];
        
        for(Opportunity op: OpList)
        {
            String testDesc= contactDescMap.get(op.AccountId);
            
            Opportunity opTest = new Opportunity();
            opTest.id = op.id;
            if(op.Description ==''|| op.Description==null){
                op.Description=testDesc;
                
            }
            else{
                op.Description=op.Description+testDesc; 
            }
            optoUpdate.add(op);
            
        }
        update optoUpdate;        
    }
}

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Rupali,

I was able to use the same trigger code in my org and I was able to achieve the implementation you were looking for and it worked absolutely fine was there any issue or error you were facing and also can you check if the trigger is active?

if there is no description in the opportunity record I was getting null so I checked for it and replaces the below statement:
if(op.description ==''){
                        op.description=''+c.description;
                    }

to
 
if(op.description ==''|| op.Description==null){
                        op.description=''+c.description;
                    }

Looking forward to your response.

Thanks.
ANUTEJANUTEJ (Salesforce Developers) 
I tried the below code with a single for loop it worked for me can you try checking:
 
trigger c_trigger on Contact (before insert) {
    if (trigger.isInsert && trigger.isbefore){
        List<Opportunity> optoUpdate=new List<Opportunity>();
        Map<id,String> contactDescMap = new Map<id,String>();
        Set<Id> accId =new Set<Id>();
        
        for(Contact c:trigger.new){
            accId.add(c.AccountId);
            contactDescMap.put(c.AccountId,c.Description);
            
        }
        
        List <Opportunity> OpList=new List <Opportunity>();
        OpList=[select AccountId,Description from Opportunity where StageName='Closed Won' and AccountId in :accId];
        
        for(Opportunity op: OpList)
        {
            String testDesc= contactDescMap.get(op.AccountId);
            
            Opportunity opTest = new Opportunity();
            opTest.id = op.id;
            if(op.Description ==''|| op.Description==null){
                op.Description=testDesc;
                
            }
            else{
                op.Description=op.Description+testDesc; 
            }
            optoUpdate.add(op);
            
        }
        update optoUpdate;        
    }
}

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Andrew GAndrew G

I would second Anutej solution.  Reading the logic, it should work nicely.

regards
Andrew
Rupali Jain 11Rupali Jain 11
Hi @Anutej the solution u suggested works fine!.
Thank you for helping out.

Regards
Rupali