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
brozinickrbrozinickr 

Simple Trigger to Write Role Name of the Owner on a record

I'm having issue testing this trigger that I wrote, I thought this would be something pretty simple but it's not working as I thought it would when I manually test it.

 

Basically, when someone edits their Goal record, I want the Owner's Role Name to populate in a field called Role__c.  When I test it, nothing is writing to the record.  I'm pretty sure it's because I haven't queried the User object, but  I am trying to make this efficient and not query from the User.  I know the User object works a little differently and I know some of the fields are more readily available, so I figured I might be able to pull this value in without querying from it.  Is that incorrect logic?

 

 

trigger Goal_ChangeRecordOwner on Al_Goal__c (before insert, before update) {

    for(Al_Goal__c goal: Trigger.new)
    {
         goal.OwnerId=goal.Goal_Owner__c;
         goal.Role__c=string.valueOf(goal.Owner.UserRole.Name);   
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

When you enter a trigger context, lookup values are not immediately available in the trigger objects stored in memory, so you will have to query the values and remap it back accordingly.

 

Something like this should work:

 

trigger Goal_ChangeRecordOwner on Al_Goal__c (before insert, before update) {
    Map<Id, User> ownerMap = new Map<Id, User>{};
    for(Al_Goal__c goal: Trigger.new)
    {        
        ownerMap.put(goal.Goal_Owner__c, null);        
    }
    
    ownerMap.putAll([SELECT Id, UserRole.Name FROM User WHERE Id IN :ownerMap.keySet()]);
    
    for(Al_Goal__c goal: Trigger.new)
    {
        goal.OwnerId=goal.Goal_Owner__c;
        User u = ownerMap.get(goal.Goal_Owner__c);
        goal.Role__c= u.UserRole.name;
    }
}

Alternatively... is Goal_Owner__c a lookup to a User? Are you also assuming Goal_Owner__c will always be the same as the OwnerId? If so you could not have the flat field and just a formula field.

 

All Answers

Sean TanSean Tan

When you enter a trigger context, lookup values are not immediately available in the trigger objects stored in memory, so you will have to query the values and remap it back accordingly.

 

Something like this should work:

 

trigger Goal_ChangeRecordOwner on Al_Goal__c (before insert, before update) {
    Map<Id, User> ownerMap = new Map<Id, User>{};
    for(Al_Goal__c goal: Trigger.new)
    {        
        ownerMap.put(goal.Goal_Owner__c, null);        
    }
    
    ownerMap.putAll([SELECT Id, UserRole.Name FROM User WHERE Id IN :ownerMap.keySet()]);
    
    for(Al_Goal__c goal: Trigger.new)
    {
        goal.OwnerId=goal.Goal_Owner__c;
        User u = ownerMap.get(goal.Goal_Owner__c);
        goal.Role__c= u.UserRole.name;
    }
}

Alternatively... is Goal_Owner__c a lookup to a User? Are you also assuming Goal_Owner__c will always be the same as the OwnerId? If so you could not have the flat field and just a formula field.

 

This was selected as the best answer
brozinickrbrozinickr

Makes sense.  I'd always wondered about that and now I know.  Thanks for the explanation.  Goal_Owner__c is a lookup and is always the same the OwnerId.  We would just make it a formula field, but we have this trigger in place just as fail safe.  We usually use the data loader to load our the AL_Goal__c records.  Normally, both of these fields are populated during the data load, but just in case someone forget to add the field in when loading it, then automatically corrects their mistake.  It would probably just make sense to make it a formula field, since the owner never changes it's not like we have to worry about historical owners.

 

Either way, thanks for your help, I appreciate it!

SurpriseSurprise

 

HI Sean ,

 

 

I am trying to unserstand your trigger.What are u trying to do in the below given line and why?

 

 

goal.OwnerId=goal.Goal_Owner__c;

brozinickrbrozinickr

I have two lookup fields, the standard OwnerId and a custom lookup called Goal_Owner__c.  These lookup off the user.  When we first started with salesforce, we were finding when we loaded records that OwnerId would populate with the person who actually loaded the data.  Since we didn't know better, we created a duplicate Owner field called Goal_Owner__c to put the Owner of the record there.  Then, our developers wrote a trigger that said, hey, get the Goal Owner records value and populate that into the OwnerId record.  Basically it was a fail safe just in case someone forgot to put OwnerId when they were loading our Goal records into SF.

SurpriseSurprise

Thanks