+ Start a Discussion
melmoussaouimelmoussaoui 

Executing a String as query

Hello, I'm searching for a way to execute a string as a query

i have a method who return the string which contains the query and i want to execute it and return the values and the fields names?


thanks in advance!!

Best Answer chosen by Admin (Salesforce Developers) 
Alok_NagarroAlok_Nagarro

Hi,

 

There is a method - Dabase.query(query);

which takes query( in string) as parameter and returns the list of SObject type if it is select query.

You can use it..

 

Thanks,

All Answers

Alok_NagarroAlok_Nagarro

Hi,

 

There is a method - Dabase.query(query);

which takes query( in string) as parameter and returns the list of SObject type if it is select query.

You can use it..

 

Thanks,

This was selected as the best answer
Pradeep_NavatarPradeep_Navatar

You can use dynamic apex to execute a query as a string and access fields. Go through the dynamic apex syntax and a sample code given below :

 

Dynamic Apex syntax :

               List<sObject> L = Database.query(string);

               it Return a list of sObjects when the query returns more than a single record.

 

Sample code:

               SObject s = [select accountNumber from account limit 1];

               Object o = s.get('AccountNumber');

               s.put('AccountNumber', 'abc'); 

 

Hope this helps.

 

melmoussaouimelmoussaoui