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
Vinay JVinay J 

Breaking text string to smaller parts

I have a text field which saves query like:-

SELECT Id FROM Object WHERE fieldA = TRUE and fieldB = null

Now, based on some other fields on which user provides input, I need to parse the query, remove some of the fields from where clause and add some other.. Can someone please help me in breaking down the query saved in a text field to smaller parts. The keywords 'Where', 'and' and 'Limit' will always be there in the query.

RamuRamu (Salesforce Developers) 
The below article might help
Balaji BondarBalaji Bondar
Hi Vinay,

Use dynamic query concept to from your SOQL query based on the conditions and data entered by the user:
Below is the sample :

String resolvedField1 = myVariable.field1__c;
List<sObject> sobjList = Database.query('SELECT Id FROM MyCustomObject__c WHERE field1__c = ' + resolvedField1);

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.

Andreas MeyerAndreas Meyer
just for the records:

if you let users store query strings in a textfield which are dynamically parsed you always run into the problem of possible SOQL-injections. It is the same with Balaji's solution. 

Read more here:


Vinay JVinay J
Dear All,

Thanks for your reple. Looks like I couldn't explain what I want.

I know how to form a query dynamically and execute it. I need help with regular expressions. The query SELECT Id FROM Object WHERE fieldA = TRUE and fieldB = null    is already saved in a text field. Now, for example, if user changes a picklist/lookup value, the query should be again modified based on the selection and should be updated. This all will happen in after trigger. Need help for how can I cut the existing query in to parts..