0

This very well may not be possible.

I am performing a SOQL with a user's Id to get their name and I would like for this to be a one liner pretty much.

What I'm wondering if there is a way to do is the following

String userName = [SELECT Id, Name FROM User WHERE Id = :userId LIMIT 1][0].Name;

I would like to access the SOQL array directly and get the occurence. Is something like this possible?

5
  • You want to do this in only one line, right ?
    – SF_user
    Jun 30, 2015 at 14:12
  • That's the goal
    – Tyager
    Jun 30, 2015 at 14:14
  • I can do it in two lines but not one :) !
    – SF_user
    Jun 30, 2015 at 14:15
  • You do mean first and last name as opposed to their username, correct?
    – crmprogdev
    Jun 30, 2015 at 14:28
  • Correct. Name will contain first and last name
    – Tyager
    Jun 30, 2015 at 15:03

2 Answers 2

0

The code I posted is possible and does work. SO yes, one liner is possible.

This is a valid statement

String userName = [SELECT Id, Name FROM User WHERE Id = :userId LIMIT 1][0].Name;
3
  • 1
    Ok, so you posted a question without try your code before ?!
    – SF_user
    Jun 30, 2015 at 14:33
  • Have you tried that? It should throw an error as the query won't return a list.
    – crmprogdev
    Jun 30, 2015 at 14:33
  • I didn't try it first cause I was being lazy (trait of my profession). But yes, I have tried it and it does work. Even though it is a limit to 1, I believe that it is returning an array with just one item in it.
    – Tyager
    Jun 30, 2015 at 15:01
0

Just use String userName = [SELECT Name FROM User WHERE Id = :userId LIMIT 1];

You must log in to answer this question.

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