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...

  • Okay, so let me get this right. Do you want to return the sObject type? or do you want related records? – subodhbahl May 7 '13 at 15:13
  • I want determine sobject type – Greg May 7 '13 at 17:01

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 (dgsr.name)
            case "custom1__c":
            case "custom2__c":
            case "custom3__c":

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!

  • 1
    thanks, but I want to do this using C# / SOAP API in an external application running on my own server, not Apex. Both our answers are conceptually along the same lines though... – Greg May 7 '13 at 17:04
  • Not a C# developer. Sorry mate, but goodluck! – subodhbahl May 7 '13 at 17:24

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'    

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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