Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm trying to find a way to get the "Id" for a custom object's setup page. So I'm NOT trying get a record Id. I'm working on a custom visualforce page and I'm setting up a link so that when the user clicks it, it'll direct them to the object's setup page. I've included a picture below to show what I'm am trying to get.

screenshot

Does anyone know of a way to query for that Id through Apex? I tried using the describe methods, but had no luck...

Any help/tips would be greatly appreciated!

  • Ryan
share|improve this question
add comment

1 Answer 1

up vote 10 down vote accepted

You can do this using the Salesforce Tooling API library from Apex or just the following code by itself:

//choose your object name (without suffix!)
String objectName = 'Work';

String query = 'SELECT Id From CustomObject Where DeveloperName = \'' + objectName + '\'';
String endpoint = Url.getSalesforceBaseUrl().toExternalForm();
endpoint += '/services/data/v29.0/tooling/query/?q=';
endpoint += EncodingUtil.urlEncode(query, 'UTF-8');

HttpRequest request = new HttpRequest();
request.setMethod('GET');
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
request.setEndpoint(endpoint);

HttpResponse response = new Http().send(request);
Id customObjectId = response.getBody().substringAfter('"Id":"').substringBefore('"');

//eg '01Id0000001BSeoEAG'
System.debug(customObjectId);

Which will give you something like:

'01Id0000001BSeoEAG'

parsed from the raw response:

{
    "size": 1,
    "totalSize": 1,
    "done": true,
    "records":[{
        "attributes": {
            "type": "CustomObject",
            "url": "/services/data/v29.0/tooling/sobjects/CustomObject/01Id0000001BSeoEAG"
        },
        "Id": "01Id0000001BSeoEAG"
    }],
    "queryLocator":null,
    "entityTypeName":"CustomEntityDefinition"
}
share|improve this answer
    
Thank you, that was exactly what I was looking for. I have another question. I had to set a remote site (the site I used was the one given to me by Url.getSalesforceBaseUrl() ) manually. Do you know a way to set a remote site dynamically or a way to find an even more generic web address that I could set as the remote site? I'm looking for something that'll work across different instances and not require the user to manually set the remote site for every different instance. –  user7702 Apr 7 at 21:30
    
@user7702 Maybe in lieu of a callout you can get creative with PageReference.getContent() and a relative URL which doesn't require a Remote Site Setting. –  user320 Apr 7 at 22:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.