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
Vinu VargheseVinu Varghese 

Need help in After delete trigger

I have the following error after deleting a record.  Insert and Update are OK. but deleting the record giving the following error. 
Unfortunately, there was a problem. Please try again. If the problem continues, get in touch with your administrator with the error ID shown here and any other related details. The requested resource does not exist

trigger InvoiceTotal on Invoice_Item__c (after insert, after update, after delete) {

    set<ID> setId = new set<ID>();
    List<invoice__c> lstInvoice = new List<invoice__c>();
           
    If (!trigger.isdelete) {
    for(Invoice_Item__c fndId :trigger.new){
        setId.add(fndId.invoice__c);
   
    decimal Totamount = 0;
    for(invoice__c parentInvoice :[select id, name, invoice_no__c, (select id, invoice__c, Total_Price__c, name  from invoice_Items__r) from Invoice__c where id in :setId]){
        Invoice__c Inv = new Invoice__c();
        Inv.id = parentInvoice.id;
        For(Invoice_Item__c item :parentInvoice.invoice_items__r){
            Inv.Total_amount__c =item.Total_Price__c;
            Totamount = Totamount + Inv.Total_amount__c;
            Inv.Total_amount__c = Totamount;
        }
            system.debug('Total  Amount of this Invoice = '+Totamount);
           lstInvoice.add(Inv);
    }
    }
    }
    Else if(trigger.isdelete){
        for(Invoice_Item__c fndId :trigger.old){
                setId.add(fndId.invoice__c);
            system.debug('Total price deleted 1 :'+fndId.invoice__c+'---'+fndId.Total_Price__c);
        decimal Totamount = 0;
            Invoice__c lstInvoice = [Select Total_amount__c from Invoice__c where id in :setId];
            lstInvoice.Total_Amount__c = lstInvoice.Total_Amount__c - fndId.Total_Price__c;
            system.debug('Previous Total Amount should be updated :'+lstInvoice.Total_Amount__c);
           
            system.debug('Total price deleted  2 :'+fndId.invoice__c+'---'+fndId.Total_Price__c);
            update lstInvoice;
        }
       
    }
    If (!trigger.isdelete){

            update lstInvoice;
    }
}



 
Vinu VargheseVinu Varghese
for(Invoice_Item__c fndId :trigger.old)

I used the above code. trigger.old for delete
Andrew GAndrew G
Your code has many faults including SOQL and DMLs inside for loops.

To help resolve your code issue, review the code in this response and modify as required.  Whilst the objects are different, the structure of your code should follow the same outline and practices.

https://developer.salesforce.com/forums/?id=9060G0000005QBBQA2

Regards
Andrew