+ Start a Discussion
Nikhil Gupta 202Nikhil Gupta 202 

Get label for a given record Id

Id recordId = 'xyz';
String objName = recordId.getSObjectType().getDescribe().getName();


I found the object Name but I also want Label for this recordId. How to use getLabel() on a given recordId?
Nikhil Gupta 202Nikhil Gupta 202

Hi charu,
Sorry but I want label of that record not the name. label will have translated value of name.

To get the label we have getLabel(). but I don't how to use that to get a particular record's label.

Abdul KhatriAbdul Khatri
Hey Nikhil

Just change getName() to getLabel(), if I understood what you need. If not can you be little more specific
 
String objLabel = recordId.getSObjectType().getDescribe().getLabel();
Nikhil Gupta 202Nikhil Gupta 202

Hi Abdul,
Sorry But this expression will give object-Name label. I want the record label.

Object Name -: Topic 
Record Id -: xyz
Record Name -: Setup 

So I want this Record Name "setup" Label

Abdul KhatriAbdul Khatri
Nikhil,

CharuDutt provided the answer to your query. I think you should try his solution and if it works mark that answer the best
Nikhil Gupta 202Nikhil Gupta 202

Sorry but that SOQL query is fetching record name. I check it  by switching the language the translation of that record name is not coming.

Thanks

Abdul KhatriAbdul Khatri
OK, May be you are looking something like below
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_tolabel.htm

You can use ToLabel in SOQL for that but I guess there are some limitations which field you can use there. Please read the above link, it may help you addressing your issue
CharuDuttCharuDutt
Hii Nikhil Gupta
Try This Code
1>           id recordId = 'a002w0000080xtjAAA';

String objName = recordId.getSObjectType().getDescribe().getLabel();
system.debug(objName);

(or)
 

2>            id recordId = 'xyz'; 
String objName = recordId.getSObjectType().getDescribe().getName(); 
system.debug(objName); 
system.debug(database.query('select name from '+ objName+ ' where Id = :recordId'));
Please Mark It as Best If It Helps
Thank You!