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

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.
this might solve your problem. try it.
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;
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:
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
BerettaJon, Try below code.
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.
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.