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
Sandesh D GanjareSandesh D Ganjare 

Want to validate the filter logic.

I am generating the dynamic query in salesforce using the field selected on the vf page. 
e.g. 1 Id = 'GHDkmdfjnsdf21'
        2 name != 'Test'
        3 city = 'pune'
Now i have text box where user can enter his/her logic like 1 AND 2 AND 3 || 1 OR 2 OR 3 so if there is combination of AND/ OR like 
1 AND 2 OR 3 then the SOQL query will be wrong correct logic is 1 AND (2 OR 3). so I want to validate this logic.

Thanks in advance. 
Shweta_AgarwalShweta_Agarwal
Hi Sandesh,

Find below code according to your requirment.
This code will add brackets '()' if user will enter combination of AND/OR
String slogic = '1 OR 2 AND 3';
if(slogic.contains('OR') && slogic.contains('AND') ){
	system.debug('--slogic--'+slogic);
	string orLogicStr = slogic.substring((slogic.indexOfIgnoreCase('OR')-2),(slogic.indexOfIgnoreCase('OR')+4));    
	system.debug('--orLogicStr--'+orLogicStr);
    orLogicStr = '('+orLogicStr+')';
    string andLogicStr;
    if(slogic.indexOfIgnoreCase('AND') > slogic.indexOfIgnoreCase('OR')){
    	andLogicStr = slogic.substring(slogic.indexOfIgnoreCase('AND')); 
		slogic = orLogicStr +' '+ andLogicStr;
    }
    else{
    	andLogicStr = slogic.substring(0,slogic.indexOfIgnoreCase('AND')+3);     
    	slogic = andLogicStr +' '+ orLogicStr;
    }
    system.debug('--andLogicStr--'+andLogicStr);
    system.debug('--slogic--'+slogic);
}

Hope this will help you

Thanks