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
Steven HoughtalenSteven Houghtalen 

How to convert an ID into a Name value?

Does anyone know how to convert an ID into a name value in Apex (not a formula)?  Thanks 
Best Answer chosen by Steven Houghtalen
Hemant_JainHemant_Jain
Hi Steven,

You will have to query the Name using the Id value that you are getting in the controller.

Please see below code for reference.
Account accObj =[select Name from Account where Id=:lookupFieldId];
System.debug('Name::' +accObj.Name)

Hope this helps.
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
you can try like below

Account.Name
Object__c.Name

Let us know if this will help you
Steven HoughtalenSteven Houghtalen
Amit,  thank you for your response.  My Visualforce Page has an InputField who's argument is a lookup field. After I select the appropriate name and I hit save, what gets sent to the custom controller is the ID and not the Name value.  My code needs the name value.  It seems like there should be an easy, straight forward way to conver the ID response to a name.value.  Perhaps not?    
Hemant_JainHemant_Jain
Hi Steven,

You will have to query the Name using the Id value that you are getting in the controller.

Please see below code for reference.
Account accObj =[select Name from Account where Id=:lookupFieldId];
System.debug('Name::' +accObj.Name)

Hope this helps.
 
This was selected as the best answer
Leo10Leo10
Hi 
you can use this code
 
String Name= Id.valueOf('yourId');

Thank you
Amit Chaudhary 8Amit Chaudhary 8
Please post your code so that we can help you
Steven HoughtalenSteven Houghtalen
Thank you Amit, Nabeel, and Hemant for your responses.  I am tied up all day today but hope to give your recommendations a try later this evening.
Steven HoughtalenSteven Houghtalen
Hi Hemant,  perfect,  it worked, thank you.   I would have thought Salesforce would have a method to do this but I guess not. 

Nabeel,  Unless I read the Developer's Guide wrong, this converts an ID to a string which is not what I needed.

Thank you all.