You need to sign in to do that
Don't have an account?

how would i achieve this task in before trigger event
//Handler Class public class InvoiceClass { public static void invoiceProduct(List<Invoice_Product__c> invProduct){ for(Invoice_Product__c inv : invProduct){ inv.Total_Price__c = inv.Price__c * inv.Quantity__c; } } public static void invoiceAmountUpdate(List<Invoice_Product__c> invProduct ){ Set<id> invoiceIds = new Set<id>(); for(Invoice_Product__c invPro : invProduct){ if(invPro.InvoiceLookUp__c != Null){ invoiceIds.add(invPro.InvoiceLookUp__c); } } List<Invoice__c> invoices = [Select id, Total_Amount__c, (Select id, Total_Price__c From Invoice_Products__r) From Invoice__c where id in : invoiceIds]; for(Invoice__c inv: invoices){ inv.Total_Amount__c = 0; for(Invoice_Product__c invProd : inv.Invoice_Products__r){ inv.Total_Amount__c += invProd.Total_Price__c; } } if(invoices != Null){ update invoices; } } } //trigger trigger InvoiceProductTrigger on Invoice_Product__c (before insert, before update, after insert, after update, after delete) { if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){ InvoiceClass.invoiceProduct(Trigger.new); } if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){ InvoiceClass.invoiceAmountUpdate(Trigger.new); } if(Trigger.isAfter && Trigger.isDelete){ InvoiceClass.invoiceAmountUpdate(Trigger.old); } if(Trigger.isAfter && Trigger.isUpdate){ InvoiceClass.invoiceAmountUpdate(Trigger.old); } }
trigger InvoiceProductTrigger on Invoice_Product__c (before insert, before update, after insert, after update, after delete) {
if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
for(Invoice_Product__c inv : trigger.new){
Set<id> invoiceIds = new Set<id>();
inv.Total_Price__c = inv.Price__c * inv.Quantity__c;
if(invPro.InvoiceLookUp__c != Null){
invoiceIds.add(invPro.InvoiceLookUp__c);
}
}
for(Invoice__c inv: [Select id, Total_Amount__c, (Select id, Total_Price__c
From Invoice_Products__r)
From Invoice__c where id in : invoiceIds]){
inv.Total_Amount__c = 0;
for(Invoice_Product__c invProd : inv.Invoice_Products__r){
inv.Total_Amount__c += invProd.Total_Price__c;
}
}
}
You dont have to write DML stmt inside before context.
first the InvoiceIds bind variable error which says InvoiceIds variable does not exist and second it is not updating any amount in the Invoice__c object.
The Invoice_Product__c Price is updating but it should update the Invoice__c Total_Amount__c field.
if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
Set<id> invoiceIds = new Set<id>();
for(Invoice_Product__c inv : trigger.new){
inv.Total_Price__c = inv.Price__c * inv.Quantity__c;
if(invPro.InvoiceLookUp__c != Null){
invoiceIds.add(invPro.InvoiceLookUp__c);
}
}
if(invoiceIds!= null && !invoiceIds.isEmpty()){
for(Invoice__c inv: [SELECT id, Total_Amount__c, (Select id, Total_Price__c FROM Invoice_Products__r)
FROM Invoice__c WHERE id in : invoiceIds]){
inv.Total_Amount__c = 0;
if(inv.Invoice_Products__r!=null){
for(Invoice_Product__c invProd : inv.Invoice_Products__r){
inv.Total_Amount__c =inv.Total_Amount__c+invProd.Total_Price__c;
}
}
}
}
}
You can not update parent record untill your child gets inserted or updated. So once you are done with the before update of child , you can update the parent value in after context.You can use process builder for the same.
Find below snippet i run for Account & Contact.
trigger TotalPrice on Contact (After insert,after update) {
Set<Id> accountSet = new Set<Id>();
for(Contact cont: trigger.new){
if(cont.AccountId!=null){
accountSet.add(cont.accountId);
}
}
List<account> actList = new List<Account>();
if(accountSet!=null && !accountSet.isEmpty()){
for(Account act : [Select id,Total_Amount__c,(Select id,Total_Price__c from contacts) from account where id in : accountSet]){
act.Total_Amount__c = 0;
if(act.contacts!=null){
for(Contact ct : act.contacts){
if(ct.Total_Price__c!=null){
act.Total_Amount__c = act.Total_Amount__c+ ct.Total_Price__c;
}
}
actList.add(act);
}
}
}
update actList;
}
Thanks
Happy
whatsapp status hindi (https://www.listbark.com/whatsapp-status-hindi/)