You need to sign in to do that
Don't have an account?
vikas rathi91
how to remove query in for loop And want to avoid for loop with in for loop
Hello Community,
I am stuck to find the solution on my Trigger . I want to Optimize my trigger code because some one use Query in for loop and nested for loop. I am abel to run code for single record but not in bulk.
Here My code please help on this, Thanks In advance.
Thanks
Accky
I am stuck to find the solution on my Trigger . I want to Optimize my trigger code because some one use Query in for loop and nested for loop. I am abel to run code for single record but not in bulk.
Here My code please help on this, Thanks In advance.
try{ for(opportunity opportunityobj:trigger.new) { opportunity opptyofoldmap=trigger.oldmap.get(opportunityobj.id); if(opptyofoldmap.closeDate!=null&opptyofoldmap.closeDate!=opportunityobj.closeDate) { Integer dueDate=opptyofoldmap.closeDate.daysBetween(opportunityobj.closeDate); if(dueDate!=0 && dueDate > 0 ) { list<OpportunityLineItem> newopp=[select id,Revenue_Start_Date__c,Opportunityid, UnitPrice, Revenue_Exchange_Rate__c, TCV__c, contract_term__c, CYR__c, NYR__c from OpportunityLineItem where Opportunityid=:opportunityobj.id AND Revenue_Start_Date__c < :opportunityobj.closeDate]; for(OpportunityLineItem OpportunityProductobject:newopp) { integer i=0; OpportunityProductobject.Revenue_Start_Date__c=OpportunityProductobject.Revenue_Start_Date__c+dueDate; OpportunityProductupdate.add(OpportunityProductobject); } } } } }
Thanks
Accky
Move the Soql query outside of the for loop. Since you have to add some logic to 'Revenue_Start_Date__c' of oppLine item, try something like below, regards
Sampath
All Answers
Please check once below snippet.
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Move the Soql query outside of the for loop. Since you have to add some logic to 'Revenue_Start_Date__c' of oppLine item, try something like below, regards
Sampath