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
Dave BerenatoDave Berenato 

Use UserID to get UserName

I'm currently using an Apex Class to generate a list of records owned by a specific User.
 
public TelemarketerDialing(){

        if(ApexPages.currentpage().getParameters().get('leadgenerator') == null)
                       {myParam = UserInfo.getUserId();}

        else{myParam = ApexPages.currentpage().getParameters().get('leadgenerator');}
        }

      public String myParam { get; set; }

I build a Visualforce page for Managers to basically click in to the list of any User using the Visualforce page URL of that individual LeadGenerator's page.
 
<td><a href="https://corere--c.na50.visual.force.com/apex/DoorKnockerPortal?leadgenerator=0056A000000f0af">Angel</a></td>

        <td><a href="https://corere--c.na50.visual.force.com/apex/TelemarketerPortal?leadgenerator=0056A000000e779">Antonio</a></td>

        <td><a href="https://corere--c.na50.visual.force.com/apex/TelemarketerPortal?leadgenerator=0056A000001IVgU">Ary</a></td>
I'm wondering if there's a way to user the UserID to display the name of the User you are viewing on the page.

Something like:
public String LeadGeneratorName = getUserInfo(myParam).UserName;
Is this possible?


 
Best Answer chosen by Dave Berenato
Manish  ChoudhariManish Choudhari
Hi Dave,

Modify your code like below:
 
public AgentPortal(){

    public String myParam { get; set; }	// Declare both variable on 
    public String userName { get; set; }    // starting of controller

        if(ApexPages.currentpage().getParameters().get('agent') == null){myParam = UserInfo.getUserId();}
        else{myParam = ApexPages.currentpage().getParameters().get('agent');}
            	User usr = [Select Name From User where Id = :myParam ];
			    userName = usr.Name; //remove String userName and only use userName
        }

Change "String userName= usr.Name;"  to "userName = usr.Name;"

Let me know if this helps.

Thanks,
Manish

All Answers

Manish  ChoudhariManish Choudhari
Hi Dave,

You can use SOQL to retrieve the user name if you have userId. SOQL that needs to be used:
User usr = [Select name From User where Id='0056F000006eynwQAA']; //replace id with actual user id

String userName = usr.Name; //use "usr.Name" to access user name of this record


//above query will run for one user id
//if you have list of userId, you can get the user names from below query
//lets say you have list      "userIDs"       that has all userIds

List<User> usrs =  [Select name From User where Id=: userIDs]; 
//get all user names using below for each loop

for(User u : usrs){
    String userName = u.Name; //retrieve user name of user record
}

Let me know if this helps.

Thanks,
Manish
Glyn Anderson 3Glyn Anderson 3
Dave,

Try the following to generate your page with the links by name.  I used Profile to determine who to display and what portal to link to, but you can change that to meet your requirements.  Disclaimer:  This is completely untested.  Don't be surprised if it doesn't compile the first time, but hopefully it conveys the idea.

<pre>
// Controller:
private Map<String,String> pageByProfile = new Map<String,String>
{   'Door Knocker' => 'DoorKnockerPortal'
,   'Telemarketer' => 'TelemarketerPortal'
}

public class LeadGenerator
{
    public Id userId;
    public String name;
    public String portal;

    public LeadGenerator( Id userId, String name, String portal )
    {
        this.userId = userId;
        this.name = name;
        this.portal = portal;
    }
}

public List<LeadGenerator> leadGenerators
{
    get
    {
        if ( leadGenerators == null )
        {
            leadGenerators = new List<LeadGenerator>();
            for ( User user :
                [   SELECT  Id, Name, Profile.Name
                    FROM    User
                    WHERE   Profile.Name IN :pageByProfile.keySet()
                ]
                (
            {
                leadGenerators.add
                (   new LeadGenerator
                    (   user.Id
                    ,   user.Name
                    ,   pageByProfile.get( user.Profile.Name )
                    )
                );
            }
        }
        return leadGenerators;
    }
    private set;
}


// Visualforce Page:
<apex:repeat value="{!leadGenerators}" var="leadGenerator">
    <apex:outputLink value="{!'/apex/' + leadGenerator.portal + '?leadgenerator=' + leadGenerator.userId}">
        {!leadGenerator.name}
    </apex:outputLink>
</apex:repeat>
</pre>
Dave BerenatoDave Berenato
Hi Manish,

Were you thinking something like this?
 
public AgentPortal(){
        if(ApexPages.currentpage().getParameters().get('agent') == null){myParam = UserInfo.getUserId();}
        else{myParam = ApexPages.currentpage().getParameters().get('agent');}
            	User usr = [Select Name From User where Id = :myParam ];
			    String userName = usr.Name;
        }
    
    public String myParam { get; set; }	
    public String userName { get; set; }

For some reason I'm able to display {!myParam} in the visualforce page but {!userName} returns nothing. Any suggestions?
Manish  ChoudhariManish Choudhari
Hi Dave,

Modify your code like below:
 
public AgentPortal(){

    public String myParam { get; set; }	// Declare both variable on 
    public String userName { get; set; }    // starting of controller

        if(ApexPages.currentpage().getParameters().get('agent') == null){myParam = UserInfo.getUserId();}
        else{myParam = ApexPages.currentpage().getParameters().get('agent');}
            	User usr = [Select Name From User where Id = :myParam ];
			    userName = usr.Name; //remove String userName and only use userName
        }

Change "String userName= usr.Name;"  to "userName = usr.Name;"

Let me know if this helps.

Thanks,
Manish
This was selected as the best answer
Dave BerenatoDave Berenato
Amazing help. Thank you so much!