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
Mohd NabeelMohd Nabeel 

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);
    }
}

 
Kt YadavKt Yadav
Hi 
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.
Mohd NabeelMohd Nabeel
thanku, i know that it is not necessary to write DML in before event but the above code i dont think so is correct..
Mohd NabeelMohd Nabeel
The thing is the above trigger is working perfectly fine the only thing is i want all the functionality to be run on Before event.
Kt YadavKt Yadav
Yes so u can correct the code as per ur convenience and object, it will work fine for before event.
Mohd NabeelMohd Nabeel
How will you access the Invoiceids bind variable in the query, it will give you an error
Kt YadavKt Yadav
What error are you getting?Could you share the error, your object and its fields with the relationship,exact scenarios for this snippet.
Mohd NabeelMohd Nabeel

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.

Kt YadavKt Yadav
trigger InvoiceProductTrigger on Invoice_Product__c (before insert, before update, after insert, after update, after delete) {
    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;
                    }
                }
            }
        }
   }
Kt YadavKt Yadav
Hi ,
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;
}
Happy KaurHappy Kaur
I will be interested in more similar topics. i see you got really very useful topics.

Thanks
Happy
whatsapp status hindi (https://www.listbark.com/whatsapp-status-hindi/)