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
Supriya Adlinge 1Supriya Adlinge 1 

Is there any way to represent string value to sobject

Hello Everyone,
this is my code.
 public static Integer[] testfetch(String rid,String sob)
{
System.debug('my Dynamic object'+sob)//it prints account,opportuinityetc.
//String sobj=('Schema.'+sob);
 String query='SELECT Id,Name from '+sob+' where  ID =:rid';
  List<Schema.account> ob = Database.query(query);
        for(account a : ob){  
        }
}
As i want to make it generic so i cant take as Schema.Account or any object. instead of account i want value of sob variable ,so what should i do to represent or convert string sob value to actual object like account , lead etc
Best Answer chosen by Supriya Adlinge 1
Rahul_kumar123Rahul_kumar123
Hi Supriya,
List<String> a=new List<String>();
a.add('Account');

<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
//This will have obj name as key and field map as value
Map<String , Map<String,Schema.SObjectField> > mapObj_FielMap = new Map<String , Map<String,Schema.SObjectField> >();

for(String objName : a)
{
Schema.SObjectType sobjType = gd.get(objName);
Schema.DescribeSObjectResult r = sobjType.getDescribe();
Map<String,Schema.SObjectField> M = r.fields.getMap();
mapObj_FielMap.put(objName , M);
}

I hope it will be helpful.

Please mark it as best Answer if it resolved the issue.

BestRegards
RahulKumar

All Answers

Ashish DevAshish Dev
Why are you prepending object name with "Schema."? Ideally you should not be. Rest is fine.

you just need to loop with sobject type instead of account like below.
 
for(sObject a: ob){  
        }


 
Rahul_kumar123Rahul_kumar123
Hi Supriya,
List<String> a=new List<String>();
a.add('Account');

<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
//This will have obj name as key and field map as value
Map<String , Map<String,Schema.SObjectField> > mapObj_FielMap = new Map<String , Map<String,Schema.SObjectField> >();

for(String objName : a)
{
Schema.SObjectType sobjType = gd.get(objName);
Schema.DescribeSObjectResult r = sobjType.getDescribe();
Map<String,Schema.SObjectField> M = r.fields.getMap();
mapObj_FielMap.put(objName , M);
}

I hope it will be helpful.

Please mark it as best Answer if it resolved the issue.

BestRegards
RahulKumar
This was selected as the best answer
Supriya Adlinge 1Supriya Adlinge 1
Hi Ashish,
Thanks For Your Reply.
Acutally ​this code is running but only for account , in my case every time object type will differ it will not just account,so instead of account i want that value in sob,which will be any object .
Supriya Adlinge 1Supriya Adlinge 1
Hi Rahul,
Thanks For Your Reply.
In my code,
     for(account a : ob) at this line, here also i want to take that value of sob instead of account ,is it      possible?
        

 
Ashish DevAshish Dev
Supriya,
Thats why in for loop cast it into sObject type, so that your code works for all the standard or custom objects.
see sObject in salesforce documentation for learning about it and its example usage.

 
Rahul_kumar123Rahul_kumar123
Hi Supriya,
Please refer the below link for reference. I hope it will be helpful.

BestRegards 
RahulKumar