+ Start a Discussion
Puja ChoudharyPuja Choudhary 

System Exception: List has no rows for assignment to SObject

Hi, 
I am writing a test class for my ctr_searchController

public with sharing class ctr_searchcontroller {

    Public Menu__c menu { get; set;}
    Public String mid {get;set;}
    Public String cid { get; set;}
    public String lang { get; set; }
    Public String tout { get; set;}
    Public String tablen{get;set;}
    Public Contact c { get; set;}
    Public String cmessage { get; set;}
     Public Room__c room {get;set;}
     Public id existingorderid { get; set;}    
    // Public String Closelaterorderid{get; set;}
     // Public Boolean closelater{get; set;}
      Public List<Order__c> order { get; set;}
      
     public ctr_searchcontroller(){
        lang = ApexPages.currentPage().getParameters().get('lang');
        mid = ApexPages.currentPage().getParameters().get('mid');
        cid = ApexPages.currentPage().getParameters().get('cid');
           cmessage = Label.Order_Submissions;
        existingorderid = null;
       room = new Room__c();
        tout = System.currentPageReference().getParameters().get('tout');
        tablen = ApexPages.currentPage().getParameters().get('tablen');
         if(cid!=null & cid!=''){
        c = [select id,name,Last_Login_Reff_Id__c,Mobilephone from contact where id=:cid limit 1];
        //order = [select id, Name,status__c from Order__c where Customer__c=:c.id and Session_id__c=:c.Last_Login_Reff_Id__c limit 1];
         order = [select id, Name,status__c from Order__c where Customer__c=:c.id and Tables__c=:tablen and Status__c='In Progress'  limit 1];
        if(order.size()>0)
                existingorderid = order[0].id;
            else
                existingorderid = null;
            
        }
        else{
            pagereference ref = new pagereference('/apex/vfp_firstpage');
            ref.setredirect(true);
        } 
         menu = [select id, name,Category__c,Description__c,Dish_Name__c,ImageURL__c,
        Price__c,Special_Dish__c,Status__c from Menu__c where id=:mid];
        
  }
}


My test class is:

@isTest
public class ctr_searchControllerTest {
    @isTest
    static void searchController(){
    
       
       // Menu__c menu = new Menu__c();
        
         
        ctr_searchController search = new ctr_searchController();
        Contact c = new Contact();
        ApexPages.currentPage().getParameters().put('cid', c.Id);
        search.menu = new Menu__c(Description__c='this is sample');
        insert search.menu;

        search.lang ='eng';
        search.tablen='1';
        search.tout ='out';
        search.order = new List<Order__c>();
        search.c = new contact(lastname='ram');
        search.room = new room__c();
       // search.c = [select id,name,Last_Login_Reff_Id__c,Mobilephone from contact where id=:'cid' limit 1];
        
        
        //search.closelater = false;
        //search.Closelaterorderid ='55888888';
        search.existingorderid ='85255558888';
      //  search.existingorderid ='';        
    }
}


I m getting the error System Exception: List has no rows for assignment to SObject
 
Varun SinghVarun Singh
you  have error in this line
search.c = [select id,name,Last_Login_Reff_Id__c,Mobilephone from contact where id=:'cid' limit 1];

 first you have to insert contact suceesfully then  you can query from cid right  now record is not created  so cid is null(there is no contact id)
 
Varun SinghVarun Singh
Check this line also
ApexPages.currentPage().getParameters().put('cid', c.Id);
Correct it

 Contact c = new Contact();
c.lastname='test';
insert c;
ApexPages.currentPage().getParameters().put('cid', c.Id);
 
Amit Chaudhary 8Amit Chaudhary 8
You need to add custom data in your test class.

Try to update your code like below
@isTest
public class ctr_searchControllerTest {
    @isTest
    static void searchController()
	{
		Account acc = new Account();
		acc.Name ='Test';
		// add all required field
		insert acc;
        
		contact  cont = new contact();
		cont.LastName ='Test';
		cont.FirstName ='Test';
		cont.accountid = acc.id;
		insert cont;
		
		Menu__c menuObj = new Menu__c();
		menuObj.Description__c='this is sample'
		// Add all required field here
		insert menuObj;
		
		Order__c ordObj = new Order__c();
		ordObj.Status__c='In Progress';
		// Add all required field here
		insert ordObj;
         
        ctr_searchController search = new ctr_searchController();
        ApexPages.currentPage().getParameters().put('cid', cont.Id);
        ApexPages.currentPage().getParameters().put('lang', 'English');
        ApexPages.currentPage().getParameters().put('mid', menuObj.id);
        ApexPages.currentPage().getParameters().put('tout', 'out');
        ApexPages.currentPage().getParameters().put('tablen', '1');


    }
}

Let us know if this will help you