You need to sign in to do that
Don't have an account?
Error In SOQL query
Hai ,
I have a small doubt,regarding Query....
Here in string soql query I used IN operator according to Setids,but it is not working properly.
I think I am fail how to write IN operator in SOQL String.Can anyone help me how to write IN operator in String SOQl.
Below is my code.
set<id> setids=new set<id>(); // To this set I have ids.
string soql='select id,o2bc__Sell_Price__c,o2bc__Item_Code__c,name,Qty__c,(SELECT ContentType,attachment.id FROM o2bc__Item__c.attachments) from o2bc__Item__c where Id IN :setids '; // here I think it is wrong with IN operator
soql+=' order by o2bc__Sell_Price__c ';
con2 = new ApexPages.StandardSetController(Database.query(soql));
con2.setPageSize(18);
Hi,
Your syntax is correct. This is how we handle an IN operator in dynamic SOQL. However, I am not sure if you have the same code in your Org too, but the setids is only initialized, there isn't anything added in the set.
First write a method which will convert the set of ids into string.
Then use this string in the query.
Eg : Where Id IN ( 'a0AU000000CDnIL', 'a0AU000000CDnIM', 'a0AU000000CDnIG')
Try this code:
Method:
public String convertSetOfIdsIntoString(set<Id> ids) {
String idString = '';
for(Id id : ids) {
idString = idString + '\'' + id + '\',';
}
if(idString != null && idString.length() > 0){
idString = idString.substring(0, idString.length()-1);
}
return idString;
}
Modified Query:
string strIds = convertSetOfIdsIntoString(setids);
string soql='select id,o2bc__Sell_Price__c,o2bc__Item_Code__c,name,Qty__c,(SELECT ContentType,attachment.id FROM o2bc__Item__c.attachments) from o2bc__Item__c where Id IN (' + strIds + ')';
soql+=' order by o2bc__Sell_Price__c ';
Vishal is correct; you don't have any IDs loaded in the set, so that is why no results are returned. Using binding is better than creating a CSV list of ID values, since you don't have to remember to quote each ID, etc.