function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RCJesseRCJesse 

Getting User object field in VisualForce controller

I have a VF page with matching controller to make some links for our reps. I want to append the value from the last name field from the running user's User object to my link. In my controller i have the main url hardcoded as a string and then i add parameters to that string depending on the field values. But when if i do something like

 

link = link + 'spid=' + User.LastName;

 

What I actually get is "spid=LastName"  

 

If i try to add the value as a apex:param to a link the code "%3f" shows up where it should have an "&". 

Best Answer chosen by Admin (Salesforce Developers) 
michaelforcemichaelforce

Definitely, for that you just use the getUserId() method and query for any fields based on the user id...

 

 

String myCustomField = [select Id, myCustomField__c from User where Id=:UserInfo.getUserId() limit 1].myCustomField__c;

 

 That should do it.

 

All Answers

prageethprageeth

Hello Jesse;

Don't u like this kind of a solution? 

link = link + 'spid=' + Userinfo.getLastName();

 

 

michaelforcemichaelforce

If possible, I recommend having your controller return PageReference instances instead of a text URL.  You can then add the Last Name as a parameter like so:

 

 

myPageReference.getParameters().put('spid', UserInfo.getLastName());

 It will then make sure the characters are URL friendly... and then you don't have to worry about doing any client side redirects.

 

Just my two lincolns, in case it helps.

 

 

RCJesseRCJesse
I do like that solution. I only wish it worked now for custom User fields also. I have a custom field pid (pid__c) that houses a unique key for each user that I would like to use also. But I guess there are no built in get set methods for custom fields. Any tips for that?
michaelforcemichaelforce

Definitely, for that you just use the getUserId() method and query for any fields based on the user id...

 

 

String myCustomField = [select Id, myCustomField__c from User where Id=:UserInfo.getUserId() limit 1].myCustomField__c;

 

 That should do it.

 

This was selected as the best answer
RCJesseRCJesse
Of course, I should have thought of that before. Thanks Michael.