+ Start a Discussion
sfdc18sfdc18 

fetching record id of object

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

PremanathPremanath

Hi

 

Simply follow this code,

 

in class:----

public string str{get;set;}

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

 

VF page:----

 

ID is     {!str}

PremanathPremanath

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

sfdc18sfdc18

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.

PremanathPremanath

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.

 

Karthik@TMCKarthik@TMC

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

PremanathPremanath

Karthik

 

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

mtbclimbermtbclimber

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.