2

I am new to Salesforce and trying to figure out the object type using the ID which I see in the URL on the Salesforce page.

I found many places describing how to do this with the Apex language, such as http://sfdcsrini.blogspot.com/2014/12/how-to-find-object-type-from-salesforce.html.

However, I don't really know where to run this. I tried running it in Developer Console (Menu = Debug => Open Execute Anonomous Window), but if its working I don't see where its dumping the results. I tried looking at the logs tab and I don't see much there.

3 Answers 3

4

The first three characters are a case-sensitive key that describe the object. A few (literally like 2 or 3) objects don't have them, but everything else does have a key prefix. You could use a table that's been generated by Daniel Ballinger to find a pretty comprehensive list.

Or you could use my Key Prefix Scanner, which uses the Apex code you've talked about to generate a complete list of known objects by querying the system directly. I'm also working on a Lightning version, too, but for now, this page generates a list of all known object types (including undocumented ones).

Once you've done some experimentation, you'll know the common types: 001, 003, 005, 006, 00Q, 00T, 00U, etc. Deal with it frequently enough, it becomes second nature to just know at a glance.

Also, side from code, if you simply go to https://<your-instance>.salesforce.com/<key-prefix>/, you'll usually end up at the List View for a record type. For example, in my org, I can go to https://na3.salesforce.com/006, and I'll end up looking at an Opportunity List View. Of course, this doesn't work for special types, but almost all normal objects have List Views.

Also, I find it more convenient to not have to deal with debug levels (likely your original problem), so I just do this in Developer Console > Execute Anonymous:

Id recordId = '0123...';
System.assert(false, recordId.getSObjectType());

In the Developer Console, you'll get a popup saying "Assertion failed: RecordType" (or whatever the type is).

2

The best place to run the resulting query is in the "Query Editor" tab.

If you want to try out the code as well though, then use the Execute Anonymous window but add a System.debug(...); call at the end:

Id id = 'xxxxxxxxxxxxxxx';
SObjectType t = id.getSObjectType();
SObject[] records = Database.query('select Id, Name from ' + t + ' where Id = :id');
System.debug(JSON.serializePretty(records));

Here dynamic Apex is used to the SObjectType can vary.

3
  • I just tried that, it waited for a while and then said it didn't finish the query and will finish it in the background (not sure why, you'd think that if you are limiting it by ID that it wouldn't have a big return set...). Where can I find its results when its done? Nov 12, 2018 at 16:13
  • @user1015214 I would expect it to complete in a second or two.
    – Keith C
    Nov 12, 2018 at 16:21
  • See my comments to Jayant below, I'm not seeing anything useful in the log... Nov 12, 2018 at 16:23
1

If you had a system.debug(..) statement in your code snippet that you had run from anonymous window, then just open the log that was generated and filter on the "Debug Only" option. That way you will find all the debug statements printed in the log, sample below.

If you didn't have any debug statement, the log will show you the overall execution logs as generated by the statement that was executed.

enter image description here

4
  • I'm not sure if I'm doing something wrong here or not, but in my Execution Log screen, I see event types such as HEAP_ALLOCATE, VARIABLE_SCOPE_BEGIN, CODE_UNIT_STARTED, USER_INFO, but nothing of type USER_DEBUG with any useful information.... Nov 12, 2018 at 16:18
  • If you didn't have any debug statement in your snippet that you executed, you won't see it. You need to have debug statements in there
    – Jayant Das
    Nov 12, 2018 at 16:19
  • I have, here is what I have now: Id id = '0015B000009KRKzQAO'; SObjectType t = id.getSObjectType(); SObject[] records = Database.query('select Id, Name from ' + t + ' where Id = :id'); System.debug(JSON.serializePretty(records)); Nov 12, 2018 at 16:21
  • If you have the logs generated, then you should be able to see the debug logs. Try putting some custom debug logs to see if those are printed or not. Try verifying the log levels and set those appropriately, that may be one of the reasons. You can find more on the log levels on the documentation
    – Jayant Das
    Nov 12, 2018 at 21:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .