6

I tried this approach, but the Salesforce Id is not being returned by getDescribe method.

Schema.DescribeFieldResult field = Schema.SObjectType.Account.fields.Name;
system.debug(' Account field, \'Name\' describe result' + field.getSObjectField().getDescribe());

Is there a way to get Salesforce Id for a field, in Apex dynamically?

4
  • Do you need Id of "fields" in custom object and standard object?
    – Ashwani
    Sep 10, 2013 at 6:45
  • I need field Ids for custom fields on custom objects. But it's good to have a method like, field.getId() to get the salesforce Id of the field for both standard and custom fields on both standard and custom objects.
    – zuke
    Sep 10, 2013 at 6:53
  • I have answered it.
    – Ashwani
    Sep 10, 2013 at 6:55
  • This is really required feature and salesforce will have to add same in near future. Upvoted Idea @ success.salesforce.com/ideaView?id=08730000000gM7mAAE May 21, 2015 at 8:52

3 Answers 3

8

There is no way to get Id for a custom or standard field except it you can retrieve name, label type, attributes etc..

The only help can be made is custom field id start from prefix 00N and in production CF appended to 00N

In addition there is a idea for which you can vote.

Conclusion: getDescribe() don't support id for field.

1
  • I've voted for on the idea.
    – zuke
    Sep 10, 2013 at 15:18
2

I know its old question but I have managed using below code, May be useful for someone. Just use correct object & field name (developerName)

string strfieldId = ''; 
                List<EntityDefinition> getFieldIDs= [SELECT DeveloperName,QualifiedApiName,NewUrl,(SELECT Id, DeveloperName, DurableId FROM Fields) FROM EntityDefinition WHERE QualifiedApiName = 'Contacts'];
                  for(EntityDefinition entityDefinition : getFieldIDs){
                      for(FieldDefinition  field : entityDefinition.Fields){
                          if(field.DeveloperName == 'Customer'){
                                 List<String> res = field.DurableId.split('\\.');
                                 strfieldId = res[1];
                                 return;
                          }    
                      }
                  }
1

You can get salesforce fields Ids using below code sample.

String prefix = Schema.getGlobalDescribe().get('Account').getDescribe().getKeyPrefix();
PageReference pr = new PageReference('/' + prefix + '/e?nooverride=1');
string html;
Blob pageContent = pr.getContent();
if(pageContent != null) {
         html = pageContent.toString();
  }
 Matcher macher = Pattern.compile('<label for="(.*?)">(<span class="requiredMark">\\*</span>)?(.*?)</label>').matcher(html);
 while(macher.find()) {
                        String label = macher.group(3);
                        String fldId = macher.group(1);
 system.debug(label+'############'+fldId);

 }

I think this might be usefull for you if i am wrong then correct me.. Happy coding... :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .