+ Start a Discussion
Hari@RockzsHari@Rockzs 

Search object name using record ID?

 

Can u any one help me ,

 

   when i am enter record id ,then search will be done on all objects,the object which is having that record with that record id will be displayed in search results, how i can achieve this?

 

 

Thanx in Advance

 

Hari@Rockz

sfdc blue fish

Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

Please elaborate on your details. Its unclear from your post exactly where you are searching or what you are attempting to accomplish.


As kyle.t mentioned, in the browser you can directly access any record by using the ID in the URL.

 

However, if you are talking about doing a custom Apex SOQL call to search for a record dynamically, that is different.

 

I have a blog post with complete code about building SOQL Dynamic Search here: http://corycowgill.blogspot.com/2011/01/building-dynamic-soql-select-all-query.html

 

The code in my blog post allows you to dynamically seach any object as the code will inspect the record ID and determine the proper object to query.

All Answers

kyle.tkyle.t

If you have the ID, there is no need to search, you can just put it in the URL after salesforce.com/

 

ie https://na3.salesforce.com/006000005JXKli

Cory CowgillCory Cowgill

Please elaborate on your details. Its unclear from your post exactly where you are searching or what you are attempting to accomplish.


As kyle.t mentioned, in the browser you can directly access any record by using the ID in the URL.

 

However, if you are talking about doing a custom Apex SOQL call to search for a record dynamically, that is different.

 

I have a blog post with complete code about building SOQL Dynamic Search here: http://corycowgill.blogspot.com/2011/01/building-dynamic-soql-select-all-query.html

 

The code in my blog post allows you to dynamically seach any object as the code will inspect the record ID and determine the proper object to query.

This was selected as the best answer
Hari@RockzsHari@Rockzs

I want to develop an search engine  ,

 

working scenario:-

 

                  when i give record id as input ,then  object which contains that record with that id will display in results.

(object name  will display in results section ).The search will be done an all objects(which are present in an organization).

 

 

 

ThanX

Hari@RockZ

Sfdc Blue fish 

Willem Jan AllaartWillem Jan Allaart

The first three chars of a record id refer to the sobject the record is based on.

 

The method below will give you the schema.sobjecttype of the records sobjecs. SobjectPrefix should be the first 3 chars of the record id.

 

//get sobjecttype by sobject prefix (3 chars)
public static Schema.SObjectType getSobjectTypeByPrefix( String SObjectPrefix ){
    for( Schema.SObjectType ot : Schema.getGlobalDescribe().values() ){
        String keyPrefix = (String)ot.getDescribe().getKeyPrefix();
        if( keyPrefix != null && keyPrefix.equals( SObjectPrefix ) ){
            return ot;
        }
    }
    return null;

}

 

if the sobject type can be found you can use ObjectType.getDescribe().getName() for displaying the sobject name.

Hope this helps.

 

Hari@RockzsHari@Rockzs

can u give me help regarding this scenario with out using 

 

               String keyPrefix = (String)ot.getDescribe().getKeyPrefix();

 

because we can get problem with other environments

 

 

ThanX

Sfdc BlueFish

Willem Jan AllaartWillem Jan Allaart

What kind of problems do you expect?

 

The only way to dynamically get the name of the sobject based on a record id is by using getDescribe.

Otherwise you have to create a static map containing the prefixes of the sobjects you want to support, like

 

private static final Map<String, String> MAP_SOBJECTNAMES = new Map<String, String> {

  'prefixA' => 'objectnameA',

  'prefixB' => 'objectnameB',

  'prefixC' => 'objectnameC',

  'prefixD' => 'objectnameD',

  etc....

}

 

public String getSobjectName( String recordid ){

    if( recordid != null ){

        String sPrefix = recordid.substring(0,3);

        if( MAP_SOBJECTNAMES.containsKey( sPrefix ) ) return MAP_SOBJECTNAMES.get( sPrefix );

    }

    return null;

}

 

 

We do it dynamically since our custom application consists close to 150 custom sobjects.

TeachMeTeachMe

I was about a similar question, but I hit this.
Just want to thank you for the solution -( I was looking for just this and i got it here).
Thanks.