Find your content:

Search form

You are here

How to get the opportunity owner name instead of owner ID

 
Share

I am posting to chatter using an apex trigger whenever the opportunity stage changes to a certain value. The problem I'm having is that I'm trying to concatenate the opportunity owner name with some text but I can only get the ID of the owner. Is there a way I can simply get the opportunity owner name and concatenate it with the text? The query is returning the list i want but displays the key value pair, and i just want to get the firstname and lastname.

List<User> name= new List<User>();
name = [SELECT FirstName FROM User WHERE Id=:o.Opp_Owner_Name__c];
post.Body = 'Test' + name.get(0);

Attribution to: user988

Possible Suggestion/Solution #1

In the trigger context, you will get only opportunity owner id in the trigger.new() map. You have to query to get the opportunity owner name.


Attribution to: Priyanka

Possible Suggestion/Solution #2

Select o.Owner.Name, o.Owner.FirstName, o.Owner.LastName, o.OwnerId From Opportunity o where o.Id in Trigger.NewMap.Keyset();

Please use the following query as Priyanka Rightly said that we cannot get those directly in Trigger.New .


Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #3

What you could do is create a lookup to user on the opportunity. and then keep the ownerId and that lookup in sync with a workflow or trigger and then you can create formula's to that newly created lookup. then you can access all you need from that user object.


Attribution to: Sven Delporte
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4942

My Block Status

My Block Content