I'm having some trouble with my code using 2 dimensional lists.
Here's where I create the list and fill it in (not necessarily will the query return something):
List<List<Task>> Tareas_Servicio = new List<List<Task>>();
For (User Us : Servicio){
List <Task> ta = [Select Id, ActivityDate, Who.name, subject, status, OwnerId From task Where ActivityDate=:Date.today() and OwnerId=:us.id and status!='Completed'];
Tareas_Servicio.add(ta);
}
The issue is when there are no elements that match the query (which can be very likely) I get an error in the following code:
If (!Tareas_Comercial.isEmpty()){
string subject = 'Tareas Incompletas Departamento Comercial<br/><br/>';
for (integer k = 0;k < Tareas_Comercial.size();k++){
string nombre = Tareas_Comercial[K][0].who.name;
subject = subject + nombre + ': ' + Tareas_Comercial[k].size()+'<br/>';
}}
The error I receive is
System.ListException: List index out of bounds: 0
I'm assuming because the list is empty and therefore can't access any data there. That's why I added the if statement to check if the list was empty before trying to do anything with it but I keep receiving the error.
How can I avoid this error if the list is empty?