+ Start a Discussion
orawan_CMPDorawan_CMPD 

List has no rows for assignment to SObject

I am wondering if there is a clever way to avoid getting an error message when a query returns no results.  At the moment it throws a

 

List has no rows for assignment to SObject


This is my code:

 

public class CMPDProjectList {

    public List<CMPD_Projects__c> lstPro{get;set;}
    public List<CMPD_Projects__Share> lstProShare{get;set;}
    public List<Group> lstGroup{get;set;}
    
    public CMPDProjectList()
    {
        getGroup();
        getShare();  
        lstPro=getProlist();    
    }
    
    
     public void getGroup()
    {
        lstGroup = [Select    Group.Id,
Group.Type, Group.RelatedId From Group Where Group.RelatedId = :Userinfo.getUserRoleId() and Group.Type = 'Role']; } public void getShare() { for(Group g:lstGroup) { lstProShare = [ Select CMPD_Projects__Share.Id, CMPD_Projects__Share.ParentId, CMPD_Projects__Share.UserOrGroupId, CMPD_Projects__Share.RowCause From CMPD_Projects__Share Where CMPD_Projects__Share.UserOrGroupId = :g.Id or CMPD_Projects__Share.UserOrGroupId = :Userinfo.getUserId()]; } } public List<CMPD_Projects__c> getProlist() { CMPD_Projects__c objPro = new CMPD_Projects__c(); List<CMPD_Projects__c> lstPro = new List<CMPD_Projects__c>();
if(lstProShare.size() > 0) { for(CMPD_Projects__Share l:lstProShare) { objPro = [Select CMPD_Projects__c.Id, CMPD_Projects__c.Name, CMPD_Projects__c.Status__c, CMPD_Projects__c.Department__c, CMPD_Projects__c.Description__c, CMPD_Projects__c.OwnerId From CMPD_Projects__c Where CMPD_Projects__c.Id = : l.ParentId And CMPD_Projects__c.Status__c = 'In Progress']; lstPro.add(objPro); } } return lstPro; } }

 

AravindBabu512AravindBabu512

Hi,

 

Please define the list objpro as below and try.

 

    List<CMPD_Projects__c> objPro = new List<CMPD_Projects__c>();

 

 

Thanks,

Aravind

orawan_CMPDorawan_CMPD
public List<CMPD_Projects__c> getProlist()
  {
    List<CMPD_Projects__c> objPro = new List<CMPD_Projects__c>();
    List<CMPD_Projects__c> lstPro = new List<CMPD_Projects__c>();
         if(lstProShare.size() > 0)
         {
           for(CMPD_Projects__Share l:lstProShare)
          {      
			objPro = [Select    CMPD_Projects__c.Id,
					    CMPD_Projects__c.Name,
					    CMPD_Projects__c.Status__c,
					    CMPD_Projects__c.Department__c,
					    CMPD_Projects__c.Description__c,
					    CMPD_Projects__c.OwnerId
                                  From	    CMPD_Projects__c
                                  Where	    CMPD_Projects__c.Id = : l.ParentId
                                  And	    CMPD_Projects__c.Status__c = 'In Progress'];
            
                     
        	lstPro.add(objPro);
          
          }         
         }    
       return lstPro;  
  }  

 Hi AravindBabu512

I Try to Do somthing like this but is still wrong if I derstand something long please instruct me.

 

Thanks

priyanka.mv26priyanka.mv26

Execute the query in try catch block..

orawan_CMPDorawan_CMPD

Thank you I try  to do something like below this is not error , but not have data return.

 

public List<CMPD_Projects__c> getProlist()
{
try
{
    CMPD_Projects__c objPro = new CMPD_Projects__c();
    List<CMPD_Projects__c> lstPro = new List<CMPD_Projects__c>();
    
     if(lstProShare.size() > 0)
     {
       for(CMPD_Projects__Share l:lstProShare)
      {      
		objPro = [Select    CMPD_Projects__c.Id,
				    CMPD_Projects__c.Name,
				    CMPD_Projects__c.Status__c,
				    CMPD_Projects__c.Department__c,
				    CMPD_Projects__c.Description__c,
				    CMPD_Projects__c.OwnerId
                          From	    CMPD_Projects__c
                          Where	    CMPD_Projects__c.Id = : l.ParentId
                          And	    CMPD_Projects__c.Status__c = 'In Progress'];
        
                 
    	lstPro.add(objPro);      
      }         
     } 
    }
    catch(Exception ex)
    {
	system.Debug('Error <CMPDProjectList>' + ex.getMessage());	
    } 
    return lstPro;       
  }  
  

 Thanks,

Apple

SamuelDeRyckeSamuelDeRycke

 

Hmm, SOQL Select should just return an empty list if no results were found I think. Which exact line is giving you that error ? Though besides that, you need to do some other code changes anyway.

 

never for soql in a for loop (that goes for sql queries on other systems too .. ). Where you may just have bad performance in other systems, you can hit governor limits in salesforce, nasty exceptions which you want to avoid. But, that's a good thing, you'll learn how to code more effectivly.

 

if(lstProShare.size() > 0)
 {
     for(CMPD_Projects__Share l:lstProShare){      
		//some stuff
                lstPro.add(objPro);
     }
}

 This doesn't make sense does it ?, should chek if it's null, if the size()=0 the for won't iterate anyway.

BryanHartBryanHart

 

wrap your queries in getters which handle no-rows.

 

for example:

private CMPD_Projects__Share getShareForGroup(Group groupToMatch){
    List<CMPD_Projects__Share> shares =
                      [ Select  CMPD_Projects__Share.Id,
                                CMPD_Projects__Share.ParentId,
                                CMPD_Projects__Share.UserOrGroupId,
                                CMPD_Projects__Share.RowCause
                        From    CMPD_Projects__Share
                        Where   CMPD_Projects__Share.UserOrGroupId = :group.Id
                        or      CMPD_Projects__Share.UserOrGroupId = :Userinfo.getUserId()];

    if(shares.size() == 1)
        return shares[0];
    return null;
}

 

also, as a note, getters should never return void, they should return whatever you're getting.

orawan_CMPDorawan_CMPD

Hi BryanHart,

 

I try to do something like this and still to return empty list :


public class CMPDProjectList {

  public List<CMPD_Projects__c> lstPro{get;set;}
  private CMPD_Projects__Share shares{get;set;}
  private Group groups{get;set;}
	
	
	private Group groupToMatch()
    {
    	List<Group> groups = [	Select	Group.Id,
			    		Group.Type,
			    		Group.RelatedId
			    	From	Group
			    	Where	Group.RelatedId = :Userinfo.getUserRoleId() 
			    	and	Group.Type = 'Role'];	
    	if(groups.size() == 1) 
	return groups[0]; 
	return null;	
    }
	
    
	private CMPD_Projects__Share getShareForGroup(Group groupToMatch)
	{ List<CMPD_Projects__Share> shares = [ Select 	CMPD_Projects__Share.Id, 												CMPD_Projects__Share.ParentId, 
							CMPD_Projects__Share.UserOrGroupId, 
							CMPD_Projects__Share.RowCause
						From 	CMPD_Projects__Share 
						Where 	CMPD_Projects__Share.UserOrGroupId = :groupToMatch.Id
						or	CMPD_Projects__Share.UserOrGroupId = :Userinfo.getUserId()]; 
		if(shares.size() == 1) 
		return shares[0]; 
		return null;    
	}
    
 
   public List<CMPD_Projects__c> getProlist()
  {
  	
    CMPD_Projects__c objPro = new CMPD_Projects__c();
    List<CMPD_Projects__c> lstPro = new List<CMPD_Projects__c>();
    
     if(shares != null)
     {     
		objPro = [Select    CMPD_Projects__c.Id,
				CMPD_Projects__c.Name,
					CMPD_Projects__c.Status__c,
					CMPD_Projects__c.Department__c,
					CMPD_Projects__c.Description__c,
					CMPD_Projects__c.OwnerId
               From		CMPD_Projects__c
              Where		CMPD_Projects__c.Id = : shares.ParentId
                And		CMPD_Projects__c.Status__c = 'In Progress'];
        
                 
    	lstPro.add(objPro);      
       
     } 
    
    return lstPro;       
  }  
  
  
}

 Thanks,

Apple


BryanHartBryanHart

You may get empty lists, I'm just saying that you have to handle them.

Apex lets you assign a list to a single object IF AND ONLY IF that list has size = 1, otherwise it will error.

 

also you dont seem to be calling groupToMatch or getSharForGroup anywhere in your code, so shares is never being set.

However your getter (getShareForGroup) should not be setting anything (shares). It makes the code difficult to understand, because people won't see where shares is getting set.

 

If that doesn't fix it, I'll need more info... what list is empty and what is the stack trace?