[SalesForce] Difference between getLocalName() and getName()

I wrote some code to get field names. I see these two different functions in the DescribeFieldResult Class. But I don't see any real time difference when I run the below code in console. How are they different?

for (Schema.SObjectType sObjectTypeVar : Schema.getGlobalDescribe().values())
{
    Schema.DescribeSObjectResult objResult = sObjectTypeVar.getDescribe();
    System.debug(objResult.getLocalName()+ '::'+objResult.getName());
}

Best Answer

Here is what I get in my DE org with a namespace (for this example say NS):

NS__My_Test_Object__c.sObjectType.getDescribe().getLocalName();
NS__My_Test_Object__c.sObjectType.getDescribe().getName();

Debug outputs as

My_Test_Object__c
NS__My_Test_Object__c

This is in the org where I have the NS set

Check via going to:

Setup -> Create -> Packages

enter image description here

If you do not see CTO next to the Namespace Prefix then CTO is not local and thus localName will include CTO__

Related Topic