function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

fetching record id of object

How can i fetch record id of object in visualforcepage controller?




Simply follow this code,


in class:----

public string str{get;set;}

String str=apexpages.currentpage().getparameters().get('id');


VF page:----


ID is     {!str}


If your asking Feching the record id,

Then get Name of the record means follow this code


public class KeyPrefix
// map to hold global describe data
private static Map<String,Schema.SObjectType> gd;

// map to store objects and their prefixes
private static Map<String, String> keyPrefixMap;

// to hold set of all sObject prefixes
private static Set<String> keyPrefixSet;

private static void init() {
// get all objects from the org
gd = Schema.getGlobalDescribe();

// to store objects and their prefixes
keyPrefixMap = new Map<String, String>{};

//get the object prefix in IDs
keyPrefixSet = gd.keySet();

// fill up the prefixes map
for(String sObj : keyPrefixSet)
Schema.DescribeSObjectResult r = gd.get(sObj).getDescribe();
String tempName = r.getName();
String tempPrefix = r.getKeyPrefix();
keyPrefixMap.put(tempPrefix, tempName);

public static String GetKeyPrefix(String ObjId)
init() ;
String tPrefix = ObjId;
tPrefix = tPrefix.subString(0,3);

//get the object type now
String objectType = keyPrefixMap.get(tPrefix);
return objectType;






//a0090000002QGKu will be your object Id
System.debug('::::::: '+ KeyPrefix.GetKeyPrefix('a0090000002QGKu') );


Hi Premanath,


I want to fetch id of an custom object inside visualforce page controller which is made for different custom object.

Is it possible?

If so, then please provide the syntax.


First thing

we can measure the id of an Object means based on first three digits of record id(0039000000CIhnH)


Above code works for any Object record.


you can pass the record id from Anonymous Block or some another class.Like this

System.debug('::::::: '+ KeyPrefix.GetKeyPrefix('a0090000002QGKu') );



The ID belongs to Any Custom Object Record ok.


You can check it from Developer console.



This might help you,


Pass recordid in URL and query that id using ApexPages.currentpage.getparameters.get('id') and use this id to serve your purpose




please see above posts then you can reply,, Your post is above second post right?


What exactly are you trying to do? It seems your request has been interpreted as being the ID is known to the calling location (i.e. whatever is generating the link) and can be added there if not already. Is that the case? 


Depending on the answer the previous suggestions may not be helpful.