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
ccoltccolt 

get api name

Hi,

 

I need to get the api names of the items of an object.

 

I can get the Name and Value with the getMap() method. But how i can get the API name.

I have a prefix problem with managed packets, so I need this thing.

 

e.g.

custom item in Contact >> married.

API name with prefix

prefix__married_c

 

how can i get this thing?

 

i can get with getMap() or getLabel() the married__c but not with prefix.

 

thanks for help.

 

greetings.

bbrantly1bbrantly1

CCOLT,

 

 

Try this, it generates an unsorted list of Fields, the getName function returns the API name of each field. Make sure you change the ObjectName var (highlighted in red) to the object you wish to list the fields for.

 

 

List<SelectOption> out = new List<SelectOption>();
out.add(new SelectOption('','Please Choose'));

String ObjectName = 'Account'; // Object Name Goes Here
Map<String, Schema.SObjectField> ObjectFieldMap = Schema.getGlobalDescribe().get( ObjectName ).getDescribe().fields.getMap();

for (Schema.SObjectField SObjectF : ObjectFieldMap.values())
{
Schema.DescribeFieldResult field = SObjectF.getDescribe();
out.add(new SelectOption('{!' + field.getName() + '}',field.getLabel()));
}

 

 

ccoltccolt

Hi,

 

thanks bbrantly1, but as I said in my post, the getMap() method read the value and name but not the complete api-name (with prefix)

 

e.g.

CostumField in Contact married

 

I don´t need

married__c

but

mycompanyPrefix__maried__c

 

thanks for help.

 

forecast_is_cloudyforecast_is_cloudy

Try this:

String ObjectName = 'Account'; // Object Name Goes Here
Map<String, Schema.SObjectField> ObjectFieldMap = Schema.getGlobalDescribe().get( ObjectName ).getDescribe().fields.getMap();

for (Schema.SObjectField SObjectF : ObjectFieldMap.values())
{
Schema.DescribeFieldResult field = SObjectF.getDescribe();
System.debug(field.getName());
}

The 'getName()' method should return the API Name without the namespace prefix.

 

 

 

forecast_is_cloudyforecast_is_cloudy

Meant to say

'The 'getName()' method should return the API Name WITH the namespace prefix.'