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 calledFirst_Contact__c
orFirst_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 forFirst_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 = ''; if(firstObjectIsAccount){ 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(); }
Answer
There are these two techniques for getting the field IDs programmatically:
- Querying Custom Object and Field IDs via Tooling API – recently available and relatively robust
- Finding Visualforce fields ids – older and potentially fragile (but has been in in use for several years in some of my code)
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.)
Attribution
Source : Link , Question Author : user10009 , Answer Author : Keith C