You need to sign in to do that
Don't have an account?
Rupali 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:
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!
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
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:
to
Looking forward to your response.
Thanks.
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.
I would second Anutej solution. Reading the logic, it should work nicely.
regards
Andrew
Thank you for helping out.
Regards
Rupali