Thursday, May 25, 2017

Finding custom fields Ids in order to pre-populate fields while creating new record

Sometimes it is required to pre-populate the custom fields while creating new record in salesforce. This can be achieved by passing all fields values which you want to populate in URL. But in order to relate url parameter value with custom field, you need know custom field Id. Instead of hard-coding custom fields Ids, you can dynamically find custom fields Ids by using Tooling API.

This will also help when you move code from one instance to another instance as you don't have change hard-coded value in URL.

Now imagine the scenario where you need to pre-populate the contact fields when user clicks on some button or link. For this you have to create URL to redirect user to new contact page. URL format will be as mentioned below:

https://{baseURL}/{object key prefix}/e?{field Id}={field value}&CF{lookup field Id}_lkid={parent recordId}&CF{lookup field Id}={parent record name}&retURL=/{retURL}

Where
  • baseURL : Your org domain URL. Use  "URL.getSalesforceBaseUrl().toExternalForm();" to find domain URL.
  • object key prefix : First 3 digit of object record ID. For example, for Account -001, Contact-003, Opportunity-006 etc. Use "Schema.getGlobalDescribe().get(ObjName).getDescribe().getKeyPrefix();" where ObjName is object API name.
  • field Id : If you need to populate custom field, then specify 15 digit custom field Id. You can use above utility class to find custom field Id.
  • field value : specify the value which you want to pre-populate for custom field.
  • lookup field Id : For custom lookup field, you need to specify field id appended by "_lkid" and corresponding 15 digit record of parent record. Remember Lookup field id should be prefix by "CF". Check URL format.
  • lookup field value: In order to populate the parent record name, specify this parameter as field Id and corresponding parent record name in {parent record name}.
  • retURL : URL where you want to redirect user after clicking on cancel button.
Note: In order to pre-populate standard fields, just right click on field in new record page and click inspect and find " <label for" xxx"> attribute as mentioned below:



Here we want to pre-populate contact last name, so after inspecting the element, we got label value as "name_lastcon2". So in order to populate last name, in URL pass parameters as

https://xxxx.my.salesforce.com/003/e?name_lastcon2=sunil&retURL=%2F003%2Fo

Now consider a scenario where we want to pre-populate below contact fields:
  • LastName
  • Email
  • Birthdate
  • Level__c (custom field)
  • Category__c(custom lookup field)

I have created a Utility class "ToolingAPIUtility"  which will return all custom field Ids of object. Just pass object API name to its static method "findCustomFieldsid". This method will return Map (Key--> field API name, Value-->custom field Id) and this can be used to create URL to pre-populate the fields while record creation.
Below is complete code to generate URL for populating above mentioned field values and required utility class.

More Blogs>>: 
USING DATABASE.UPSERT WITH EXTERNAL ID   
DYNAMIC APEX IN SALESFORCE   
SOQL INJECTION IN SOQL   
CUSTOM METADATA AND CUSTOM SETTINGS IMPLEMENTATION TRICKS   
SMART TABLE USING ANGULARJS IN VISUALFORCE PAGE   
REST API TUTORIAL FOR SALESFORCE   
VISUALFORCE COMPONENT FOR RECORD STATUS BAR   
DYNAMICALLY CREATING AND DESTROYING LIGHTNING COMPONENTS    
RAISING AND HANDLING CUSTOM EVENTS IN sALESFORCE lIGHTNING    
WHY TO USE DESIGN RESOURCE AND HOW TO ADD DYNAMIC OPTION TO DATASOURCE    
PASSING INNER WRAPPER CLASS TO LIGHTNING COMPONENT    
LIGHTNING COMPONENT FOR RECORDTYPE SELECTION FOR ANY SOBJECT    
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE   

1 comment:

  1. • Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating. Power Bi Online Training hyderabad

    ReplyDelete