Determine sobject Type From Id Using SOAP API

I’m using C# and would like to determine the type of an sObject based on it’s id.

I would like to pass an id to my app and have it return a list of accounts related to it. I need to identify the sobject type of the id so I can create a proper soql query for it. (If it’s an Invoice__c do one query, if it’s a Crazy_Custom_Grandchild__c do another, etc…)

I tried a .search() api call like this:

FIND {'001800000116UUiAAM'} IN Id FIELDS RETURNING Contact(Id, Phone, FirstName, LastName), 
Lead(Id, Phone, FirstName, LastName), Account(Id, Phone, Name)

but received a malformed query error because of the id field.

I don’t want to have to do a whole bunch of try ... catch calls, there has to be a better way…


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

best solution I’ve come up with so far is to use .describeGlobal() like this:

DescribeGlobalResult dgr = binding.describeGlobal();
DescribeGlobalSObjectResult dgsr = dgr.sobjects.FirstOrDefault(x =>
    string.IsNullOrEmpty(x.keyPrefix) ? false : id.StartsWith(x.keyPrefix));

    if (dgsr != null)
        switch (
            case "custom1__c":
            case "custom2__c":
            case "custom3__c":

Method 2

This approach is what I followed in Apex, I am not too sure how will it work in the SOAP API

  1. Create a Map of Schema.getGlobalDescribe();
  2. Now, key will be the first 3 letters of the ID and value will be sObject Name

Follow some Code:

//Suppose accId is a string which holds an Id    
String keyCode  = accId.subString(0,3);

//Create a Map
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();

//Iterate over all the Map Values
for(Schema.SObjectType objectInstance : gd.values()){
     if(objectInstance.getDescribe().getKeyPrefix() == keyCode){
      //do your processing with the API name what you want
      System.debug('Id is related to object: '+ objectInstance.getDescribe().getName());

I hope this helps!

Method 3

You can do this more efficiently now with the SOAP versions of the Tooling API via EntityDefinition. There is a KeyPrefix field you can search on to find the specific match.


Select Id,QualifiedApiName,KeyPrefix from EntityDefinition where KeyPrefix = '001'

Method 4

WSC + JAVA The best solution

String recordId = '00163000009j310';
String prefix = recordId.substring(0, 3);

String sobjectType = null;
for (DescribeGlobalSObjectResult sobj : connection.describeGlobal().getSobjects()){
    if (sobj.getKeyPrefix() != null) {
        if (sobj.getKeyPrefix().equals(prefix)){
            sobjectType = sobj.getName();

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x