0

Is there a way to fetch the Object API Name using the Dynamic Object label and Schema. The other way around i have done. But i need this way to get SF API Names to be sent through Rest.

I tried for Standard Case Object and its working fine, But I have a custom object called "TMC Info", Its API name is "Agency_Info__c"

But i want this value to be dynamically fetched.

Below is the code i have used and its working for standard object, but not working for custom object :

// If i give this value as 'Case' its working, but this value 'TMC Info' is showing error as Attempt to deference NULL
String myObj = 'TMC Info';  
Schema.DescribeSObjectResult describe = Schema.getGlobalDescribe().get(myObj).getDescribe();
system.debug(describe.getName());

i fetched all the object api name and label, and i find the custom object "TMC Info" and its api name getting returned :

for ( Schema.SObjectType o : Schema.getGlobalDescribe().values() )
{
    Schema.DescribeSObjectResult objResult = o.getDescribe();
    system.debug( 'Sobject: ' + objResult );
    system.debug( 'Sobject API Name: ' + objResult.getName() );
    system.debug( 'Sobject Label Name: ' + objResult.getLabel() );   
}

Here is the picture :

Debug log for All object api

Is there anything I'm missing here ?

  • 1
    Object labels aren't required to be unique, and there are plenty of orgs where they are not. It's a risky idea to use the label as a primary identifier. – David Reed Apr 16 '18 at 12:08
  • I have to get all the picklist values for a record type. This was my primary requirement. I tried to achieve this via UI API and the end point URL for UI API needed fieldAPIName, ObjectAPIName and recordTypeID to be passed. The 3rd party environment can ask for any object's picklist values. So i wanted the UI API end point to be dynamically generated. That's why looking for Object API Name for labels. – AjaySFDC Apr 16 '18 at 12:20
  • The object label should not be used for this purpose. Your solution will be deeply fragile. You should be using the API name to identify the object in the external system. – David Reed Apr 16 '18 at 12:22
  • Okay thanks. Then if that is the only way, I will ask the external system to add all the object's api names to their system. – AjaySFDC Apr 16 '18 at 12:30
1

There is no direct way of doing this as Object label can't be unique. So you might get unexpected result. But you can use describe to get the result.

You can create a map of Label and API name and from that Map pass the label as key. But in case you have multiple object with same name you will not get correct result.

List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();    

Map<String,String> objName = new Map<String,String>();
for(Schema.SObjectType f : gd)
{
    if(f.getDescribe().isCustom())
    {
      objName.put(f.getDescribe().getLabel(),f.getDescribe().getName());
    }
}

System.debug('Object Names'+objName);
| improve this answer | |
  • This code will return all the custom object as Map of label & api names. Is it not possible to get a single Object API name via label ? Considering that the label of the object is unique. I am getting the result for Case object and not for Custom objects. – AjaySFDC Apr 16 '18 at 12:18
  • @AjaySFDC unfortunately thats not possible using label only. – Tushar Sharma Apr 16 '18 at 12:33
1

The object label should never be used as an identifier of the Sobject on an external system, because the label is not unique. If you were to construct such a solution, it could have a significant chance of breaking in the future, should

  • An object's label be altered (a change that generally shouldn't cause code-level problems, and which an admin might not know would have this side effect).
  • Two objects have the same label (a situation that could be created simply by installing a managed package, not even requiring any explicit label changes by your administrator).

To avoid this high fragility, the external system must use the object's API name as its unique identifier. If the external system has a user-facing aspect, it could source the labels for the objects from Salesforce and display these to the user, and you could dynamically fetch the display names, or cache and regularly refresh them. However, the external system's internal data storage and communication over the UI API or other Salesforce APIs should use the object's API name.

In many cases, the label and API name for standard objects are the same. This is true for Case, as you've seen. However, it's not true for other standard objects or for custom objects. For example, the standard object with API name OpportunityLineItem has the label Opportunity Product.

Note that the keys in the Map returned by Schema.getGlobalDescribe() are the API names of the objects, not the labels. That's why your example works for Case, but not for a custom object.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.