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
Tabrez AlamTabrez Alam 

Dynamic Query

In my first page I have used getDescribe() function to get all the object of the org & I’m passing that selected object to another page using map where I will get the list of related fields of that object. On another page I have 5 rows as we have in standard workflow for rule criteria
When I’m selecting the field, operator & entering the value & saving it, each row is getting saved as a single record, by appending these fields using formula field example. “accountnumber = 123456” (Formula field of text type).
I’ve created a master detail relationship between the 2 custom objects “Rule” & “Rule Criteria”. The records of the Rule criteria are appearing under the appropriate ‘Rule Name’ related list.
Now my requirement is I need to fetch the records under ‘Rule Name’ one by one & from the object “Rule Criteria” & append them in a single string separating them by AND , example

This is my page 
-----------------------

Object : Account (output label for getting the selected object from the previous page)
Rule Name : My Example Rule (Text Field to enter rule name)
Evaluation Criteria : Record Is Created (Dropdown List)

Field Name             Operator      Value
Account Number       Equals      123456
Email                      Contains      example
Select Field               None
Select Field               None
Select Field               None

Now the records in the object will look like
Record 1
--------------
Rule Name : My Example Rule
Field : accountnumber
Operator : =
Value = 123456
Formula String : accountnumber = 123456

Record 2
--------------
Rule Name : My Example Rule
Field : email
Operator : LIKE \’%{val}%\ (i.e contains)
Value = test
Formula String : email LIKE \’%{val}%\ example

So the query will look like “ String str = select accountnumber, email from account where Record1.formula_string AND Record2.formula _string; “
So how can I get the functionality of the operators selected &
How can I write the string
Please assist
Best Answer chosen by Tabrez Alam
MandyKoolMandyKool
Hi,

You can create a List<string> conditions = List<string>(); 
Then add each condition to this eg. conditions.add(yourCondition);
Then you can join the string as below to create a where clause of your query.
string strWhereClause = '';

strstrWhereClause = string.join(conditions, ' AND ');

You can use this where clause to generate your query.

Hope this helps!!

All Answers

MandyKoolMandyKool
Hi,

You can create a List<string> conditions = List<string>(); 
Then add each condition to this eg. conditions.add(yourCondition);
Then you can join the string as below to create a where clause of your query.
string strWhereClause = '';

strstrWhereClause = string.join(conditions, ' AND ');

You can use this where clause to generate your query.

Hope this helps!!
This was selected as the best answer
Tabrez AlamTabrez Alam
Thanks Brother
Got the string that needed thanks for help
this is the code for the query just for help of others

queryString = 'SELECT ';
        for(Rule_Criteria__c lstrc : lstRuleCriteria)
        {
            queryString = queryString + lstrc.Field__c + ',';
        }
        queryString = queryString.removeEnd(',');
        queryString +=  ' FROM ' + selectedObject;
                
        conditionList = new List<string>();
        for (Rule_Criteria__c condition : [SELECT Formula_String__c FROM Rule_Criteria__c where RuleCriteriaMD__c =: objrule.id]) 
        { 
            conditionList.add(condition.Formula_String__c); 
        }
        queryString += ' WHERE ';
        for(string s : conditionList)
            {
                queryString += s + ' AND ';
            }
            
        queryString = queryString.removeEnd(' AND ');