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);

3 Answers 3

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 .

  • Yep thanks, I did that. Only problem is it returns something like this (key, value pair) :{FirstName=Joe, Id=005Q0000001EWtDIHY}. How can I grab just the firstname and lastname as text so i can assign to post.body?
    – user988
    Nov 30, 2012 at 17:31
  • Post the code if you have that you are trying so that i can help. Nov 30, 2012 at 17:43
  • List<User> name= new List<User>(); name = [SELECT FirstName FROM User WHERE Id=:o.Opp_Owner_Name__c];post.Body = 'Concatenate this' + name.get(0);
    – user988
    Nov 30, 2012 at 18:00
  • It basically returns a list, but i just want to print out the first name and last name in the list.
    – user988
    Nov 30, 2012 at 18:10
  • 1
    Great! It worked, Thanks for the help man! You're awesome!
    – user988
    Nov 30, 2012 at 19:15

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.


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .