Once a custom record is created by a Community user on the Napili Community, Account lookup field of that record should be populated from UserID(CreatedBy) >> ContactId >> AccountID. I am trying to update a Lookup field of a record after its creation using a trigger.
trigger setAccountLookup on Kindle__c (after insert) {
Kindle__c[] resList = new Kindle__c[] {};
for (Kindle__c res : Trigger.new){
res.Account__c = res.CreatedBy.Contact.AccountId;
resList.add(res);
}
update resList;
}
ERROR: setAccountLookup: execution of AfterInsert caused by: System.FinalException: Record is read-only Trigger.setAccountLookup: line 4, column 1
Am I missing something or doing it in the wrong way?