function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mohan Babu Kutuvan JanakiramanMohan Babu Kutuvan Janakiraman 

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
Best Answer chosen by Mohan Babu Kutuvan Janakiraman
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

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. 

 

for (Contacts Con: myContactList ){
if(Con.Account.Name == 'Acme'){
//This is a filtered list

}
}


Another option i guess would be, although there is nothing wrong with splitting it as I have done above , would be:

 

//Introduce a List for every filter criteria
List<Contact> FilteredContact = new List<Contact>();

//Make a broad SOQL

//Cycle through MyContactList

//Use if statement to fill Contact Holder

if(Con.Account.Name == 'Acme'){
FilteredContact .add(Con);
}

You can now cycle through FilteredContact or any number of reduced Lists that you create on the initial cycle through.

You could also do something further with maps I guess. If you wanted to get super tricky. But, if you are just starting out and dealing with 1000 records, that should work perfectly fine.
 

 

All Answers

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

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

 

List<contact> myContactList = [select Id, Name, AccountId, Account.Name from Contact where Account.Name := 'Acme'];

for (Contacts Con: myContactList ){

}
 



Method 2:

Query an entire list and add an if statement to within the for loop:

 

List<contact> myContactList = [select Id, Name, AccountId, Account.Name from Contact];

for (Contacts Con: myContactList ){
if(Con.Account.Name == 'Acme'){
//Insert Function
}

}


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!

Mohan Babu Kutuvan JanakiramanMohan Babu Kutuvan Janakiraman
Hello 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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

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. 

 

for (Contacts Con: myContactList ){
if(Con.Account.Name == 'Acme'){
//This is a filtered list

}
}


Another option i guess would be, although there is nothing wrong with splitting it as I have done above , would be:

 

//Introduce a List for every filter criteria
List<Contact> FilteredContact = new List<Contact>();

//Make a broad SOQL

//Cycle through MyContactList

//Use if statement to fill Contact Holder

if(Con.Account.Name == 'Acme'){
FilteredContact .add(Con);
}

You can now cycle through FilteredContact or any number of reduced Lists that you create on the initial cycle through.

You could also do something further with maps I guess. If you wanted to get super tricky. But, if you are just starting out and dealing with 1000 records, that should work perfectly fine.
 

 

This was selected as the best answer
Mohan Babu Kutuvan JanakiramanMohan Babu Kutuvan Janakiraman
Thank you for response Michael. Cycling through the entire list is kind of not acceptable as it might be of not proper way and FilteredContact also would not be proper as if I want to use my list multiple times for different accounts in that case I might end up having multiple lists. 

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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

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;