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 '13 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 '13 at 6:53
  • I have answered it. – Ashwani Sep 10 '13 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 – Chirag Mehta May 21 '15 at 8:52
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 '13 at 15:18
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... :)

1

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;
                          }    
                      }
                  }

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.