You need to sign in to do that
Don't have an account?
Rohith 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
Here is the whole code
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(); } }
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
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.
If you find you answer than please mark as best answer
Thanks And regards,
Suraj Tripathi.