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
Pritam Patil 19Pritam 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.
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi Amit,

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
sumit singh 37sumit singh 37
trigger OwnerSwap on Course__c (after insert) 
{  
    
map<id,Course__c> course= new Map<id, Course__c> [select OwnerId  from Course__c where id IN : Trigger.new]


    for(Course__c c : Trigger.New)
	{
	   if(c.OwnerId == null)
	   {
		  course.get(c.id).OwnerId = c.Health_Coach__c;
	   }
	}

   update course.values();
}

 
sumit singh 37sumit singh 37
use the above code or change the trigger to before update
Pritam Patil 19Pritam Patil 19
Hi all,
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.
Amit Chaudhary 8Amit Chaudhary 8
You can'not set the contact as record owner. Please create one user lookup to set record owner
trigger OwnerSwap on Course__c (before insert) 
{
    for(Course__c c : Trigger.New)
	{
	   if(c.OwnerId == null)
	   {
		   c.OwnerId = c.Health_Coach__c; 
// Health_Coach__c should be user lookup or create new user lookup
	   }
	}
}

Let us know if this will help you
 
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi Pritam,
  • Make sure you are assigning the user id to owner (in your case this maynot be the reason for current issue)
  • first try checking updating the record owner with the same credentials and data from Developer console and see if the same issue is coming. if that is coming there even then it must be an issue access on the record (check crud access on object || fiel level access || owd (sharing rule))
  • if that is working fine in developer console then must be an issue with the trigger event (record got locked in the process). Not sure this is your issue as you mentioned the same is not working as well for before insert.
  • make sure you are not performing dml on same record... if you post your complete code related to this can give more idea.

Let me know, if it helps you :)
nagasfdc1@gmail.com
hims mosehims mose
You can try to adjust some speed optimize script in your program to increase the loading speed. I did the same job for my page of Life coaching (https://dennisberry.com/) and its working fine now.