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
BerettaJonBerettaJon 

List has no rows? unable to prevent this common issue

Hi all, thanks for reading.

 

I have a before insert trigger on a custom object.  The trigger should query the User table and grab the Manager record where ID = the custom object's owner.managerID.

 

 List <User> userList = [select name from USER where ID = :ownerObj.ManagerID limit 1];
               if(userList.size() == 0 || userList == null)
               {
                   trigger.new[0].addError('No manager was found for the user attempting to own a Projection.  Please fill in the manager field for this user before   creating a Projection for them.');
               }
               String managerName = userList[0].name;

 

I thought my if statement would prevent String managerName = userList[0].name from ever blowing up but I am getting the list has no rows for assignment on that line and not seeing my error message inside the IF.

 

What gives? 0.1 BTC reward if you are interested. Thanks again.

Naidu PothiniNaidu Pothini
List <User> userList = [SELECT Name FROM USER WHERE Id = :ownerObj.ManagerId LIMIT 1];

if(userList.size() < 1)
{
	trigger.new[0].addError('No manager was found for the user attempting to own a Projection.  Please fill in the manager field for this user before   creating a Projection for them.');
}
else
{
	String managerName = userList[0].name;
}

 this might solve your problem. try it.

BerettaJonBerettaJon

Still not working

 

 List<User> userList = new List<User>([select name from USER where ID = :ownerObj.ManagerID limit 1]);
               if(userList.size() < 1 || userList == NULL)
               {
                   trigger.new[0].addError('No manager was found for the user attempting to own a Projection.  Please fill in the manager field for this user before creating a Projection for them.');
               }
               String managerName = userList[0].name;

Naidu PothiniNaidu Pothini
List<User> userList = new List<User>([select name from USER where ID = :ownerObj.ManagerID limit 1]);

if(userList.size() < 1 || userList == NULL) { trigger.new[0].addError('No manager was found for the user attempting to own a Projection. Please fill in the manager field for this user before creating a Projection for them.'); }
Systemd.debug('---------------------------------------------------'+userList[0].Name);
String managerName = userList[0].name; // Is this the line of code where you are getting the error? if so can you plese check the value in a debug statment?

 

BerettaJonBerettaJon

Code:

List<User> userList = new List<User>([select name from USER where ID = :ownerObj.ManagerID limit 1]);
               if(userList.size() < 1 || userList == NULL)
               {
                   trigger.new[0].addError('No manager was found for the user attempting to own a Projection.  Please fill in the manager field for this user before creating a Projection for them.');
               }
               String managerName = '';
              
                   System.debug('JON ' + userList[0].name);

 

Debug:

14:32:08.124 (124756000)|SOQL_EXECUTE_BEGIN|[25]|Aggregations:0|select name from USER where ID = :tmpVar1 limit 1
14:32:08.126 (126931000)|SOQL_EXECUTE_END|[25]|Rows:0
14:32:08.126 (126988000)|SYSTEM_METHOD_ENTRY|[25]|LIST<User>.addAll(Object)
14:32:08.127 (127007000)|SYSTEM_METHOD_EXIT|[25]|LIST<User>.addAll(Object)
14:32:08.127 (127017000)|SYSTEM_METHOD_ENTRY|[26]|LIST<User>.size()
14:32:08.127 (127037000)|SYSTEM_METHOD_EXIT|[26]|LIST<User>.size()
14:32:08.127 (127093000)|SYSTEM_METHOD_ENTRY|[28]|SObject.addError(String, String)
14:32:08.127 (127122000)|SYSTEM_METHOD_EXIT|[28]|SObject.addError(String, String)
14:32:08.127 (127284000)|FATAL_ERROR|System.ListException: List index out of bounds: 0
WannaKnowWannaKnow

Seems your Trigger.new is causing problem.

 

Can you please check what value you are getting in Trigger.new? (From debug log)

 

If you are getting values, can you please specify which trigger event you are using and also paste some more code.

 

 

Thanks,

Shailesh P

Rahul SharmaRahul Sharma

BerettaJon, Try below code.

 

List <User> userList = [SELECT Name FROM USER WHERE Id = :ownerObj.ManagerId LIMIT 1];

// Access the fiest element of array only when list is not empty, otherwise add error!
if(!userList.isEmpty())
{
    String managerName = userList[0].name;
}
else
{
    trigger.new[0].addError('No manager was found for the user attempting to own a Projection.  Please fill in the manager field for this user before   creating a Projection for them.');
}

 In your example you are null checking in if, but you are accessing userList[0] just outside, rather than accessing inside else loop. Let me know if you still face an issue.

BerettaJonBerettaJon

The most recent post looks like a good solution however I wont be able to test it until a later date.  I will return to this thread after I have tested it.