Lets say you wanna create a new instance of a custom object, lets call this Custom Object Relationship_Detail__c.

You are trying to create a link where it takes you to the proper RecordTypeId and it prepopulates the first field of Relationship_Detail__c, this field is called First_Contact__c or First_Account__c, they are take in the Name of a account of contact.

In the new relationship page you can use firebug or inspect the source to find that the ID for this custom field is CF00No00000020GfM for First_Account__c and CF00No00000020GfN for First_Contact__c. Cool, but this value changes between orgs, so i need to have access to this ID without hardcoding it.

How can i access this value? How can I access the ID for a filed of a custom object? Preferably not through using querys.

This is my code as right now:

public PageReference newRelationshipButtonClicked(){

String prefixForNewRelationship = Relationship_Detail__c.SObjectType.getDescribe().getKeyPrefix();

 Id recordTypeId;
 String  encodedObjectName ='';
 String typeOfObjectForFirst = '';


  encodedObjectName = EncodingUtil.urlEncode(myAccount.Name, 'UTF-8');
  typeOfObjectForFirst = 'CF00No00000020GfM';

  if(secondObjectIsAccount){ //both are accounts

   recordTypeId = Schema.SObjectType.Relationship_Detail__c.getRecordTypeInfosByName().get('Account-Account Relationship').getRecordTypeId();

  } else{ //im account, person i wanna have a relation to is a contact

   recordTypeId = Schema.SObjectType.Relationship_Detail__c.getRecordTypeInfosByName().get('Account-Contact Relationship').getRecordTypeId();


} else { //im a contact

  encodedObjectName = EncodingUtil.urlEncode(myContact.Name, 'UTF-8');      
  typeOfObjectForFirst = 'CF00No00000020GfN';

  if(secondObjectIsAccount){ //im a contact, other is an account

   recordTypeId = Schema.SObjectType.Relationship_Detail__c.getRecordTypeInfosByName().get('Contact-Account Relationship').getRecordTypeId();

  } else{ //we are both contacts

   recordTypeId = Schema.SObjectType.Relationship_Detail__c.getRecordTypeInfosByName().get('Contact-Contact Relationship').getRecordTypeId();


3 Answers 3


There are these two techniques for getting the field IDs programmatically:

This is assuming that the fields you are trying to default are custom object fields (which they appear to be from your URL example).

(As you probably already know there are several ways to get the record type ID including querying the RecordType object e.g. by DeveloperName.)


Following is solution to get fieldID by Object Name and its field Name, It can be useful to get ID dynamically for URL Hacking, auto populate field or other purpose

public static Id getFieldIdsByNameForSobjectType(String nameObj, String fieldName) {
    String objectTypeForQuery = nameObj;
    FieldDefinition  fieldDefinitions; 
    List<EntityDefinition> entityDefinitions = [SELECT Id, DurableId from EntityDefinition WHERE DeveloperName = :nameObj];
    String fieldNamespacePrefix ;

         String fieldNameForQuery = fieldName.replace('__c', '');
      if (fieldNameForQuery.contains('__')) {
            fieldNamespacePrefix = fieldNameForQuery.substringBefore('__');
            fieldNameForQuery = fieldNameForQuery.substringAfter('__');
        if (fieldNamespacePrefix != null && fieldNamespacePrefix != '') {
            fieldDefinitions = [SELECT Id, DeveloperName, NamespacePrefix, DurableId FROM FieldDefinition WHERE EntityDefinitionId = :entityDefinitions[0].DurableId AND DeveloperName = :fieldNameForQuery AND NamespacePrefix = :fieldNamespacePrefix];
        } else {
            fieldDefinitions = [SELECT Id, DeveloperName, NamespacePrefix, DurableId FROM FieldDefinition WHERE EntityDefinitionId = :entityDefinitions[0].DurableId AND DeveloperName = :fieldNameForQuery];

    return fieldDefinitions.DurableId.split('\\.')[1];


// You will get eg.  CF00No00000020GfN_lkid     
// getFieldIdsByNameForSobjectType('Relationship_Detail__c','First_Account__c') will return ID of custom field.

After a quick search I found this answer for your question

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

You can get custom field IDs from the tooling API so you should be able to make an HTTPRequest from apex to retrieve this information.

Code should be the following though I haven't tested this:

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

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

//query for custom fields
endpoint += 'query/?q=select+id,DeveloperName,FullName+from+CustomField+limit+1'

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

Note: Unfortunately this only works for custom fields and I don't know of any way to get this same information for standard fields.

Please note, an alternative solution would be to have a custom setting and you can store your values within there.

  • Sorry, i don't really understand the code above. Not really sure how that answers my question. I need the ID before entering the new object page, i need this ID to construct the URL
    – user10009
    Commented Jun 24, 2014 at 19:09
  • Id of your record, or the Id of the fields?
    – Double A
    Commented Jun 24, 2014 at 19:12
  • I need the ID for of the field.
    – user10009
    Commented Jun 24, 2014 at 19:15
  • Create a new Account, Contact, or custom object. On the page where you are creating this object, there will be many fields you need to fill in. Right click on it and press "inspect Element", there is an ID associated with that field. How can i access this ID without hardcoding it? This is my example URL: na17.salesforce.com/a0a/… This prepopulates First Contact name to: My Contact Name.Because of this: CF00No00000020GfN=My+Contact+Name How can i get CF00No00000020GfN in my code??
    – user10009
    Commented Jun 24, 2014 at 19:19
  • Yes, there is no current way of doing this, as stated in the referred link. This was an interesting solution someone came up with. Otherwise, you'll have to use a custom setting to store these values, so you wouldn't need to update the code since this can't be done within production.
    – Double A
    Commented Jun 24, 2014 at 19:30

You must log in to answer this question.

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