You need to sign in to do that
Don't have an account?

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
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
I hope it will be helpful.
Please mark it as best Answer if it resolved the issue.
BestRegards
RahulKumar
All Answers
you just need to loop with sobject type instead of account like below.
I hope it will be helpful.
Please mark it as best Answer if it resolved the issue.
BestRegards
RahulKumar
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 .
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?
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.
Please refer the below link for reference.
- https://developer.salesforce.com/forums/ForumsMain?id=9060G000000I3S2QAK
I hope it will be helpful.BestRegards
RahulKumar