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
Rohith Kumar 98Rohith Kumar 98 

How to assign lookup in child record?

I created a custom object named Payment Order, and Account is parent to it with lookup relationship. When creating a new record in Payment Order and Account is not selected as lookup, I need to populate some fields from the custom object Payment Order(child) to Account and create that new Account. I did create an Account with fields but when I try to add the newly created Account as look up to the corresponding child I get an error that the record is read-only.

paymentOrder is a reference to each record from Trigger.new
account is a reference to newly created account
 
paymentOrder.Account__c = account.Id;

Here is the whole code
public static void insertAccountContactInformation(List<Payment_Order__c> paymentOrderList){
        List<Account> accountList = new List<Account>();
        for(Payment_Order__c paymentOrder : paymentOrderList){
            if(paymentOrder.Account__c == null){
                //Creating new record in Account with field values from Payment Order
                Account account = new Account(Name = paymentOrder.Payee_Name__c, Phone = paymentOrder.Phone_No__c,
                                             BillingStreet = paymentOrder.Address_Street_1__c,
                                             ShippingStreet = paymentOrder.Address_Street_2__c,
                                             ShippingPostalCode = paymentOrder.Zip__c,
                                             ShippingState = paymentOrder.State__c,
                                             ShippingCountry = paymentOrder.Country__c);
                paymentOrder.Account__c = account.Id;
                accountList.add(account);
            }
        }
        //Inserting created account record
        try{
            insert accountList;
        }catch(Exception error){
            error.getMessage();
        }
    }

 
Best Answer chosen by Rohith Kumar 98
ANUTEJANUTEJ (Salesforce Developers) 
Hi Rohit,

I believe the error is "execution of AfterUpdate caused by: System.FinalException: Record is read-only"

>> https://salesforce.stackexchange.com/questions/23922/system-finalexception-record-is-read-only-trigger-updatecompetitors-line-24

As mentioned in the above link, "This is because you are in an after insert/update trigger and the records are read only in that context as they have been written, but not committed, to the database.

Unfortunately your trigger is relying on the ids of the records, which means you won't be able to use before insert as the ids won't be populated at that time (as the records haven't been written to the database at that point so while you can write to them, database generated fields aren't populated).

In this instance you'll need to clone the record (or query anew via SOQL) and make changes to the new copy of the record, then execute the update against the new copies."

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Rohit,

I believe the error is "execution of AfterUpdate caused by: System.FinalException: Record is read-only"

>> https://salesforce.stackexchange.com/questions/23922/system-finalexception-record-is-read-only-trigger-updatecompetitors-line-24

As mentioned in the above link, "This is because you are in an after insert/update trigger and the records are read only in that context as they have been written, but not committed, to the database.

Unfortunately your trigger is relying on the ids of the records, which means you won't be able to use before insert as the ids won't be populated at that time (as the records haven't been written to the database at that point so while you can write to them, database generated fields aren't populated).

In this instance you'll need to clone the record (or query anew via SOQL) and make changes to the new copy of the record, then execute the update against the new copies."

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Rohith Kumar 98Rohith Kumar 98
I updated the code and the account lookup seems to be null, IDK where I am wrong :(
 
public static void insertAccountContactInformation(List<Payment_Order__c> paymentOrderList){
        List<Payment_Order__c> paymentOrderUpdateList = new List<Payment_Order__c>();
        List<Account> accountList = new List<Account>();
        for(Payment_Order__c paymentOrder : paymentOrderList){
            if(paymentOrder.Account__c == null){
                //Creating new record in Account with field values from Payment Order
                Account account = new Account(Name = paymentOrder.Payee_Name__c, Phone = paymentOrder.Phone_No__c,
                                             BillingStreet = paymentOrder.Address_Street_1__c,
                                             ShippingStreet = paymentOrder.Address_Street_2__c,
                                             ShippingPostalCode = paymentOrder.Zip__c,
                                             ShippingState = paymentOrder.State__c,
                                             ShippingCountry = paymentOrder.Country__c);
                
                //To update Lookup field Creating copy of Payment Order as it is read-only
                Payment_Order__c paymentOrderUpdate = paymentOrder.clone(true, true, true, true);
                paymentOrderUpdate.Account__c = account.id;
                
                System.debug(paymentOrderUpdate);
                
                paymentOrderUpdateList.add(paymentOrderUpdate);
                accountList.add(account);
            }
        }
        //Inserting created account record
        try{
            insert accountList;
        }catch(Exception error){
            error.getMessage();
        }
        
        //Updating Payment Order
        try{
            update paymentOrderUpdateList;
        } catch(Exception error){
            error.getMessage();
        }
    }

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Rohith Kumar,
public static void insertAccountContactInformation(List<Payment_Order__c> paymentOrderList){
        List<Account> accountList = new List<Account>();
        for(Payment_Order__c paymentOrder : paymentOrderList){
            if(paymentOrder.Account__c == null){
                //Creating new record in Account with field values from Payment Order
                Account account = new Account(Name = paymentOrder.Payee_Name__c, Phone = paymentOrder.Phone_No__c,
                                             BillingStreet = paymentOrder.Address_Street_1__c,
                                             ShippingStreet = paymentOrder.Address_Street_2__c,
                                             ShippingPostalCode = paymentOrder.Zip__c,
                                             ShippingState = paymentOrder.State__c,
                                             ShippingCountry = paymentOrder.Country__c);
                paymentOrder.Account__c = account.Id;
                accountList.add(account);
            }
        }
        //Inserting created account record
        try{
            insert accountList;
        }catch(Exception error){
            error.getMessage();
        }

    For(Account acc:accountList){
        for(Payment_Order__c paymentOrder : paymentOrderList){
            if(PaymentOrder.Payee_Name__c==acc.Name){
                paymentOrder.AccountID=acc.Id;
            }
        }
    }
    try{
    update paymentOrderList;
    }catch(Exception e){
    e.getMessage();
    }    
    }

If you find you answer than please mark as best answer
Thanks And regards,
Suraj Tripathi.
Rohith Kumar 98Rohith Kumar 98
Sorry, I was trying to get the account id before it's being inserted, problem is solved now. Thank you :)
Rohith Kumar 98Rohith Kumar 98
Thanks All :)