How to prepopulate fields on Standard layout? - Tooling API to the rescue
Have you ever wondered populating a field's value while creating a new record on a standard detail page?
You can do so by assigning the value to field id. But how do I get the field Id? By grabbing the Id from Inspect Element? If so, then, how do you manage the field ids accross different Salesforce orgs/environments?
We have Tooling API to the rescue.
Use Tooling API when you need fine-grained access to an org’s metadata. Tooling API’s SOQL capabilities for many metadata types allow you to retrieve smaller pieces of metadata.
You can use Tooling API using REST/SOAP. We can also use these Apex Wrappers & Javascript Wrappers(jsForce).
Thank you very much Andrew Fewcett, James Loghry & Shinchi Tomita for creating these wonderful wrappers! These are awesome, timesaving & very helpful!
Let's get into hands-on in retrieving the field id using JSForce library.
In the above example, Bug is a custom object, and Type is a custom field in Bug. I am trying to retrieve the id of Type(CustomField) using Bug(CustomObject) id.
You can do so by assigning the value to field id. But how do I get the field Id? By grabbing the Id from Inspect Element? If so, then, how do you manage the field ids accross different Salesforce orgs/environments?
We have Tooling API to the rescue.
Use Tooling API when you need fine-grained access to an org’s metadata. Tooling API’s SOQL capabilities for many metadata types allow you to retrieve smaller pieces of metadata.
You can use Tooling API using REST/SOAP. We can also use these Apex Wrappers & Javascript Wrappers(jsForce).
Thank you very much Andrew Fewcett, James Loghry & Shinchi Tomita for creating these wonderful wrappers! These are awesome, timesaving & very helpful!
Let's get into hands-on in retrieving the field id using JSForce library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsforce/1.6.0/jsforce-core.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsforce/1.6.0/jsforce-api-tooling.js"></script> | |
function getFieldId(){ | |
var conn = new jsforce.Connection({ accessToken: "{!$API.Session_Id}" }); | |
var customObjectId = ''; | |
/* Retrieve CustomObject Id from DeveloperName. | |
* As we are querying from CustomObject, we need not specify '__c'. | |
*/ | |
conn.tooling.sobject('CustomObject'). | |
find({ DeveloperName: "Bug"},["Id","DeveloperName"]).execute(function(err, records) { | |
if (err) { return console.error(err); } | |
customObjectId = records.length > 0 ? records[0].Id : ''; | |
/* Query CustomField using CustomObjectId. | |
* As we are querying from CustomField, we need not specify '__c'. | |
*/ | |
conn.tooling.sobject('CustomField'). | |
find({ TableEnumOrId: customObjectId, DeveloperName : "Type"},["Id","DeveloperName"]). | |
execute(function(err, records) { | |
if (err) { return console.error(err); } | |
var customFieldId = records.length > 0 ? records[0].Id.substring(0, 15) : ''; | |
console.log(customFieldId); | |
}); | |
}); | |
} |
Good post .
ReplyDeletehttp://salesforce-walker.blogspot.in/2015/10/customfield-id-through-tooling-api.html
it also may be helpful for that.
Saw your post. Good one!
ReplyDelete