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
PremanathPremanath 

How to Avoid DML and SOQL operations in for loop

Hi All,

 

   I have problm with Governor limits.

   In for loop we cannot use DML and SOQL quiries ..  But i have used in my issue.

  That issue is Account related opportunity records amount should be add and give the total amount ..

  

Please give me code instead of this ... 

 

 

 

trigger AmountTask on Opportunity (after insert)
{
for(Opportunity varopp:Trigger.new){
Decimal Amount=0;
List<Opportunity> oppbj=[select id,name,Amount from Opportunity where AccountId=:varopp.AccountId];
for(integer i=0;i<oppbj.size();i++){
Amount=Amount+oppbj[i].Amount;
}
//system.debug('----->'+oppbj.size());
Account varacc=[select id,name,Total_Amount__c from Account where id=:varopp.AccountID];
varacc.Total_Amount__c=Amount;
update varacc;
}
}

 

 

 

 

 

Thank you guys....................

Best Answer chosen by Admin (Salesforce Developers) 
rohitsfdcrohitsfdc

Try this code

trigger AmountTask on Opportunity (after insert)
{
Map<id,decimal> acctotal = new Map<id,decimal>();

for(opportunity opp :trigger.new)
{
if(opp.amount!=null)
{    
    if(acctotal.get(opp.accountid)==null){
    acctotal.put(opp.accountid,opp.amount);
    }
    else
    {
    decimal amt = 0;
    amt = acctotal.get(opp.accountid)+opp.amount;
     acctotal.put(opp.accountid,amt);
     }
}
}
list<account> alist= new list<account>();
list<account> accList = [select total_amount__c from account where id in:acctotal.keyset()];
for(account a:acclist){
a.total_amount__c+=acctotal.get(a.id);
alist.add(a);
}
update alist;
}

 

All Answers

rohitsfdcrohitsfdc

Hi,

as my understanding, your code is summing up amount of all opportunities of an account. If that is the case, you dont need to write a trigger. You can use ROLL UP summary field for that.

 

PremanathPremanath

Thanks for you reply

 

I know that but i want know how can i manage Governor limits in this trigger..

 

 please solve my problm.......

 

Can anybody help me plz...

rohitsfdcrohitsfdc

Try this code

trigger AmountTask on Opportunity (after insert)
{
Map<id,decimal> acctotal = new Map<id,decimal>();

for(opportunity opp :trigger.new)
{
if(opp.amount!=null)
{    
    if(acctotal.get(opp.accountid)==null){
    acctotal.put(opp.accountid,opp.amount);
    }
    else
    {
    decimal amt = 0;
    amt = acctotal.get(opp.accountid)+opp.amount;
     acctotal.put(opp.accountid,amt);
     }
}
}
list<account> alist= new list<account>();
list<account> accList = [select total_amount__c from account where id in:acctotal.keyset()];
for(account a:acclist){
a.total_amount__c+=acctotal.get(a.id);
alist.add(a);
}
update alist;
}

 

This was selected as the best answer
PremanathPremanath

Thanks a lot........

PremanathPremanath

Hi Rohit,

 

This operation is not working for update. It adding wrong way .. i.e 

 

 if amount : 5000   update amount = 7000

Total amount : 5000+7000  it takes ...

plz help me...  cheak this code.....

 

 

trigger AmountTask on Opportunity (after insert,after update){
if(Trigger.isinsert){
Map<id,decimal> acctotal = new Map<id,decimal>();
for(opportunity opp :trigger.new){
if(opp.amount!=null){
if(acctotal.get(opp.accountid)==null){
acctotal.put(opp.accountid,opp.amount);
}
else
{
decimal amt = 0;
amt = acctotal.get(opp.accountid)+opp.amount;
acctotal.put(opp.accountid,amt);
}
}
}
list<account> alist= new list<account>();
list<account> accList = [select total_amount__c from account where id in:acctotal.keyset()];
for(account a:acclist){
a.total_amount__c+=acctotal.get(a.id);
alist.add(a);
}
update alist;
}


if(Trigger.isUpdate){
Map<id,decimal> acctotal = new Map<id,decimal>();
for(opportunity opp :trigger.new){
if(opp.amount!=null){
if(acctotal.get(opp.accountid)==null){
acctotal.put(opp.accountid,opp.amount);
}
else
{
decimal amt = 0;
amt = acctotal.get(opp.accountid)+opp.amount;
acctotal.put(opp.accountid,amt);
}
}
}
list<account> alist= new list<account>();
list<account> accList = [select total_amount__c from account where id in:acctotal.keyset()];
for(account a:acclist){
a.total_amount__c+=acctotal.get(a.id);
alist.add(a);
}
update alist;
}

}