You need to sign in to do that
Don't have an account?
HARI KRISHNA
Help me for Bulkifying this Trigger
------------------Help me for Bulkifying this Trigger-----------------------------Invoice have look up relationship with Account...
trigger InvAmountUpdate on Invoice__c (After Update)
{
for(Invoice__c inv:Trigger.New)
{
Account acc=new Account();
acc=[Select id,Account_Balance__c From Account Where id =:inv.Account__c];
if(acc.Account_Balance__c==null)
acc.Account_Balance__c=0;
Invoice__c invOld=Trigger.oldMap.get(inv.id);
if(inv.Invoice_Amount__c!=invOld.Invoice_Amount__c)
acc.Account_Balance__c=acc.Account_Balance__c-invOld.Invoice_Amount__c+inv.Invoice_Amount__c;
update acc;
}
}
trigger InvAmountUpdate on Invoice__c (After Update)
{
for(Invoice__c inv:Trigger.New)
{
Account acc=new Account();
acc=[Select id,Account_Balance__c From Account Where id =:inv.Account__c];
if(acc.Account_Balance__c==null)
acc.Account_Balance__c=0;
Invoice__c invOld=Trigger.oldMap.get(inv.id);
if(inv.Invoice_Amount__c!=invOld.Invoice_Amount__c)
acc.Account_Balance__c=acc.Account_Balance__c-invOld.Invoice_Amount__c+inv.Invoice_Amount__c;
update acc;
}
}
In general, here's how you would handle bulk invoices. But please note the following: You'll need to handle null values with Account_Balance and Invoice_Amount__c values. Also, what if the Invoices pertain to the same account? The account balance might be wrong in that case.