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

Need help with trigger to update owner

I had the following trigger in place to update the Housing Request (custom object) owner ID to the current user's ID when the request status was updated to "In Process". But now I am realizing I need to trigger the owner update sooner. So I need to change to trigger when the status is "New" and since the user I want to change ownership to will not be in the record at the time I can no longer user the current user's ID to assign ownership to. Instead, I need to assign ownership to a related user (Housing_Request__c.Housing_Rep__r.Id) but every time I attempt to change ownership to this related user I get a method error. Can you help me modify this to work?



trigger ownerUpdate on Housing_Request__c (before update) {
    // no bulk processing; will only run from the UI   
    if ( == 1) {   
        // if a Housing Request has been updated to In Process
        if ([0].Status__c == 'In Process') {   
            // Change Trigger.old[0].OwnerId to Current User ID
  [0].OwnerId = UserInfo.getUserId();


Figured it out... if anyone else is in this situation, this is what I used




trigger ownerUpdate on Housing_Request__c (before insert, before update) {
   for (Housing_Request__c hreq :
     //check if status of request is New
     if (hreq.Status__c == 'New'){   
            //change owner to housing rep
            hreq.OwnerId = hreq.Housing_Rep__c;