This is only the 2nd trigger I've attempted to write, and it's making me nuts. I have two custom objects: Sales Order Lines (child of Sales Order) and Sales Invoice Lines (child of Sales Invoice). Each Sales Order Line ultimately has one corresponding Sales Invoice Line created from it.

I want my trigger to write back two fields from the Sales Invoice Line to its related Sales Order Line, Invoice Number and Invoice Date.

I tried it two ways. An After Insert trigger on Sales Invoice Lines that writes back to Sales Order Lines, and an After Update trigger on Sales Order Lines that waits for a related Sales Invoice line to be created.

Attempt #1 - After Insert on Sales Invoice Line

trigger writeInvoiceANDDateToSoLines on c2g__codaInvoiceLineItem__c (after insert) {
    for (c2g__codaInvoiceLineItem__c newline : trigger.new){

        //Find the Sales Order Line that matches the current Sales Invoice Line.                                                  
        sales_order_lines__c thisline = [SELECT Id, sales_invoice__c, invoice_date__c FROM sales_order_lines__c WHERE Id = :newline.sales_order_line__c];

        //Set Invoice Number and Invoice Date for the matching Sales Order Line.
        For(sales_order_lines__c thisline: SOlinestoupdate) {
           thisline.Sales_Invoice__c = newline.c2g__Invoice__c;
           thisline.Invoice_Date__c = newline.createddate.date(); 

Attempt #2 - After Update on Sales Order Line

trigger invoiceDateNumberToSalesOrder on Sales_Order_Lines__c (after update) {
    for (Sales_Order_Lines__c thisline : trigger.new){
        //Check if Sales Order has been invoiced.
        if(Sales_Order_Lines__c.Sales_Invoice_Line_Item__c != NULL){

            //Copy date and invoice number from related invoice.               
            thisline.Sales_Invoice__c = thisline.Sales_Invoice_Line_Item__r.c2g__Invoice__c;
            thisline.Invoice_Date__c = thisline.Sales_Invoice_Line_Item__r.createddate.date();                                                    

Attempt #2 is the closest I've gotten to success. It's unfortunately throwing a read only error code when I update a Sales Order Line record.

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger invoiceDateNumberToSalesOrder caused an unexpected exception, contact your administrator: invoiceDateNumberToSalesOrder: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.invoiceDateNumberToSalesOrder: line 4, column 1

I'm at a loss now. Any tips on how to get around this read only issue?

  • 1
    You need to query for the parent and do the work on that. Attempt #2 is the correct trigger to do it on though.
    – dphil
    May 22, 2014 at 20:59
  • Thank you for the comment. I'll make some edits and report back. May 27, 2014 at 13:43

1 Answer 1


You are not able to update a record in an after trigger. If you are not dependent on validation rules or workflows, you can change the trigger to a before.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .