How to get the entity ID for a custom field in Apex?

How to get the entity ID for a custom field in Apex?

On July 19, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on How to get the entity ID for a custom field in Apex?
How to get the entity ID for a custom field in Apex

Question

I need a way to get the 15-character entity ID for a custom field on the Lead object in an Apex controller. I tried using the following method:

private String getEntityIdFor(String fieldName) {
    Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Lead.fields.getMap();
    return fieldMap.get(fieldName).getDescribe().getId();
}

However, I ran into an issue since there is no getId() method available, so this approach doesn’t work. How can I retrieve the entity ID for a custom field in Apex?

Answer:

One way to retrieve the entity ID for a custom field is by using the Tooling API. You can make an HTTP request to the Tooling API to query for the custom field’s details, including the ID.

Enhance your career with CRS Info Solutions Salesforce training, providing expert mentorship, hands-on experience, and in-depth knowledge—sign up for a free demo today!!!

Here’s an example of how you could do it in Apex:

HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');

String toolingendpoint = 'https://na1.salesforce.com/services/data/v20.0/tooling/';

// Query for custom fields
toolingendpoint += 'query/?q=SELECT Id, DeveloperName, FullName FROM CustomField LIMIT 1';
req.setEndpoint(toolingendpoint);
req.setMethod('GET');

Http h = new Http();
HttpResponse res = h.send(req);
System.debug(res.getBody());

Explanation: The code sends an HTTP GET request to Salesforce’s Tooling API to query for custom field details, specifically retrieving the Id, DeveloperName, and FullName for custom fields. It sets the authorization header using the session ID and specifies the request content type as JSON. The response from the API is captured and printed in the debug log.

Alternate approaches

Here are a couple of alternate approaches to retrieve the entity ID for a custom field in Apex.

1. Using the Tooling API

You can query the Tooling API for custom field details. This approach requires sending an HTTP request to retrieve the field information. The Tooling API is useful for getting metadata, like the Id, DeveloperName, and FullName of a custom field.

Here’s an example of how to use it in Apex:

HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');

String toolingendpoint = 'https://na1.salesforce.com/services/data/v20.0/tooling/';

// Query for custom fields
toolingendpoint += 'query/?q=SELECT Id, DeveloperName, FullName FROM CustomField LIMIT 1';
req.setEndpoint(toolingendpoint);
req.setMethod('GET');

Http h = new Http();
HttpResponse res = h.send(req);
System.debug(res.getBody());

Explanation: The code sends an HTTP GET request to the Salesforce Tooling API to retrieve custom field details. It includes an authorization header using the current session ID and queries for the Id, DeveloperName, and FullName of custom fields. The response is logged using System.debug to display the results from the API call.

2. Using the Schema.DescribeFieldResult

While you can’t directly retrieve the entity ID with getDescribe().getId(), you can still get useful information like the field’s name, type, and other metadata. For example, you can retrieve the full name of the custom field (which includes the entity ID as part of the API name) using the following approach:

Schema.SObjectField field = Schema.SObjectType.Lead.fields.getMap().get('CustomFieldName');
Schema.DescribeFieldResult fieldDesc = field.getDescribe();
String fieldFullName = fieldDesc.getName(); // This includes the API name with the entity ID
System.debug('Field Full Name: ' + fieldFullName);

Explanation: The code retrieves the Schema.SObjectField for the custom field on the Lead object by accessing it through the field map. It then uses getDescribe() to obtain the field’s metadata and calls getName() to get the full API name, which includes the entity ID. Finally, it logs the full API name, allowing you to see both the field’s developer name and its entity ID.

This approach will give you the full API name of the custom field, which includes the entity ID. However, it doesn’t give you the 15-character ID directly.

3. Using Custom Metadata or Custom Settings

If the field IDs are something you need to reference often, consider storing the field IDs in a custom metadata type or custom setting. You can then query these records in Apex without relying on Tooling API calls. This would involve creating a custom metadata type to store field IDs and querying the metadata when necessary.

For example, create a custom metadata type Field_Reference__mdt with a field Entity_ID__c to store field IDs, and then query it in Apex:

List<Field_Reference__mdt> fieldReferences = [SELECT Entity_ID__c FROM Field_Reference__mdt WHERE DeveloperName = 'CustomFieldName'];
String fieldId = fieldReferences.isEmpty() ? null : fieldReferences[0].Entity_ID__c;
System.debug('Stored Field ID: ' + fieldId);

Explanation: The code queries the custom metadata type Field_Reference__mdt for a record with a DeveloperName of ‘CustomFieldName’. It retrieves the Entity_ID__c field, which contains the field ID, and stores it in the fieldId variable. If no matching records are found, fieldId is set to null, and the result is logged using System.debug().

This approach requires you to manually maintain the field IDs in the custom metadata records but can be very useful if you need to reference these values frequently in your application.

Transform Your Career with Salesforce Training in Bangalore

Unlock the doors to a successful career with our industry-driven salesforce course. Tailored to help you become an expert in the Salesforce ecosystem, our program offers in-depth training across Admin, Developer, and AI tracks, along with expert guidance for certification and thorough interview preparation.

Our hands-on learning approach equips you with practical skills, ensuring you’re ready to tackle real-world challenges and stand out in the job market. Salesforce training in Bangalore With comprehensive class materials, personalized mentorship, and a focus on practical learning, we provide everything you need to excel.

Take the first step towards transforming your career—join our free demo class today and discover how our expert-led training can make a difference!!!

Related Posts:

Comments are closed.