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
akschampakschamp 

get object data by using only id of that object

Hi.....

 

I have the list of ID's only of standard/custom objects......

 

I want to get comman data for field 'lastactivitydate' for each object present in that list using id's of the same....how can i get...

 

for Ex i have record id '0013000000ZEghO' for , then how should i get 'Last activitydate' for this?

 

any suggestions or solutions....please let me know...

 

Many thanks,

Akshay

Best Answer chosen by Admin (Salesforce Developers) 
LaurentDLaurentD

Yes there is.

This code will associate the Id prefix of every object to its name. 

// Going through Salesforce Object and associate Object's name with its ID prefix
	private static void initSalesforceIdPrefix(){
		Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
		for(String sObj : Schema.getGlobalDescribe().keySet()){
		   Schema.DescribeSObjectResult r =  gd.get(sObj).getDescribe();
		   String tempName = r.getName();
		   String tempPrefix = r.getKeyPrefix();
		   System.debug('Processing Object['+tempName + '] with Prefix ['+ tempPrefix+']');
		   keyPrefixMap.put(tempPrefix,tempName);
		}
	}

 

All Answers

SurekaSureka

Hi,

 

You mean "Last Modified Date"? If so, you can try like the below:

 

Account a = [select LastModifiedDate from Account where Id=: '0013000000ZEghO'];

 

To get the Last Modified Date, you can use, a.LastModifiedDate.

 

Thanks

akschampakschamp

Hey,

 

Thanks a lot for your reply!!!!!

 

I don't know which id's are in that list it could be id of any object.

 

means - 01q300000009RKJ, a0630000006FN4r, 0013000000ZEghO, a0Q30000004GAMV....etc like this.

 

so if i pick any one id, then for that i need 'last Activity date'

 

please let me know......

 

Thanks,

Akshay

SurekaSureka

Hi,

 

In that case you can make use of Dynamic SOQL.

 

Store the Id in a string.Check for the first three characters of the Id.

 

For eg. If the first three characters are '001', then dynamically pass the object name to your dynamic query.

 

String s='0018000000iDBJW';
String id ='id';
String objectName;

if(s.substring(0,3) == '001')
{
objectName ='Account';
}
String query = 'SELECT Name FROM ' + objectName + ' WHERE ' + objectName + '.' + 
id+ '=' + '\'' + s+ '\'' ;
Account a = Database.query(query);
 This will give you the corresponding record.

 

Thanks

akschampakschamp

Hi..

Thanks again for your reply......

 

But th case is that  hav only id, dont know the object name like 'Account','Contact',........

 

I only know id, is here any way to get data?

 

thanks

SurekaSureka

Hi,

 

First three letters of the Id, specifies the Object. For eg. If the id is'001.........' , 001 specifies the Account.

I have given this extraction step in my above code.

 

Thanks

akschampakschamp

Hi.....

 

yeah....that s correct!!!!

 

But in my case object id's [standard+custom] are coming dynamically.

So using above code i can iplment for standard objects. but in my list  also having custom objects to. and that id list is dynamic.

 

So please suggest for that case. Thank you ver much for you time.

Please let me know if there is any way?

 

Thanks

akschampakschamp

Hi.....

 

yeah....that s correct!!!!

 

But in my case object id's [standard+custom] are coming dynamically.

So using above code i can iplment for standard objects. but in my list  also having custom objects to. and that id list is dynamic.

 

So please suggest for that case. Thank you ver much for you time.

Please let me know if there is any way?

 

Thanks

akschampakschamp

please help me out on above probem..........

 

Thanks

SurekaSureka

Hi,

 

I am not sure whether this will help you out or not.

 

For the custom objects, On click of the custom object tab, you will get a id. For eg.  https://na6.salesforce.com/a0E/o 

 

When you click on a record in that tab, the Id will be like "https://na6.salesforce.com/a0E80000003BUW2".

 

So the first three letters of the tab and the corresponding records are same. In your code, you can hard code like, if the first three letters are "a0E" then it belongs to the corresponding custom object and pass in the dynamic query..

 

Hope this helps.

 

Thanks

akschampakschamp

ok.......This is true....but in case of custom objetcs initials(first three letters) of id changes per organization.....so not possible to hard code as we'll not be able to work for dfferent organization.......

 

Please let me knw if have any other solution!!!!!!

LaurentDLaurentD

Yes there is.

This code will associate the Id prefix of every object to its name. 

// Going through Salesforce Object and associate Object's name with its ID prefix
	private static void initSalesforceIdPrefix(){
		Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
		for(String sObj : Schema.getGlobalDescribe().keySet()){
		   Schema.DescribeSObjectResult r =  gd.get(sObj).getDescribe();
		   String tempName = r.getName();
		   String tempPrefix = r.getKeyPrefix();
		   System.debug('Processing Object['+tempName + '] with Prefix ['+ tempPrefix+']');
		   keyPrefixMap.put(tempPrefix,tempName);
		}
	}

 

This was selected as the best answer