0

I have below piece of code where it throws Error List has no row to assign at below marked line.

Code

get
    {   terrid = [select UserId, TerritoryID from Userterritory where UserID=: UserInfo.getUserId()].TerritoryID;
        ParentTerr = [select Parentterritoryid,ID from territory where ID=: terrid].Parentterritoryid;
        List<territory> territoryList= [select Parentterritoryid,ID from territory where ID=: ParentTerr ];
            if(territoryList!=null && !territoryList.isEmpty()){
                 PterrId = territoryList.get(0).Parentterritoryid;
            }
        DM = [select UserId,territoryid from userterritory where territoryid =: ParentTerr ].UserId; // This line throws error
        RM = [select UserId,territoryid from userterritory where territoryid =: PTerrId ].UserId;
        UserEmail=[select email from user where id = :UserInfo.getUserId()].Email;

        UserTemp = [Select u.LastName, u.Id, u.FirstName, u.Email From User u limit 1];

        UserList = new List<SelectOption>();

        for(User temp : UserTemp)
        {
            UserList.add(new SelectOption(temp.Id, temp.Email));

        }
        return UserList;
    }
        set;
5
  • The line tries to assign an ID to 'PterrId' - which I assume is an ID variable. Essentially the error means that the query on that line is not returning any records.
    – Prajith
    Feb 17 '16 at 8:23
  • Another point, the query 'select Parentterritoryid,ID from territory where ID=: ParentTerr' returns a List<territory> . So you cannot access the 'Parentterritoryid' of a list, you need to specify which element of the list and then access the 'Parentterritoryid'. Thanks
    – Prajith
    Feb 17 '16 at 8:26
  • Can you please correct me my code? I am not getting why Id can't be assigned? Feb 17 '16 at 8:33
  • can you confirm the second query always returning valid values?
    – highfive
    Feb 17 '16 at 8:41
  • Might not be , so it needs a null check. Feb 17 '16 at 9:03
1

Always try to use list.

Check if list is not empty then get the value and assign to variable.

 List<territory >   lstterritory = [select Id, Parentterritoryid
                                      from territory where ID=: ParentTerr ];
if(!lstterritory.isEmpty())
   PterrId  = lstterritory[0].Parentterritoryid;

I suggest do for all the Queries.


Updates

List<Userterritory>  lstUserterritory = [select UserId, TerritoryID from Userterritory where UserID=: UserInfo.getUserId()];
if(!lstUserterritory.isEmpty()){
    terrid = lstUserterritory[0].TerritoryID;
}

List<territory> lstterritory = [select Parentterritoryid,ID from territory where ID=: terrid].Parentterritoryid;
if(!lstterritory.isEmpty()){
    ParentTerr = lstterritory[0].Parentterritoryid;
}

List<territory >   lstterritory = [select Id, Parentterritoryid
                                      from territory where ID=: ParentTerr ];
if(!lstterritory.isEmpty()){
   PterrId  = lstterritory[0].Parentterritoryid;
 }

List<userterritory> lstUserterritory =[select UserId,territoryid from userterritory where territoryid =: ParentTerr ];
if(!lstUserterritory.isEmpty()){
     DM = lstUserterritory[0].UserId;
}

List<userterritory> lstUserterritory1 =[select UserId,territoryid from userterritory where territoryid =: PTerrId ];
if(!lstUserterritory1.isEmpty()){
     RM = lstUserterritory1[0].UserId;
}

List<user> lstuser =[select email from user where id = :UserInfo.getUserId() ];
if(!lstuser.isEmpty()){
     UserEmail = lstuser[0].email;
}
3
  • Please help. I am unable to do the change. Feb 17 '16 at 9:11
  • @SFDC_Learner one min
    – Ratan Paul
    Feb 17 '16 at 9:14
  • @SFDC_Learner check my updated ans
    – Ratan Paul
    Feb 17 '16 at 9:21
1

As Stygon and highfive said that the query is not returning any results.

The ideal approach world be to query a list first. Check the size of the list. If the size is greater than zero then use the data to get the Parentterritoryid. See Salesforce Solution

Since you are trying to extract a Parentterritoryid directly from query and the where clause contains ID , Salesforce will try to retrieve only one SObject which if not found will lead to the error you are getting.

Try this:

List<territory> territoryList= [select Parentterritoryid,ID from territory where ID=: ParentTerr ];
if(territoryList!=null && !territoryList.isEmpty()){
     PterrId = territoryList.get(0).Parentterritoryid;
}
3
  • It worked but again same error here. DM = [select UserId,territoryid from userterritory where territoryid =: ParentTerr ].UserId; Feb 17 '16 at 9:07
  • Please help. I am unable to do the change. Feb 17 '16 at 9:11
  • Please use the lists everywhere. That should solve it.
    – manjit5190
    Feb 17 '16 at 9:11
0

Thanks Guys! I got it!

get
        {   terrid = [select UserId, TerritoryID from Userterritory where UserID=: UserInfo.getUserId()].TerritoryID;
            ParentTerr = [select Parentterritoryid,ID from territory where ID=: terrid].Parentterritoryid;
            List<territory> territoryList= [select Parentterritoryid,ID from territory where ID=: ParentTerr ];
                if(territoryList!=null && !territoryList.isEmpty()){
                     PterrId = territoryList.get(0).Parentterritoryid;
                }

            List<userterritory> DMUser= [select UserId,territoryid from userterritory where territoryid =: ParentTerr];
              if(DMUser!=null && !DMUser.isEmpty()){
                     DM = DMUser.get(0).UserId;
                }
           // DM = [select UserId,territoryid from userterritory where territoryid =: ParentTerr ].UserId;
            if(PterrId!=null)
            RM = [select UserId,territoryid from userterritory where territoryid =: PTerrId ].UserId;


            UserEmail=[select email from user where id = :UserInfo.getUserId()].Email;

         //   UserTemp = [Select u.LastName, u.Id, u.FirstName, u.Email From User u limit 1];

            UserList = new List<SelectOption>();

            for(User temp : UserTemp)
            {
                UserList.add(new SelectOption(temp.Id, temp.Email));

            }
            return UserList;
        }
        set;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.