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
kunal kaushik 13kunal kaushik 13 

I have a SOQL Query in String form and i want to get the Object name on which the query is running. How can i do so?

Example : String str = ' SELECT Name,Id FROM Account LIMIT 10';
Now i want to retrieve "Account" from this query for further use.
Agustin BAgustin B
hi, you can use the id you get from the query and get the object name:
String objectName = objectId.getSobjectType().getDescribe().getName();

if it helps please mark as correct, it may help others
Foram Rana RForam Rana R
Hi Kunal,

Use the below Code.
String str = ' SELECT Name,Id FROM Account LIMIT 10';
String str2 = str.substringAfter('FROM');
String str3 = str2.substringBefore('LIMIT');
system.debug('Expected is ' + str3);

Mark, It as best answers If it helps you

Thanks,
Foram Rana