# Is there a way to dynamically generate conditions in a loop

I currently have a batch proces which gets an amount of records as a scope. I want to execute some manipulations on these records based on some conditions where only 1 value changes and where the amount of conditions could change over time. Is there a way to do this dynamically without looping several times trough the scope

Example

``````List<Account> scope

for(Account acc : scope){
if (acc.number__c > 1){
//condition 1 do something: acc.number__c = acc.number__c + 1;
}
if (acc.number__c > 2){
//condition 2 do something: acc.number__c = acc.number__c + 2;
}
if (acc.number__c > 3){
//condition 3 do something: acc.number__c = acc.number__c + 3;
}
}
``````

So can ik generate condition 1 till 3 dynamically so next time I could generate as many conditions as needed and loop troug scope only once?

• What determines the number of conditions dynamically in your case? – arut Apr 23 at 12:00
• @arut Its dependent om a map which get filled with amount of conditions. – Thomas Apr 23 at 12:17
• So, can I assume that the length of the map determines the number of conditions and each element in the map defines the condition? – arut Apr 23 at 12:49
• @arut Yes that is completely correct. – Thomas Apr 23 at 13:59
• Does the answer provided by @TylerZika work in your case? – arut Apr 23 at 18:46

``````List<Account> scope
List<Integer> conditions = new List<Integer>{1, 2, 3, 4, 5, 6};

for(Account acc : scope){
for(Integer c : conditions) {
if (acc.number__c > c){
//condition 1 do something: acc.number__c = acc.number__c + c;
}
}
}
``````
• In this case how to test for first condition and in this case this will loop trough all conditions for every scope record right? Isnt it possible to order scope by number__c asc and only loop trough left conditions so you only loop once trough all conditions for the whole scope? – Thomas Apr 24 at 5:42
• I don't fully understand what you are asking. What determines that you only test for the first condition? My code will loop through all conditions. You can use `ORDRY BY` in a SOQL of data or use a custom sort to order your conditions. I don't understand your last question. – Tyler Zika Apr 24 at 5:48
• lets say scop is 50.000 record so in this case I would loop 50.000 times trough all possible conditions? 6x – Thomas Apr 24 at 6:16

For anyone who bumps into this. I ended resolving this the following way which seems to work

``````integer y = 0;
decimal x = conditionList[y].Days_Due__c;

for (Account acc : accList) {
if (acc.DDate__c >= x) {
if(acc.DDate__c > x){
y = y + 1;
x = conditionList[y].Days_Due__c;
}
if (acc.DDate__c == x) {
acc.Status__c = conditionList[y].Status_NDue__c;