• Rupali Jain 11
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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!
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!