function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC coderSFDC coder 

list has no rows for assignment to sobject

hi all,

i have a controller where in am getting this error displayed on my VF page.
Below is my code
public String distributionIds;
    
  public List<Distribution_List_Item__c> distItemLst{get;set;}
    
    //CONSTRUCTOR
    public VistAudit_VFC()
    {
        visitId=ApexPages.currentPage().getParameters().get('id');
        relatedAccountId=ApexPages.currentPage().getParameters().get('accid');
        distItemLst=new List<Distribution_List_Item__c>();
        distributionIds='';
    }
    
    //Constructs the data displayed on the page for all sections
    public void createData()
    {
     distributionIds=[select Id from Distribution_List__c where Business_Partner__c=:relatedAccountId 
                      AND Start_Date__c < today AND End_Date__c > today AND Status__c='Active'].Id;

     distItemLst=[select id from Distribution_List_Item__c where Distribution_List__c =:distributionIds];
}

in the above code,actually no record exisits in database to return the rows.But because of this error it discards the further execution of the code.
So can anyone please suggest how do i get rid of this error when there are no records present in database?

Thanks
Best Answer chosen by SFDC coder
SS KarthickSS Karthick
Hi Neha Patil,
     The problem is you are gettting Distitmelist for that you need to use list of distribution id.
 
Just try the below code

public List<Distribution_List_Item__c> distributionIDs {get;set;}


distributionIds= [select Id from Distribution_List__c where Business_Partner__c=:relatedAccountId AND Start_Date__c < today AND End_Date__c > today AND Status__c='Active'].Id; 

distItemLst=[select id from Distribution_List_Item__c where Distribution_List__c in :distributionIds];

I hope this will helps you



Please mark this post as solved so that it benifit others. 

Thanks 
Karthick

 

All Answers

SS KarthickSS Karthick
Hey,
    Change the type of the id variable..
like 
public Distribution_List_Item__c distributionIDs;


Please mark this post as solved so that it benifit others. 

Thanks 
Karthick
 
SFDC coderSFDC coder
hey karthik,

your trick also doesnt work.. :(
SS KarthickSS Karthick
Make the soql like below
 
distItemLst=[ select id from Distribution_List_Item__c where Distribution_List__c in [select Id from Distribution_List__c where Business_Partner__c=:relatedAccountId AND Start_Date__c < today AND End_Date__c > today ANDStatus__c='Active'].Id]];


Please mark this post as solved so that it benifit others. 

Thanks 
Karthick
 
ManjunathManjunath
Hi,

You can just by pass it by puting a "Try catch"(In catch block dont put anything) block around the query. if you are sure if the future methods and this query are not related.

Regards,
Manjunath
SFDC coderSFDC coder
thanks karthi,
 Your second solution did wrok.But the scenario is that i want to use that of distribution list in multiple places.so
according to you solution,i'll have to apply the nested query every now and then.

So what is going wrong in my query?
cant i rectify it?just to save the several other nested queries as you just showed?
SFDC coderSFDC coder
hi manjunath,

I too had thought of the same solution earlier,but then it doesn't seem to be a feasible solution.
Thanks for the reply
SS KarthickSS Karthick
Hi Neha Patil,
     The problem is you are gettting Distitmelist for that you need to use list of distribution id.
 
Just try the below code

public List<Distribution_List_Item__c> distributionIDs {get;set;}


distributionIds= [select Id from Distribution_List__c where Business_Partner__c=:relatedAccountId AND Start_Date__c < today AND End_Date__c > today AND Status__c='Active'].Id; 

distItemLst=[select id from Distribution_List_Item__c where Distribution_List__c in :distributionIds];

I hope this will helps you



Please mark this post as solved so that it benifit others. 

Thanks 
Karthick

 
This was selected as the best answer
SFDC coderSFDC coder
it did solve my problem.But still i am unable to conclude on why it didn't work when i was assigning the value in a string variable.What is going wrong in that case?