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
huniversehuniverse 

set query condition in loop

hi

 

currently i am displaying fields in data table through following. i need to implement search and display. So is there any way to use qrystring in for  loop below  so search work fine along with datatable. Kindly suggest any idea, code, link.

 

// search logic

String qryString = 'select Id, Name,custom1__c,custom2__c from Account ' + sCondition ;

 

for(account c : [select Id, Name, custom1__c,custom2__c from Account]){
                        accountList.add(new aAccount(c));
            }

 

Thanks in advance

sfdcfoxsfdcfox

You don't need to assign it to a separate list when doing your query. Just do this:

 

accountList = Account acc:Database.query('select id,name,custom1__c... from account '+sCondition);

But remember, you must guard against SOQL injection attacks; while they are not nearly as devastating as SQL counterparts (i.e. they can't do ';DROP DATABASE org'), they could still possibly access records outside their scope (you use be using with sharing, of course, but still...) I recommend that you use variable binding with your dynamic query for maximum safety. Variable binding is done by preceding the variable name with a colon. I'd recommend that you read the documentation on dynamic SOQL for a full example on how this all works.