+ Start a Discussion
SurekaSureka 

How to retrieve salesforce Id of a custom field in Apex

Hi All, I wanted to retrieve Salesforce Id of a field in Apex code. Any Idea? Thanks
crop1645crop1645

Sureka --

 

The normal Apex technique is to use the Field Describe methods as documented in the Apex developer's guide.  You'll get a token to the metadata about an SFDC field where you can then fetch all sorts of information about the field (name, label, type, etc.)  You can iterate through all fields of an SObject or iterate across all SObjects.

 

Not sure how you get an SFDC ID field to a field as I don't think fields have IDs accessible to Apex.

SuperfellSuperfell

There's no supported way to get this (because there's no supported things you can do with it once you've got it)

dgonzalezdgonzalez

I need to add support for the WebToLead servlet which does not handles picklists' Api names... instead, one has to use Api Ids for picklists

 

here is a reference of this issue:

 

http://success.salesforce.com/ideaView?id=08730000000BpISAA0

 

Regards,

Daniel

Nilesh ManeNilesh Mane

Hi Sureka,

 

Did you get solution to this problem?

I am facing the same issue.

If u have any solution to this, plz plz let me know.

 

Thanks,

Nilesh M.

SurekaSureka

Hi Nilesh,

 

I have not got any solution.

 

Thanks

Bhuvana

Nilesh ManeNilesh Mane

Thanks for your reply.

If you get any solution, please post in this blog only.

 

Thanks,

Nilesh M.

Philip FPhilip F

Hello All,

 

@SimonF, thanks for the definite answer, although there is one clear use case for the IDs - the web-to-lead functionality.  In order to post from a form, you have to post using the field ID, not the name.

 

@dgonzalez, I promoted the idea.  It's a great idea.

 

@Nilesh Mane and @Sureka, there doesn't seem to be a good way to fully access this information in code.  You can parse the web-to-lead generated HTML if you can tolerate a manual step in your workflow.  Generate the HTML, then copy/paste into a VF page that parses it and stores the values somewhere.  That's what we're going to do.


Andy BoettcherAndy Boettcher

I'll chime in on another use case.

 

In an APEX controller, I want to return a PageReference on an apex:commandButton press to send the user to a child object with pre-populated fields.

 

Right now, I have to view source and pull the fieldIds out - statically setting them either in code or in a Custom Setting to pull.  Not ideal.

 

-Andy

Alex BuAlex Bu

We actually have a work around for this issue; however it only works for the lookup fields.

 

First we could iterate all the parameters and find out those keys start with 'CF' and end with 'lkid'.

And then we compare the value with the id prefix of the SObject.

However, for the other fields we have no solution.....

 

The code:

// get sobject id prefix
String prefix = Schema.getGlobalDescribe().get('Account').getDescribe().getKeyPrefix();
String idVal = null;
Map<String, String> params = ApexPages.currentPage().getParameters();
for (String key : params.keySet()) {
  if (key.startsWith('CF') && key.endsWith('lkid')) {
    String val = params.get(key);
    if (val.startsWith(prefix)) {
      idVal = val;
      break;
    }
  }
}

 

Cyril CatonCyril Caton

Hi Alex,

 

Thanks for the workaround! I tried your solution, and it worked great for me. I just hoped there was a better way to handle this field initialization... 

 

Jereriah ManningJereriah Manning
Did anyone ever find a way to do this?

I need the record Id, the 00N value of a known field name on a specific object. I'm not on the page with the lookup field, I need it prior to sending the user there to crete the child record.

Thanks,
~J
Rahul_sgRahul_sg
Now using the tooling API you can query this information :
Example - you can run the below query in dev console to get field ids of the Lead object.
Select Id, DeveloperName, NamespacePrefix, TableEnumOrId From CustomField where TableEnumOrId ='lead'
arun kumar 443arun kumar 443
Hi Sureka,

You can get all Custom Field id's and API Name from ToolingAPI callout. You have to deploy Apex Tooling API from https://github.com/afawcett/apex-toolingapi. Here Id is Field Id, DeveloperName is API Name of that Field and  TableEnumOrId is Object name of that Field.
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req1.setHeader('Content-Type', 'application/json');            req1.setEndpoint('https://yourinstance/services/data/v38.0/tooling/query/?q=select+id,DeveloperName,TableEnumOrId+from+CustomField');
req1.setMethod('GET');
HttpResponse res1 = new Http().send(req1);
System.debug('Response Body' +res1.getBody());
Thanks,
Arun.
 
James S. CloudAnswers.comJames S. CloudAnswers.com
There is now an undocumented field on the CustomField object called "FullName" which gives you the full name of the field including the table on which it's located using dot syntax, the namespace, and the __c at the end.  The current implementation though gives you an error if you query more than 1 record at a time, but if you need it, it's there!

Here is a use-case for getting field ids:  The analytics API allows you to define buckets only using the source field as the 15 digit id of the custom field.  It won't accept any other format that I could find and will even error out when providing the 18 digit id.
Abdulla d 5Abdulla d 5
List<EntityDefinition> getFieldIDs= [SELECT DeveloperName,QualifiedApiName,NewUrl,(SELECT Id, DeveloperName, DurableId FROM Fields) FROM EntityDefinition WHERE QualifiedApiName = 'CustomObject__c'];
here you can get the objectid, and fiedld in DurableId . in the form of objectid.fieldid, you can get the field id by using the substring logic.