You need to sign in to do that
Don't have an account?

How to create a loop based on a query from a string?
Standard loops with inline SOQL look something like this:
for(Item_Detail__c S : [SELECT Amount__c, Name, Id FROM Item_Detail__c WHERE Item__c in :ItemsSet]) { // code inside the loop goes here }
Due to the lack of a Select * in SOQL, I would like to generate the select part of the query but at the end append the :ItemsSet part.
This will allow me to build a query on all fields but have the where condition dependant on the set and be able to loop thru the results of the query.
Does anyone have an example of how to do this?
Thanks!
A simple change got it working. The bind variable is set in a static string.
Here is the updated code:
Thanks for the help!
All Answers
You need to set it up a little differently:
List<Item_Detail_c> slist = new List<Item_Detail__c>;
String query = 'SELECT Amount__c, Name, Id FROM Item_Detail__c WHERE Item__c in :' + someVariable;
slist = Database.query(query);
for (Item_Detail__c s: slist){
//code
}
where someVariable is the string that you want to dynamically generate.
Let me know if you have any questions.
Thank you for the reply. When I get a moment, I will try it out. Of course, if it works I will mark your post as the solution. Thanks again.
More to follow ....
I just now got a chance to look at this and it isn't working.
This code:
Throws this exception: EXCEPTION_THROWN|[135]|System.QueryException: unexpected token: '{'
From the database query line. The debug log shows this is the query:
It looks like this way of 'building' a bind varaible into a SOQL string doesn't work. Thanks for the reply though. I am open to any other ideas.
Thanks
A simple change got it working. The bind variable is set in a static string.
Here is the updated code:
Thanks for the help!
No problem