function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jeff BloomerJeff Bloomer 

Query for Id of a Custom Field

Is it possible to do a SOQL query that allows me to obtain the SF Id of a master-detail custom field I have in a custom object?  I'm not wanting the ID for a record in the object I want the Id for the actual field.  Field API name is Opportunity__c in a custom object with an API name of Win_Form__c.

marcobmarcob

Hi Jeff,

do i understand correctly that Opportunity__c is the master of Win_Form__c? And you need to read the actual ID of the Opportunity__c?

Br, Marco

Jeff BloomerJeff Bloomer

No, the standard Opportunity object is the parent of my Win_Form__c object.  There's a field in the Win_Form__c object called Opportunity__c, which is a master-detail relationship lookup to Opportunity.  What I want to have my query return is the Id for the Opportunity__c field itself.  Does that make sense?

 

 

marcobmarcob

Hi Jeff,

sorry, still not clear. A field does not have an ID. If you want the ID of the related opportunity object, well, that is the field Opportunity__c (or you can query Opportunity__r.id, make sure that you have the relationship name right, which you will find in the field definition of Opportunity__c in your Win_Form object definition), but that is so obvious that i suspect that i don't understand it yet. Sorry for that!

Br,

Marco

Jeff BloomerJeff Bloomer

All objects in SF have an ID.  After you create a Custom Field in a Custom Object, click on it in the "Custom Fields & Relationships" screen.  When it displays the screen with information about the field, look at the path in your address bar at the top of the screen.  After I click on Opportunity__c from the Win_Form__c Custom Object screen, the path that displays in my window is: https://cs8.salesforce.com/00NL0000002t3Ub.

 

I want a query that returns 00NL0000002t3Ub as my value, because 00NL0000002t3Ub is the ID of the Opportunity__c field.  Does this help?

sfdcfoxsfdcfox
There is literally no API for this at all. You cannot query the ID via metadata API, SOAP API, Rest API, Apex Code describe methods, etc. You have to actually log in to the application and take a look at the URL, as you've just pointed out. If you're trying to build a PageReference to a standard page that includes the field's values (which is the only reason I know of why anyone would want this information), you're going to have to work for it.

The only method that I can see of doing this programmatically is to perform screen-scraping (e.g. PageReference /ui/setup/Setup, getContent(), parse the links to find the correct link for the object, then PageReference the object's detail or field pages, and getContent(), then parse that page).

This might not help you though. Sometimes salesforce uses the 15 character identifier for custom fields, and sometimes it uses CF<sfid> instead, with no apparent reason for which it prefers. And, of course, standard fields use a different set of ID values, which can sometimes change depending on the page layout. Lookup fields and multi-select picklists are also separate in how they behave.

I think, in short, you'd be better off just making the user figure out what the ID should be, and storing this data in a custom setting.
Jeff BloomerJeff Bloomer

That's what I was afraid of.  I like to be able to assign values to variables instead of hard-coding wherever I can.  Building my code in Sandbox I will have to obtain the ID for that field once it is deployed into Production.

 

In a nutshell I was trying to get the field Id so that I didn't have to hardcode it into the PageReference I'm using to open an new instance of an object.  I have a button with javascript that looks like this:

 

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")}

var txtOpptyId = '{!Opportunity.Id}';
var txtOpptyName = '{!Opportunity.Name}';
var txtOpptyType = '{!Opportunity.Type}';
var txtTotalFirstYear = '{!Opportunity.Total_First_Year_Value_hidden__c}';
var numTotalFirstYear = Number(txtTotalFirstYear.replace(/[^\d\.\-\ ]/g, ''));
var txtWinStage = '{!Opportunity.StageName}';
var numWinForms = '{!Opportunity.Number_of_Win_Forms__c}';
var txtWinFormId = '';

if(numWinForms > 0){
	var result = sforce.connection.query("Select Id From Win_Form__c where Opportunity__c = '" + txtOpptyId + "'");
	txtWinFormId  = result.getArray("records")[0].Id;
	parent.location.href = txtWinFormId //+ "/e?retURL=%2F" + txtOpptyId;
}else if(numTotalFirstYear >= 25000 && txtWinStage == "Closed Won" && txtOpptyType !="Retention"){
	parent.location.href = "/a0c/e?CF00NL0000002t3Ub=" + txtOpptyName + "&CF00NL0000002t3Ub_lkid=" + txtOpptyId + "&retURL=%2F" + txtOpptyId;
}else if(numTotalFirstYear < 25000){
	alert("The Opportunity's First Year Total is less than $25K.");
}else if(txtOpptyType =="Retention"){
	alert("A Win Form can only be used for\nAcquisition and Expansion wins.");
}else if(txtWinStage != "Closed Won"){
	alert("To create a Win Form, the Opportunity Stage must be Closed Won.");
}

 If you take a look at the line that reads:

parent.location.href = "/a0c/e?CF00NL0000002t3Ub=" + txtOpptyName + "&CF00NL0000002t3Ub_lkid=" + txtOpptyId + "&retURL=%2F" + txtOpptyId;

 

I was trying to dynamically retrieve 00NL0000002t3Ub instead of hardcoding it into the script.  This is no huge deal though.  All I have to do is change it once I create it in Production.  This was a nice-to-have, not a gotta-have.

sachin sabusachin sabu

hi jeff,
why dont you use Custom Label.

alert('{!$Label.LabelName}');
Vijaya AmarnathVijaya Amarnath
Hi Jeff,


I think it'll help..

String objid = 'Account'; //Here if you are using custom object means give object Id (Ex: String objid='01I28000000GYeh';)
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');            
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v38.0/tooling/query/?q=select+id,DeveloperName,TableEnumOrId+from+CustomField+where+TableEnumOrId=\''+objid+'\'');
req.setMethod('GET');
HttpResponse res = new Http().send(req);
System.debug('Response : ' +res.getBody());

It'll give response as follows:

{"size":25,"totalSize":25,"done":true,"queryLocator":null,"entityTypeName":"CustomField","records":
[{"attributes":{"type":"CustomField","url":"/services/data/v38.0/tooling/sobjects/CustomField/00N28000005p7zMEAQ"},
"Id":"00N28000005p7zMEAQ","DeveloperName":"CustomerPriority","TableEnumOrId":"Account"}]}


Parse the same, and get the individual field Id's..