7

Is there any way to find out the Id of an object,object may be custom or native like if I have a custom object i.e Test__c and when I write some query to get the Id then Id must be return in this format-"01I90000000jJon" which I found from object definition page where the first 3 letter(01I) represent the custom object. Please suggest me that query.

Thanks

3
  • an ID represents an instance of a given object, e.g. a Test__c record - is that what you're after? Commented Dec 31, 2012 at 16:03
  • @user1087 - Is what you want the ID that appears in the URL when you are on an Object's definition page? For example, na14.salesforce.com/01Id0000000lPWq might take me to the Custom Object's definition page where I can add custom fields, edit page layouts, etc. You want to know how to get the '01Id0000000lPWq' via Apex? Commented Jan 1, 2013 at 20:56
  • Yes Peter exactly I need the same but u didn't suggest me that query will you please suggest me that soql.
    – user1087
    Commented Jan 2, 2013 at 7:29

3 Answers 3

7

If I understand you correctly, you want the keyPrefix of the object (i.e., the first 3 characters of one of its record's IDs).

You can use Apex Describe Information, specifically the DescribeSObjectResult's getKeyPrefix method is what you want. Here is an example with the Account object. Just change Account to whatever (e.g., Test__c) object that you want.

Schema.DescribeSObjectResult r = Account.sObjectType.getDescribe();
String keyPrefix = r.getKeyPrefix();

Alternatively, if you already have a record with the ID field you could just use the substring method. Note that you must convert the ID to a String first.

Account testRec = [Select Id from Account Limit 1];
String keyPrefix = String.valueOf(testRec.Id).subString(0, 3);
2
  • 2
    Winter'13 also added Id.getSObjectType() www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_id.htm Commented Dec 31, 2012 at 18:18
  • We only get the first record Id by using the above SOQl i.e [Select Id from Test__c Limit 1] but not Test__c Id i.e-01I90000000jOLr
    – user1087
    Commented Jan 1, 2013 at 6:10
3

I think you want to extract 15 digit record id from database.

In salesforce, When we query for record id it always returns 18 digit ID. there is no way to get 15 digit record ID as far as i know. But we can use some function further to convert 18 digit ID into 15 digit id. like

 String id = '01I90000000jJonJON'; //18 digit Id
 String id15Digit = id.substring(0, 15); // will give you '01I90000000jJon' 15 digit id

We use substring(0, 15) as this will return 15 characters starting at the 0 index.

Below are few more points regarding 15 digit and 18 digit ids :

15 digit case-sensitive version which is referenced in the UI
18 digit case-insensitive version which is referenced through the API

The last 3 digits of the 18 digit ID are a checksum of the capitalizations of the first 15 characters, this ID length was created as a workaround to legacy systems which were not compatible with case-sensitive IDs. The API will accept the 15 digit ID as input but will always return the 18 digit ID.

3
  • Thanks but my aim is not to find out the record id of a object rather I want the Object id.As we create a custom object it will also store in some object(i.e custom object) like a record so I want to find out that Id which generated when a object is created.
    – user1087
    Commented Jan 1, 2013 at 5:47
  • 1
    @PramodKumar I believe you meant to use substring(0, 15) to get the first 15 digits. Commented Feb 4, 2015 at 22:15
  • Yes... substring(0, 14) Commented Feb 5, 2015 at 5:42
1

https://saifi-dev-ed.my.salesforce.com/01I6F000001fAX7?setupid=CustomObjects

for this custom object ide is given in the url after .com/.

but using soql apex we can't do that. ALthough it's used in api to find out the id. synatx will be like

toolingAPI.query('Select Id, DeveloperName, NamespacePrefix From CustomObject Where DeveloperName = \'Test\'')

here test is the object name you can search read more about

https://andyinthecloud.com/2014/01/05/querying-custom-object-and-field-ids-via-tooling-api/

You must log in to answer this question.

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