[SalesForce] Get Label For SObject API Name

I have an exhaustive list of custom objects whose API Names and labels does not match. I end up opening each object in order to determine if the label has matching API Name.I want to see the API Name displayed along with label on the result page of custom Objects. Is there a better way to get label for a custom object when its API Name is known?

Best Answer

You can get the label using the DescribeSObjectResult:

DescribeSObjectResult describe = SObjectType.MyApiName__c;
system.debug(describe.getLabel());

Another common syntax is:

DescribeSObjectResult describe = MyApiName__c.sObjectType.getDescribe();

You can also just one-line it:

system.debug(SObjectType.MyApiName__c.getLabel());
Related Topic