You need to sign in to do that
Don't have an account?
Sparky Jane
How to write query outside the for loop in triggers?
trigger fieldupdate on Contact (before insert, before update) {
for(Contact c : Trigger.new){
Account val = [SELECT Acc_Cost__c from Account where Id=:c.AccountId];
val.Acc_Cost__c +=c.Cost__c;
upsert val;
}
for(Contact c : Trigger.new){
Account val = [SELECT Acc_Cost__c from Account where Id=:c.AccountId];
val.Acc_Cost__c +=c.Cost__c;
upsert val;
}
Below is the modified code .
and there is governor limit on Dml as well . so never fire a dml inside for loop.
let us know if you have any question .
please don't Forget to Mark this as your best answer if it works fine for you
Regards,
Grazitti Team
Web: www.grazitti.com
All Answers
https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=ALLQUESTIONS&id=906F0000000AfETIA0
Regards,
Sravs
Below is the modified code .
and there is governor limit on Dml as well . so never fire a dml inside for loop.
let us know if you have any question .
please don't Forget to Mark this as your best answer if it works fine for you
Regards,
Grazitti Team
Web: www.grazitti.com
Give this a try (please use after insert, after update on your trigger)
Also, this is a good reference on how to bulkify your code, avoid DML inside your loop and other Apex Best Practices.
https://developer.salesforce.com/page/Apex_Code_Best_Practices