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?

  • You want to do this in only one line, right ? – SF_user Jun 30 '15 at 14:12
  • That's the goal – Tyager Jun 30 '15 at 14:14
  • I can do it in two lines but not one :) ! – SF_user Jun 30 '15 at 14:15
  • You do mean first and last name as opposed to their username, correct? – crmprogdev Jun 30 '15 at 14:28
  • Correct. Name will contain first and last name – Tyager Jun 30 '15 at 15:03
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;
| improve this answer | |
  • 1
    Ok, so you posted a question without try your code before ?! – SF_user Jun 30 '15 at 14:33
  • Have you tried that? It should throw an error as the query won't return a list. – crmprogdev Jun 30 '15 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 '15 at 15:01
0

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.