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
PCPC 

Soql query is showing that list has no rows for assignment

Hi,
I have a custom object calle OLI__c
I m trying to fetch  the order line items from OLI__c  that are repeated against a particular customer in the Order__c Object.

But I am getting the error 
List has no rows for assignment to SObject 
An unexpected error has occurred. Your development organization has been notified.

I have tested the soql query in developer console and it is giving the desired result.
favourites = [select Menu__r.id menuid from OLI__c  where Order__c in(SELECT id FROM Order__c where Customer__c=:c.id and Status__c='Completed') group By Order__r.Customer__r.name,Menu__r.Dish_Name__c,Menu__r.id having count(id)>1 ];
          
 for(AggregateResult ar : favourites){
                 
                 mid = String.Valueof(ar.get('menuid'));
                
            }

            if(mid!='')    
                favmenu = [select id, name,Category__c,Description__c,Image__c,ImageURL__c,Dish_Name__c,Price__c,
                 Special_Dish__c,Status__c,SubCategory__c,veg_non_veg__c,remarks__c from Menu__c where id=:mid];

 
Best Answer chosen by PC
PawanKumarPawanKumar
You try below .

favourites = [select Menu__r.id menuid from OLI__c  where Order__c in(SELECT id FROM Order__c where Customer__c=:c.id and Status__c='Completed') group By Order__r.Customer__r.name,Menu__r.Dish_Name__c,Menu__r.id having count(id)>1 ];
          
          List<String> menuIdList = new List<String>();
 for(AggregateResult ar : favourites){
                 
                 menuIdList.add(String.Valueof(ar.get('menuid')));
                
            }

            if(menuIdList!=null && !menuIdList.isEmpty())    
                favmenu = [select id, name,Category__c,Description__c,Image__c,ImageURL__c,Dish_Name__c,Price__c,
                 Special_Dish__c,Status__c,SubCategory__c,veg_non_veg__c,remarks__c from Menu__c where id IN : menuIdList];

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Puja Choudhary, Hope it will be helpful.
  • Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.
Thanks
Rahul Kumar
PCPC
I checked in the developer console....The query is returning the rows.
PawanKumarPawanKumar
Just convert  your favmenu declaration  as below and try.
List<Menu__c > favmenu
PCPC
Hi pawan,
I have made the change List<Menu__c > favmenu as suggested by you.
now i have noticed that when i am making the query as 

 Public Id mid {get;set;} //  I think there is a problem in the declaration of mid....It is not able to hold a list of ids.
 Public List<Menu__c> favmenu {get;set;}

// if(mid!='')  //commenting this line  and in the below query adding limit 1, I m able to fetch the desired info for first menu item
favmenu = [select id, name,Category__c,Description__c,Image__c,ImageURL__c,Dish_Name__c,Price__c, Special_Dish__c,Status__c,SubCategory__c,veg_non_veg__c,remarks__c from Menu__c where id=:mid limit 1];

CAN YOU PLEASE SUGGEST SOMETHING ON THIS.
PawanKumarPawanKumar
You try below .

favourites = [select Menu__r.id menuid from OLI__c  where Order__c in(SELECT id FROM Order__c where Customer__c=:c.id and Status__c='Completed') group By Order__r.Customer__r.name,Menu__r.Dish_Name__c,Menu__r.id having count(id)>1 ];
          
          List<String> menuIdList = new List<String>();
 for(AggregateResult ar : favourites){
                 
                 menuIdList.add(String.Valueof(ar.get('menuid')));
                
            }

            if(menuIdList!=null && !menuIdList.isEmpty())    
                favmenu = [select id, name,Category__c,Description__c,Image__c,ImageURL__c,Dish_Name__c,Price__c,
                 Special_Dish__c,Status__c,SubCategory__c,veg_non_veg__c,remarks__c from Menu__c where id IN : menuIdList];
This was selected as the best answer
PawanKumarPawanKumar
is above useful?
PCPC
Thank you Pawan for the help....Now its working fine.
PCPC
Can you tell me how i can select distinct records in soql query.