Javascript get field ids for URL

I’m trying to replicate the “New” button on a related list.

The only difference is that, if a record of that type already exists, then it simple returns an alert.

Otherwise the “New” activity should proceed as normal.

This is in order to enforce a one-to-one relationship.

So my button javscript looks something like this:


new_url = "{!URLFOR( $Action.Child__c.New)}";
sforce.connection.sessionId = '{!$Api.Session_ID}';
var query = sforce.connection.query("SELECT Id FROM Child__c WHERE Master__c = '{!Master__c.Id}'");
if (!Number(query.size)) window.location.replace(new_url);
else alert('This Master__c already has a Child__c assigned.');

This works, but the URL generated does not include information to assign the master record reference. The user would have to guess where they just came from.

I can hard code it. Eg:

new_url = "{!URLFOR( $Action.Child__c.New, null, [CF00Nm0000000VQvy=Master__c.Name, CF00Nm0000000VQvy_lkid=Master__c.Id])}";

But we all know that hard coding Ids is a road to ruin.

So how can I remove those references to field Ids (using standard pages)?

Can I write a SOQL query to pull out field ids?


In javascript you can use this formula only

{!URLFOR( $Action.Child__c.New , null, [saveURL=Master__c.Link, retURL=Master__c.Link, CF00Nm0000000VQvy_lkid=Master__c.Id , CF00Nm0000000VQvy=Master__c.Name])}

By giving hard coded ID of the field.

But we can get it through code also in apex, by using toolingAPI. Follow this link for toolingAPI

Source : Link , Question Author : powlo , Answer Author : Vivek Malpani

Leave a Comment