You need to sign in to do that
Don't have an account?
Harshwardhan Singh Karki
how to get the Id of custom field in apex(dynamically)?
Ex: Let Custom Object - A__c have Picklist Field (Custom Field) - P__c , so how to get Id of P__c in apex ?
function readOnly(count){ }
You need to sign in to do that
Don't have an account?
You can try using Tooling API. Something like below.
https://andyinthecloud.com/2014/01/05/querying-custom-object-and-field-ids-via-tooling-api/
https://salesforce.stackexchange.com/questions/17327/how-to-get-the-entity-id-for-a-custom-field-in-apex
Also check below limitation for apex describe information should also give Field ID's
https://ideas.salesforce.com/s/idea/a0B8W00000GdiFcUAJ/apex-describe-information-should-also-give-field-ids
Please mark as Best Answer if above information was helpful.
Thanks,
Use the DescribeFieldResult class to retrieve the field's metadata.
Copy code
Schema.DescribeFieldResult fieldResult = A__c.P__c.getDescribe();
Use the getId() method to retrieve the Id of the field.
Copy code
Id fieldId = fieldResult.getId();
You can also use the getName() method to retrieve the name of the field as a string.
Copy code
String fieldName = fieldResult.getName();
So the complete code will look like this:
Copy code
Schema.DescribeFieldResult fieldResult = A__c.P__c.getDescribe(); Id fieldId = fieldResult.getId();
Please keep in mind that you need to replace A__c with the actual name of your custom object and P__c with the actual name of your picklist field.
Also, you can use this approach to get the Id of any custom field, not just picklist fields, on any custom object