We can get the Ids of Salesforce Object’s custom fields in the Apex code using Tooling API. Following is the example code for the same:
String objectDevName = 'ObjectName';
ToolingAPI toolingAPI = new ToolingAPI();
List
Map
if (objectData!=null && !objectData.isEmpty()) {
ToolingAPIWSDL.sObject_x objectDetails = objectData[0];
Id objectId = objectDetails.Id;
List
for(ToolingAPIWSDL.sObject_x fieldDetails: customFields){
customFieldIds.put(fieldDetails.DeveloperName, String.valueOf(fieldDetails.Id).substring(0, 15));
}
}