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.

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

share|improve this question
    
an ID represents an instance of a given object, e.g. a Test__c record - is that what you're after? –  user293 Dec 31 '12 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? –  Peter Knolle Jan 1 '13 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 Jan 2 '13 at 7:29
add comment

2 Answers

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, 14); // will give you '01I90000000jJon' 15 digit id

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.

share|improve this answer
    
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 Jan 1 '13 at 5:47
add comment

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);
share|improve this answer
1  
Winter'13 also added Id.getSObjectType() www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_id.htm –  techtrekker Dec 31 '12 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 Jan 1 '13 at 6:10
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.