You need to sign in to do that
Don't have an account?
value of filter criterion for field must be of type string and should be enclosed in quotes
IN: Dynamic SQOL, Dynamic Search
Based on Jeff Douglas's article Dynamic Search,
Can anyone please provide suggestions as to how to use 'IN' in a dynamic SOQL
// runs the search with parameters passed via Javascript
public PageReference runSearch() {
String billcode = Apexpages.currentPage().getParameters().get('billcode');
List<String> parts = billcode .split('\\,');
}
if (!billcode.equals(''))
soql += ' and bill_Code__c IN '+parts+'';
In the search parameter if i pass billcode as BC23,BC42, this is the SOQL that is generated
select Name, Branch__c, F1__c, F2__c, F3__c from Obj__c Where Branch__c!=null and bill_Code__c IN (BC42,BC93) order by Branch__c asc limit 25 // quotes missing
but the search doesn't return anything. it needs to be IN ('BC42','BC93') // quotes for each passed value.
Based on Jeff Douglas's article Dynamic Search,
Can anyone please provide suggestions as to how to use 'IN' in a dynamic SOQL
// runs the search with parameters passed via Javascript
public PageReference runSearch() {
String billcode = Apexpages.currentPage().getParameters().get('billcode');
List<String> parts = billcode .split('\\,');
}
if (!billcode.equals(''))
soql += ' and bill_Code__c IN '+parts+'';
In the search parameter if i pass billcode as BC23,BC42, this is the SOQL that is generated
select Name, Branch__c, F1__c, F2__c, F3__c from Obj__c Where Branch__c!=null and bill_Code__c IN (BC42,BC93) order by Branch__c asc limit 25 // quotes missing
but the search doesn't return anything. it needs to be IN ('BC42','BC93') // quotes for each passed value.
String[] accountNumbers = new String[]{'1111'};
Account[] accounts = Database.query('SELECT Id, Name FROM Account WHERE AccountNumber IN :accountNumbers');
accounts = [SELECT Id, Name FROM Account WHERE AccountNumber IN :accountNumbers];
All Answers
Another problem is that when the number of elements in parts is large enough the string conversion won't actually return all the values.
I'm wondering why you can't just use parts directly in the query, as in:
Obj__c[] list = [SELECT Name, ... FROM Obj__c WHERE Branch__c != NULL AND bill_Code__c IN :parts];
Only variable references are allowed in dynamic SOQL/SOSL.
If i do ,
soql += ' and bill_code__c IN \''+ parts+'\'';
my querty results in
select Name, Branch__c, F1__c, F2__c, F3__c from Obj__c Where Branch__c!=null and bill_Code__c IN '(BC42,BC93)' order by Branch__c asc limit 25
Basically in my search paramenters if I pass BC42,BC93 I want all records where bill_Code__c IN (' BC42', 'BC93')
ideally my Query would be
select Name, Branch__c, F1__c, F2__c, F3__c from Obj__c Where Branch__c!=null AND (bill_Code__c = 'BC42' OR bill_Code__c = 'BC42') order by Branch__c asc limit 25
String[] accountNumbers = new String[]{'1111'};
Account[] accounts = Database.query('SELECT Id, Name FROM Account WHERE AccountNumber IN :accountNumbers');
accounts = [SELECT Id, Name FROM Account WHERE AccountNumber IN :accountNumbers];
/*
//This is how i got it to work
//creating a list of comma separated values
// iterating over this list to add to another list so that i have individualized values
//when in the search parameter user enters val1,val2,val3, it is actually interpreted as one string i.e string = 'val1,val2,val3'
// by doing what i have done in my code, i get 'val1', 'val2', 'val3'
// Let me know if you need further help or need more explanation
*/
public with sharing class ClassNameController {
public list<string> parts1 {get;set;}
public void runQuery() {
try {
system.debug('see how the SOQL is printed:' +soql);
pbatransactions =Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 1000 ');
} catch (Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
}
}
public PageReference runSearch() {
String billcode = Apexpages.currentPage().getParameters().get('billcode');
List<String> parts = billcode.split(',');
system.debug('>>>>>>>>>>>>>> ' +parts);
parts1 = new list<string> ();
for(integer i =0;i<parts.size();i++){
string squotesappended =parts[i];
parts1.add(squotesappended );
}
soql = 'select Name, Branch__c, bill_Code__c from Obj__c Where Branch__c!=null ' ;
if (!billcode.equals(''))
soql += ' and bill_Code__c IN: parts1';
runQuery();
return null;
}
}
string squotesappended =parts[i]; should be changed as below
string squotesappended ='\'' + parts[i] + '\'';