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
AneeshaAneesha 

rec.Owner.Name returning null in before update trigger

I am trying to update a text field with the owner's name everytime the owner is changed to  one among a particular group of users. The new record's owner id is correctly returned in the debug log however the rec.owner.Name is returning null. 

Can somebody help me understand what is the issue?
Best Answer chosen by Aneesha
Rajesh kanthaRajesh kantha
Hi Aneesha,

This is a known issue from salesforce side, to known more on this you can go ahead and check this link : http://www.michaelforce.org/recipeView?id=a0G30000006aQgCEAU .
But i can suggest you to do some custom logic for this on your trigger like this...
 
  Map<String,String> idOwnerNameMap = new Map<String,String>();
  //Query all active user and add id and Name to this map
  //Query all queue and add id and Name to this map
  for(Record rec : Trigger.new){
   String OwnerName ;
   // check that owner is a user (not a queue)
   if((String)(rec.OwnerId).substring(0,3) == '005' ){
      OwnerName =  idOwnerNameMap.get(rec.OwnerId);
   }
   // check that owner is a queue
   else if((String)(rec.OwnerId).substring(0,3) == '00G' ){
    OwnerName =  idOwnerNameMap.get(rec.OwnerId);
   
   }
 
  }

Regards,
Rajesh

All Answers

Rajesh kanthaRajesh kantha
Hi Aneesha,

This is a known issue from salesforce side, to known more on this you can go ahead and check this link : http://www.michaelforce.org/recipeView?id=a0G30000006aQgCEAU .
But i can suggest you to do some custom logic for this on your trigger like this...
 
  Map<String,String> idOwnerNameMap = new Map<String,String>();
  //Query all active user and add id and Name to this map
  //Query all queue and add id and Name to this map
  for(Record rec : Trigger.new){
   String OwnerName ;
   // check that owner is a user (not a queue)
   if((String)(rec.OwnerId).substring(0,3) == '005' ){
      OwnerName =  idOwnerNameMap.get(rec.OwnerId);
   }
   // check that owner is a queue
   else if((String)(rec.OwnerId).substring(0,3) == '00G' ){
    OwnerName =  idOwnerNameMap.get(rec.OwnerId);
   
   }
 
  }

Regards,
Rajesh
This was selected as the best answer
AneeshaAneesha
Thanks Rajesh. 

SF should fix this issue. It feels wierd to waste a query when u can simply use the object relationship.