I have a requirement to get the ID of a custom field in apex .

When we navigate to Setup-->Create-->Object--> Click on Object --> Click on field .In field detail page url we can find field Id


how that can be accomplished dynamically through Apex?

Thanks in advance

  • Just now i solved it and posted the answer . Thanks for your help @Daniel Ballinger Commented Oct 8, 2015 at 9:42

2 Answers 2


You can use Tooling Api for this. Code Sample

// Constructs the Tooling API wrapper (default constructor uses user session Id)
ToolingAPI toolingAPI = new ToolingAPI();

// Query CustomObject object by DeveloperName (note no __c suffix required)
List<ToolingAPI.CustomObject> customObjects = (List<ToolingAPI.CustomObject>)
     toolingAPI.query('Select Id, DeveloperName, NamespacePrefix From CustomObject Where DeveloperName = \'Test\'').records;

// Query CustomField object by TableEnumOrId (use CustomObject Id not name for Custom Objects)
ToolingAPI.CustomObject customObject = customObjects[0];
Id customObjectId = customObject.Id;
List<ToolingAPI.CustomField> customFields = (List<ToolingAPI.CustomField>)
     toolingAPI.query('Select Id, DeveloperName, NamespacePrefix, TableEnumOrId From CustomField Where TableEnumOrId = \'' + customObjectId + '\'').records;

// Dump field names (reapply the __c suffix) and their Id's
System.debug(customObject.DeveloperName + '__c : ' + customObject.Id);
for(ToolingAPI.CustomField customField : customFields)
          customObject.DeveloperName + '__c.' +
          customField.DeveloperName + '__c : ' +

For more information you can read Blog Post. you can find tooling API wrapper here or if you want to do it manually then you can read the doc. But query will remain same for it.

  • Already i tried this Tooling API . when i execute the above code "invalid Object name CustomObject " error is coming .Whether this code works for you? Commented Oct 7, 2015 at 12:12
  • 'SELECT ID,DeveloperName From CustomField Where DeveloperName = \'FieldName(Without __c)\''; can you share your code it will help to identify or take help from doc . they are so easy to understand. Commented Oct 7, 2015 at 12:19
  • where i need to place this code.I am trying to execute the simply in Execute Anonymous ,'ToolingAPI toolingAPI = new ToolingAPI();' but the error Invalid type: ToolingAPI.Sorry i am newbie:) Kndly help me out Commented Oct 7, 2015 at 12:52
  • you need to install wrapper class package from given link. then try to run this code Execute Anonymous Commented Oct 7, 2015 at 12:54

Finally i got it done with Remote Action method for ToolingAPI.

        id objid = '01IG0000002EHp5';//Id of the object
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        req.setHeader('Content-Type', 'application/json');
        req.setEndpoint( URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v28.0/tooling/query/?q=Select+id,DeveloperName+from+CustomField+where+TableEnumOrId=\''+objid+'\'');
        Http h = new Http();
        HttpResponse res = h.send(req);    
        string a = res.getBody(); 
        list<String> lst = new list<String>();
        Matcher m = Pattern.compile('"Id":"(\\w+)","DeveloperName":"(\\w+)"').matcher(a);
        while(m.find()) {

Finally it returns a list with Fieldname:FieldId like below,

(Field1:00NG000000DFGuwMAH,   Field2:00NG000000DFGvXMAX,   Field3:00NG000000DFGvYMAX)

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