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
Meeta Khullar 5Meeta Khullar 5 

System.QueryException exception

I have a visual force page that is build to do revenue recognition using custom object invoice and start object order. The form has an Account field that the user can look up the revenue based on the account. The problem that I'm having is that when the user put in the account that has 0 order the VF page throws 'System.QueryException' exception. I'm trying to use the try catch but it is still failing. Here is the code snip, the row that is highlighted in bold and underlined is the one that is causing the issue. Please suggest how do I resolve this issue.

public with sharing class RMSController{
    // public List<Order> ord1 = new List<Order>();
    public Date startDate {get; set;}
    public Date endDate {get; set;}
    public boolean renderSearch = false;
    public string  orderNumber{get;set;}
    public string  InvoiceNo {get;set;}
    public String  domain {get;set;}
    public String accountId { get;  set; }
    public Order ord {  set; }
    
    
   public Order getOrd(){
        if(accountId!= null) 
              return [select id,account.id,account.QAD_Domain__c from order where account.id = : accountId limit 1]; 
              return null; 
   } 
Nayana KNayana K
While querying records, it is always recommended to use List to store the result even if returns only one record.

So you can alter your code like this :

public with sharing class RMSController{
    // public List<Order> ord1 = new List<Order>();
    public Date startDate {get; set;}
    public Date endDate {get; set;}
    public boolean renderSearch = false;
    public string  orderNumber{get;set;}
    public string  InvoiceNo {get;set;}
    public String  domain {get;set;}
    public String accountId { get;  set; }
    public List<Order> lstOrd {  set; }
    
    
   public List<Order> getlstOrd(){
        if(accountId!= null) 
              return [select id,account.id,account.QAD_Domain__c from order where account.id = : accountId limit 1]; 
              return null; 
   } 
   
   
While lstOrd in VF page, you can put a check whether list is empty and render accordingly. SOmething similar to this:

<Apex:outputPanel rendered = "{!lstOrd.size > 0}">
<apex:outputField = "{!lstOrd[0].account.id}"/>
<apex:outputField = "{!lstOrd[0].account.QAD_Domain__c}"/>
</Apex:outputPanel>

 
SandhyaSandhya (Salesforce Developers) 
Hi Meeta Khullar,

Try to use something like this 
List<order> od =[select id,account.id,account.QAD_Domain__c from order where account.id = : accountId limit 1];
System.debug('od'+ od);
and see what it is returning in your debug logs in developer console so that you will be able to find your bug.

Hope this helps you!

If this helps you mark it as solved.

Thanks and Regards
Sandhya

 
Meeta Khullar 5Meeta Khullar 5
THanks Nayana, I tried your solution it is still not working. I'm not very familiar with VF and Controller coding. Here is the code for my VF where we capture the account information

 <apex:pageBlockSectionItem >
                 <apex:outputLabel value="Account" for="theLookup"/>
                 <apex:inputField id="Account" value="{!ord.accountid}" label="Account">
                     <apex:actionSupport event="onchange" rendered="false">                             
                           <apex:param name="accountId" value="" assignTo="{!accountId}"/>  
                     </apex:actionSupport>
                 </apex:inputField> 
             </apex:pageBlockSectionItem> 

When I put your code in the controller I get the VisualforceArraylist.account error


public with sharing class RMSController{
    // public List<Order> ord1 = new List<Order>();
    public Date startDate {get; set;}
    public Date endDate {get; set;}
    public boolean renderSearch = false;
    public string  orderNumber{get;set;}
    public string  InvoiceNo {get;set;}
    public String  domain {get;set;}
    public String accountId { get;  set; }
   //public Order ord {  set; }
    public List<Order> Ord {  set; }
    
   /*public Order getOrd(){
        if(accountId!= null) 
              return [select id,account.id,account.QAD_Domain__c from order where account.id = : accountId limit 1]; 
              return null; 
   }*/
   
   public List<Order> getOrd(){
        if(accountId!= null)
              return [select id,account.id,account.QAD_Domain__c from order where account.id = : accountId limit 1];
              return null;
   }

Please suggest how do I fix this?

Regards,
Meeta