You need to sign in to do that
Don't have an account?
Pritam Patil 19
Changing OwnerId of a record in custom object
Hello,
I am trying to write an after insert trigger to change the OwnerId of a record in a custom object. But when I try and create a new record the following error occurs :
Apex trigger OwnerSwap caused an unexpected exception, contact your administrator: OwnerSwap: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.OwnerSwap: line 6, column 1
Following is the trigger I have written :
trigger OwnerSwap on Course__c (after insert) {
for(Course__c c : Trigger.New)
{
if(c.OwnerId != null)
{
c.OwnerId = c.Health_Coach__c;
}
}
}
Thanks.
I am trying to write an after insert trigger to change the OwnerId of a record in a custom object. But when I try and create a new record the following error occurs :
Apex trigger OwnerSwap caused an unexpected exception, contact your administrator: OwnerSwap: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.OwnerSwap: line 6, column 1
Following is the trigger I have written :
trigger OwnerSwap on Course__c (after insert) {
for(Course__c c : Trigger.New)
{
if(c.OwnerId != null)
{
c.OwnerId = c.Health_Coach__c;
}
}
}
Thanks.
We can not update any of the base record in the event "after trigger". that you can do only with the event "before trigger". After trigger event we use only when we require any audit fields (last modified roll, up summary etc...) and formula fields and use them for updating any other obejct reocords. In your code change the event to "before insert" it will work.
let me know, if it helps you :)
nagasfdc1@gmail.com
Thanks for your replies. I tried doing the trigger "Before insert" but still I cant see any positive result.
@sumit - I tried the trigger code you provided, but it is not working.
Actually the field 'Health_Coach__c' is a lookup to contact. So what should be done.?
Thanks.
Let us know if this will help you
Let me know, if it helps you :)
nagasfdc1@gmail.com