+ Start a Discussion

How to display user's name using trigger

Hi.  I have a trigger that sends an email.  I have the email and merge fields in the trigger.   One of the merge fields is Sales Rep. It is being pulled from a user lookup field.  But the Sales Rep ID is displayed instead of the name.  How can I display the actual user's name instead of the ID?  Here is the line I am using in the trigger:


'<tr><td>Sales Representative</td><td>' + request.Sales_Representative__c + '</td></tr>'


Thanks for any help.


You'd need to retrieve the users information based on the lookup field value.


Something like:



User u = [ Select Username, LastName, FirstName From User where Id =: request.Sales_Representative__c ]; 

 Then you can write your code as:



'<tr><td>Sales Representative</td><td>' + u.Firstname + '</td></tr>'




Ankit AroraAnkit Arora

This is bit simple, just query Sales_Representative__r.Name where you are querying Sales_Representative__c and use it as a merge field.



Ankit Arora