How to get Salesforce Custom Field Ids in Apex

--

How to get Salesforce Custom Field Ids in Apex
How to get Salesforce Custom Field Ids in Apex

We can get the Ids of Salesforce Object’s custom fields in the Apex code using ToolingAPI. Following is the example code for the same:

String objectDevName = 'ObjectName';
ToolingAPI toolingAPI = new ToolingAPI();
List objectData = (List)toolingAPI.query('Select Id From CustomObject Where DeveloperName = \'' + objectDevName + '\'').records;
Map customFieldIds = new Map();
if (objectData!=null && !objectData.isEmpty()) {
ToolingAPIWSDL.sObject_x objectDetails = objectData[0];
Id objectId = objectDetails.Id;
List customFields = (List)toolingAPI.query('Select Id, DeveloperName From CustomField Where TableEnumOrId = \'' + objectId + '\'').records;
for(ToolingAPIWSDL.sObject_x fieldDetails: customFields){
customFieldIds.put(fieldDetails.DeveloperName, String.valueOf(fieldDetails.Id).substring(0, 15));
}
}

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Originally published at https://blog.webnersolutions.com on April 19, 2021.

--

--

Webner Solutions
Webner Solutions

Written by Webner Solutions

Our team in Salesforce is very strong, with in-depth knowledge of Salesforce classic and Lightning development as well as the Service cloud.

No responses yet