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
wadams2010wadams2010 

Unable to get owner name as string when owner is queue or user

Hey SFDC Community!

I am having an issue with a trigger where I am unable to get the Owner.Name as a string when the owner value is changed. When I use OwnerId the field that I am trying to populate will work but when I use Owner.Name it gets populated as blank. I know this should work but for some reason I am running into issues over it. See the trigger below and let me know what you think. I have highlighted and underlined the line where I have the Owner.Name. Thanks in advance!
 
trigger UpdateOFR on Feature_Request__c (after update, after insert) { 

    if (trigger.isUpdate){
        
        Set<Id> setFRchangeIds = new Set<Id>();
        
        for (Feature_Request__c fr : trigger.new){
            Feature_Request__c frOldFR = trigger.oldMap.get(fr.Id);
            
            boolean frIsChanged = (fr.Feature_Request_Status__c != frOldFR.Feature_Request_Status__c || fr.Target_Release__c != frOldFR.Target_Release__c || fr.OwnerId != frOldFR.OwnerId);
            if (frIsChanged){
                setFRchangeIds.add(fr.Id);
            }
        }
        
        if (setFRchangeIds.isEmpty() == false){
            List<Opportunity_Feature_Request__c> listOFR = [Select Id, Feature_Request__c From Opportunity_Feature_Request__c where Feature_Request__c IN:setFRchangeIds];
            for (Opportunity_Feature_Request__c ofr : listOFR){
                
                Feature_Request__c fr = trigger.newMap.get(ofr.Feature_Request__c);
                
                ofr.Feature_Request_Owner__c = fr.Owner.Name;
                ofr.Feature_Request_Status__c = fr.Feature_Request_Status__c;
                ofr.Feature_Request_Target_Release__c = fr.Target_Release__c;
            }
            if (listOFR.isEmpty() == false){
                update listOFR;
            }
        }
    }
}

 
@Karanraj@Karanraj
In the trigger.New you can't get the parent field name. If you want the parent level information you have to write SOQL query to get the name of the field. 


 
trigger UpdateOFR on Feature_Request__c (after update, after insert) { 

    if (trigger.isUpdate){
        Map<Id,Feature_Request__c > mapFeatureRequest = new Map<Id,Feature_Request__c>([Select Id,Name,Owner.Name,OwnerId,Feature_Request_Status__c,Target_Release__c from Feature_Request__c where ID IN: Trigger.New]);
        Set<Id> setFRchangeIds = new Set<Id>();
        
        for (Feature_Request__c fr : trigger.new){
            Feature_Request__c frOldFR = trigger.oldMap.get(fr.Id);
            
            boolean frIsChanged = (fr.Feature_Request_Status__c != frOldFR.Feature_Request_Status__c || fr.Target_Release__c != frOldFR.Target_Release__c || fr.OwnerId != frOldFR.OwnerId);
            if (frIsChanged){
                setFRchangeIds.add(fr.Id);
            }
        }
        
        if (setFRchangeIds.isEmpty() == false){
            List<Opportunity_Feature_Request__c> listOFR = [Select Id, Feature_Request__c From Opportunity_Feature_Request__c where Feature_Request__c IN:setFRchangeIds];
            for (Opportunity_Feature_Request__c ofr : listOFR){
                
                Feature_Request__c fr = mapFeatureRequest.get(ofr.Feature_Request__c);
                ofr.Feature_Request_Owner__c = fr.Owner.Name;
                ofr.Feature_Request_Status__c = fr.Feature_Request_Status__c;
                ofr.Feature_Request_Target_Release__c = fr.Target_Release__c;
            }
            if (listOFR.isEmpty() == false){
                update listOFR;
            }
        }
    }
}