You need to sign in to do that
Don't have an account?
orawan_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; } }
Hi,
Please define the list objpro as below and try.
List<CMPD_Projects__c> objPro = new List<CMPD_Projects__c>();
Thanks,
Aravind
Hi AravindBabu512
I Try to Do somthing like this but is still wrong if I derstand something long please instruct me.
Thanks
Execute the query in try catch block..
Thank you I try to do something like below this is not error , but not have data return.
Thanks,
Apple
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.
This doesn't make sense does it ?, should chek if it's null, if the size()=0 the for won't iterate anyway.
wrap your queries in getters which handle no-rows.
for example:
also, as a note, getters should never return void, they should return whatever you're getting.
Hi BryanHart,
I try to do something like this and still to return empty list :
Thanks,
Apple
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?