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

Conditions in List FOR loop statement
Hello All,
I'm new to Salesforce Apex coding and undergoing some training and classes in Apex. I noticed that we cannot give any condition in List FOR loop. For example, lets say I've a list of 1000 Contacts in myContactList. I need to loop myContactList for one particular account instead of running all the records one by one and check inside the loop if the account name is what am expecting. I've seen we can use SOQL for this. But my question is can we not use the local list variable to have filter and loop only the records which I want to?
Something similar to below code for reference:
for (Contacts myContact : myContactList.Where(Account.Name = 'Acme')){
}
Let me know if there are any way I can achieve this using myContactList instead of having SOQL call.
Thanks.
Regards,
Mohan Babu
I'm new to Salesforce Apex coding and undergoing some training and classes in Apex. I noticed that we cannot give any condition in List FOR loop. For example, lets say I've a list of 1000 Contacts in myContactList. I need to loop myContactList for one particular account instead of running all the records one by one and check inside the loop if the account name is what am expecting. I've seen we can use SOQL for this. But my question is can we not use the local list variable to have filter and loop only the records which I want to?
Something similar to below code for reference:
for (Contacts myContact : myContactList.Where(Account.Name = 'Acme')){
}
Let me know if there are any way I can achieve this using myContactList instead of having SOQL call.
Thanks.
Regards,
Mohan Babu
Ok, well if you have a list that you can filter through and dont want to query another through soql, like I said cycle through the entire list and then make a split with the if statement.
Another option i guess would be, although there is nothing wrong with splitting it as I have done above , would be:
All Answers
Hi there,
I am not sure if this is a new way of outlining a for loop and you merely have the syntax slightly wrong, but in my opinion how I could rewrite this in two main ways:
Method 1:
Query a list with the where criteria
Method 2:
Query an entire list and add an if statement to within the for loop:
Whichever method you use would depend on what you were trying to do. Please mark this as the answer if it helps yoiu out.
Also keep in mind, i did this free hand and have been coding in c#, so the syntax might be slightly off, but I think it should be fine for copy and paste.
Cheers Michael!
Thank you for your reply. As you can see, I have mentioned the both ways which you had suggested in my question. I want to if I can write the condition in the FOR loop without SOQL to fetch only filtered values. The syntax which I've written is not correct as per Apex, but I was looking something similar to that.
Regards,
Mohan Babu
Ok, well if you have a list that you can filter through and dont want to query another through soql, like I said cycle through the entire list and then make a split with the if statement.
Another option i guess would be, although there is nothing wrong with splitting it as I have done above , would be:
I think there should be a way to have the condition where I can filter and do my operations each time I want based on different values at different locations.
Anyhow I will mark this question as answered as Salesforce does not have that option now.
Thanks.
Regards,
Mohan Babu
Hi Mohan,
Perhaps what you are interested in is using a static list? In this method you can store your filtered query as a static list and then make a new List equal to static list.
E.G.
List<Contact> FilteredList = StaticListHelper.FilteredConList;