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
ran67ran67 

Getting the object name ,and field of that object using ID

Hi

I want to get the Objectname by using The Id ="a0190000003LIVw"

and i  want to get the field of that spefic object .

cn  u please help me to get this

Best Answer chosen by Admin (Salesforce Developers) 
Gunners_23Gunners_23

List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();

Map<String,String> objectMap = new Map<String,String>();
for(Schema.SObjectType f : gd)
{
     objectMap.put(f.getDescribe().getKeyPrefix(), f.getDescribe().getName()));
}

 

ID sampleId ="a0190000003LIVw";

 

String prefix = sampleId.split(0,3);

 

String objectName = objectMap.get(prefix);

 

Map<String, Schema.SObjectField> desResult = Schema.getGlobalDescribe().get(objectName).getDescribe().Fields.getMap();


List<String> fieldList = new List<String>();


fieldList.addAll(desResult.keySet());


for(integer i =0;i<fieldList.size();i++)
    System.debug(fieldList[i]);

All Answers

asish1989asish1989

HI

    please go through this link....

           http://boards.developerforce.com/t5/Apex-Code-Development/How-to-get-the-object-API-name-by-Id/td-p/258323

           http://boards.developerforce.com/t5/Apex-Code-Development/get-object-data-by-using-only-id-of-that-object/td-p/188408/page/2

 

 

I think ..Your problem will be solved ...

 

Did this post answers your questions...If so please mark it solved...so that Others get benifited...

 

Thanks

asish

Gunners_23Gunners_23

For the getting the the object name

 

List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();

Map<String,String> objectMap = new Map<String,String>();
for(Schema.SObjectType f : gd)
{
     objectMap.put(,f.getDescribe().getKeyPrefix(), f.getDescribe().getName()));
}

 

ID sampleId ="a0190000003LIVw";

 

String prefix = sampleId.split(0,3);

 

String objectName = objectMap.get(prefix);

 

Similary  you can get the fields of the object and then query it

 

 

ran67ran67

Yaa I am getting the objectname but i want the fields of the object also how to query using the sobject  

 

thanks for ur help'

asish1989asish1989

Hi 

   Try this

       For field .

               

Schema.DescribeFieldResult F = Account.AccountNumber.getDescribe();

        For Query

 

RecordType rt = [SELECT Id,Name FROM RecordType WHERE SobjectType='Account' LIMIT 1];

     For more Information please refer this link.....

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject_describe.htm

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_fields_describe.htm

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_schema.htm      

Gunners_23Gunners_23

List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();

Map<String,String> objectMap = new Map<String,String>();
for(Schema.SObjectType f : gd)
{
     objectMap.put(f.getDescribe().getKeyPrefix(), f.getDescribe().getName()));
}

 

ID sampleId ="a0190000003LIVw";

 

String prefix = sampleId.split(0,3);

 

String objectName = objectMap.get(prefix);

 

Map<String, Schema.SObjectField> desResult = Schema.getGlobalDescribe().get(objectName).getDescribe().Fields.getMap();


List<String> fieldList = new List<String>();


fieldList.addAll(desResult.keySet());


for(integer i =0;i<fieldList.size();i++)
    System.debug(fieldList[i]);

This was selected as the best answer
ran67ran67

hi am not able to get the fields of that object.

i m  getting error in this line

Compile Error: unexpected token: ',' 

 

 objectMap.put(,f.getDescribe().getKeyPrefix(), f.getDescribe().getName()));

 

Please help me to get the field of that object.......

thanks in advance

 

Gunners_23Gunners_23

 objectMap.put(,f.getDescribe().getKeyPrefix(), f.getDescribe().getName()));

 

comma is present after opening parenthesis. For getting field I've posted the code in previous reply

ran67ran67

Yaa i have kep t like that only but still am getting error . in that line .please look into it

 Compile Error: expecting a right parentheses, found ','

 

objectMap.put(,f.getDescribe().getKeyPrefix(), f.getDescribe().getName()));

Gunners_23Gunners_23

My bad!!! Please remove the comma and try to save it

ran67ran67

how can i write a query to  display the cureent account record details.

By using the Sobject.

 

Gunners_23Gunners_23

Cont from Previous

 

Below we're creating a Query

 

String Query = 'SELECT ';
for(integer i =0;i<fieldList.size();i++)
    Query += fieldList.get(i) + ',';

Query = Query.subString(0,Query.length()-1);

Query += ' FROM ' + objectName;

 

Now to run Query use Database.Query

 

List<Account> listAccount = Database.Query(Query);

 

Note i've used List<Account> you can't define  something like List<objectName>. So before running the Query determine the object type

ran67ran67

If i want  to retrive only one Record. not list.that to only the current record am getting all the Record

how can i do it