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
sachin joshi 14sachin joshi 14 

Field is not Writable error in Trigger

Hello Everyone,
I am facing an error on below trigger
trigger totaloflineitmoninvc on Line_Item__c(after insert, after update) {
			Invoice__c [] inv = new List<Invoice__c>();
    Set<Id> lineitmid = new Set<Id>();
    for(Line_Item__c li : Trigger.New)
    {
        lineitmid.add(li.Invoice__c);
    }
	Decimal total;
    Invoice__c[] Invo = [Select Id, Invoice_Total__c, (Select Id, Line_Item_Total__c from line_Items__r)
                        	From Invoice__c Where Id IN : lineitmid];
    for(Invoice__c invc : Invo ){
         total = 0;
                for(Line_Item__c litm : invc.Line_Items__r ){
         			total += litm.Line_Item_Total__c;
                }
    invc.Invoice_Total__c = total;
    inv.add(invc);   
    
    }
    update inv;
}

I am noy able to understand why I am getiing this error, all i want to do is just assign the total of my Line Items in Invoice Total.
Please give suggesions. 
sharathchandra thukkanisharathchandra thukkani
your Invoice_Total__c field on Invoice__c must be formula field that is the reason you might getting the Field is not Writable error.

If there is master detail relation ship between Invoice__c and line_Item__c better you go for rollup summary field.
Ajay K DubediAjay K Dubedi
hii sachin joshi 14,

What is the relationship between line_Items__c and Invoice__c? If it's master-detail then, by default, you can't 'reparent' the record, that is, write to the Invoice__c field after the detail record has been created. You can, however, selecting 'Allow Reparenting' in the master-detail relationship definition to make it work.

Thanks.