You need to sign in to do that
Don't have an account?
Jeff 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.
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
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?
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
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?
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.
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:
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.
hi jeff,
why dont you use Custom Label.
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..