Tuesday 17 July 2018


List has no rows for assignment to SObject  error

 Consider following query:
   
      id p;
      user u = [SELECT Id from user where Name = :username];
      if (u != null)
      p = u.Id;

  •  The above code will fail if there is no user record with the matching username. It doesn't actually return a null.


 It would be safer to do the following:

    id p;
     user[] userlist = [SELECT Id from user where Name = :username];
     if (userlist.size() > 0)
       p = userlist[0].Id;

No comments:

Post a Comment