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
padmini sspadmini ss 

in test class getting List has no rows for assignment to SObject

My Apex code:
public with sharing class RouteOutlet_v3{

public Integer totalCount {get;set;}
public Integer rowIndex {get;set;}
public Integer selectedDay { get; set; }
public Integer Weeklyoff{ get; set; }
public list<String> strLocation{get;set;}
public Route_Outlet__c del;
public list<Route_Outlet__c> listRouteOutlet { get; set; }
Route_Outlet__c rou= new Route_Outlet__c();  
public Route__c route{get;set;}
public list<OutletWrapper> outletWrapperList{get;set;}
public RouteOutlet_v3(ApexPages.StandardController controller) {
        route=new Route__c ();
        strLocation=new list<String>();
        outletWrapperList=new list<OutletWrapper>();
        addRouteoutlet();
}
     
public PageReference saveoutlet1(){
    if(route!=null){
       try{ 
             system.debug('&&&&&&&   '+route);
            upsert route;
            List<Route_Outlet__c> listRouteOutlet=new list<Route_Outlet__c>();
                      
            if(outletWrapperList.size()>0){
                 for(OutletWrapper olwIterator: outletWrapperList){
                   if(olwIterator.routeOutlet!=null){
                       if(olwIterator.routeOutlet.Account__c!=null){
                         Route_Outlet__c routeiterator=olwIterator.routeOutlet;
                         routeiterator.Area__c=olwIterator.outletArea;
                         routeiterator.Contact_Person__c=olwIterator.contactPerson;
                         routeiterator.Country__c=olwIterator.outletCountry;
                          routeiterator.City__c=olwIterator.outletCity;
                         routeiterator.Date__c=date.today();
                         routeiterator.Mobile_Number__c=olwIterator.outletPhone;
                         routeiterator.Pin_Code__c=olwIterator.outletPinCode;
                         routeiterator.Route__c=route.id;
                         routeiterator.State__c=olwIterator.outletState;
                         listRouteOutlet.add(routeiterator);
                       }
                   }
                 
                 }
                }
                if(listRouteOutlet.size()>0){
                         
                        system.debug('+_+_+_+_+   '+listRouteOutlet);
                        insert listRouteOutlet;
                         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Confirm,'Record Saved Successfull');
                         ApexPages.addMessage(myMsg);
                 }
                 
            }
            catch (Exception ex){
               // ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Error,'Error Occurred : '+ex);
              //  ApexPages.addMessage(myMsg);
            }
        }
    else{
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Error,'No appropriate Route found.');
        ApexPages.addMessage(myMsg);
    }
    
    return null;
} 
public void ClearAll(){
    if(listRouteOutlet!=null)
        listRouteOutlet.clear();
        route.Name='';
        route.Date__c=null;
        route.City__c=null;
        selectedDay=null;
        Weeklyoff=null;                       
        outletWrapperList.clear();
        ApexPages.getMessages().clear();
        route=new Route__c ();
  
}
public void fetchMapLocation(){
       strLocation=new list<String>();
       if(outletWrapperList.size()>0){
           for(OutletWrapper olwIterator: outletWrapperList){
           if(olwIterator.routeOutlet!=null){
               if(olwIterator.routeOutlet.Account__c!=null){
                     //strLocation.add(olwIterator.outletArea);
                  strLocation.add(olwIterator.outletCity);
               }
           }
         }
       }
}
public void addPopulatedAccounttoRouteOutlet(){
        rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
        OutletWrapper olwWrap=outletWrapperList.get(rowIndex);
        Account acc=[Select id,BillingStreet,BillingCity,BillingState,BillingCountry,BillingPostalCode,Phone from Account where Id=: olwWrap.routeOutlet.Account__c];
        olwWrap.outletArea=acc.BillingStreet;
         olwWrap.outletCity=acc.BillingCity;
        olwWrap.outletState=acc.BillingState;
        olwWrap.outletCountry=acc.BillingCountry;
        olwWrap.outletPinCode=acc.BillingPostalCode;
        olwWrap.outletPhone=acc.Phone;
        fetchMapLocation();
}
public void addRouteoutlet(){
    Route_Outlet__c rOutlet=new Route_Outlet__c ();
    OutletWrapper olw=new OutletWrapper();
    olw.routeOutlet=rOutlet;
    outletWrapperList.add(olw);
}

public void deleteRouteoutlet(){
    rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
    outletWrapperList.remove(rowIndex);
    fetchMapLocation();
}

public class OutletWrapper{
 public Route_Outlet__c routeOutlet{get;set;}
 public string outletArea{get;set;}
 public string contactPerson{get;set;}
 public string outletCountry{get;set;}
 public string outletCity{get;set;}
 public string outletState{get;set;}
 public string outletPinCode{get;set;}
 public string outletPhone{get;set;}
 public OutletWrapper(){}
 public OutletWrapper(Route_Outlet__c routeOutlet,string outletArea,string contactPerson,string outletCountry,string outletCity,string outletState,string outletPinCode,string outletPhone){
   this.routeOutlet=routeOutlet;
   this.outletArea=outletArea;
   this.outletCity=outletCity;
   this.contactPerson=contactPerson;
   this.outletCountry=outletCountry;
   this.outletState=outletState;
   this.outletPinCode=outletPinCode;
   this.outletPhone=outletPhone;
 }
}         
}
My Test class:
@isTest
private class TestRouteOutlet_v3 {
    static testMethod void runTestRoutes() {
        Account a = new Account(
            Name = 'Test Account ',
            BillingStreet = 'Test Street ',
            BillingCity = 'Test City ',
            BillingCountry = 'Test Country '
        );
        insert a;

        City__c c = new City__c(Name = 'Test City ' );
        insert  c;

        Route__c rou = new Route__c(
            Name = 'Test route ',
            Date__c = System.today(),
            City__c =c.Id
        );
        insert rou;

        Route_Outlet__c routeOutlet = new Route_Outlet__c(
            Account__c = a.Id,
            Route__c = rou.Id
        );
        insert routeOutlet ;

        Integer index = 0;

        PageReference reference = Page.RouteOutlet_v3;
        reference.getParameters().put('rowIndex', String.valueOf(index));
        Test.setCurrentPage(reference);

        ApexPages.StandardController sc = new ApexPages.StandardController(rou);
        RouteOutlet_v3 rotout = new RouteOutlet_v3(sc);

        rotout.addRouteoutlet();
        rotout.addPopulatedAccounttoRouteOutlet();
        rotout.deleteRouteoutlet();
        rotout.saveoutlet1();
    }
}

I am geting  Error, in this Function/line:
public void addPopulatedAccounttoRouteOutlet(){
        rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
        OutletWrapper olwWrap=outletWrapperList.get(rowIndex);
        Account acc=[Select id,BillingStreet,BillingCity,BillingState,BillingCountry,BillingPostalCode,Phone from Account where Id=: olwWrap.routeOutlet.Account__c];
        olwWrap.outletArea=acc.BillingStreet;
         olwWrap.outletCity=acc.BillingCity;
        olwWrap.outletState=acc.BillingState;
        olwWrap.outletCountry=acc.BillingCountry;
        olwWrap.outletPinCode=acc.BillingPostalCode;
        olwWrap.outletPhone=acc.Phone;
        fetchMapLocation();
}
The Error is:
System.QueryException: List has no rows for assignment to SObject
Class.RouteOutlet_v3.addPopulatedAccounttoRouteOutlet:
pconpcon
The issue is that your outletWrapperList is not getting populated quite right.  On line 108 you are just assigning a empty instance of Route_Outlet__c you should be generating all of these lists in your contstructor based on the id that is passed in.  This feels way over complicated for what you are trying to do.
jp1234jp1234
If I am not mistaken, your controller and addRouteoutlet() method just inserts empty Route_Outlet__c record to the olwWrap list, and not actually grabbing the existing Route_Outlet__c record.  Hence, Route_Outlet__c record associated with your olwWrap instance has null as Account__c, which is causing your Account SOQL query to return empty result.
padmini sspadmini ss
Hi Pcon, Basically, this is a Page , To Accept/insert new Routes and Route outlet records.
In the Pageblock table,
In the first column, am populating the account name(lookup), based on that writing the query, and assiging to wrapper class,
After that finally when Add button is pressed..then i am inserting , line no 168.
so, In this situation how can i pass values in the constructor itself...can u pls suggest me. Pcon..?
padmini sspadmini ss
Then how to over come this situation ..?? Pcon & Jp1234
jp1234jp1234
Instead of calling addRouteoutlet() method in the controller, you should try to populate your olwWrap list with records associated with standard controller record.
 
jp1234jp1234
Again, it's not a problem with addRouteoutlet() per se.  Your controller shouldn't call that method.  Instead, it should try to populate that list by grabbing the pre-existing Route_Outlet__c record.
padmini sspadmini ss
can u pls give the sample jp1234
jp1234jp1234
Since you are passing in a Route_Outlet__c record as a standard controller object (although, I'm not sure you should), replacing 
addRouteoutlet() with following should work.
OutletWrapper olw=new OutletWrapper();
olw.routeOutlet= (Route_Outlet__c)controller.getRecord();
outletWrapperList.add(olw);


 
padmini sspadmini ss
std controller is Route__c not Route__outlet__c , jp1234
jp1234jp1234
for (Route_Outelet__c outlet : 
      [SELECT Id, Account__c, ...
                      FROM Route_Outlet__c 
                      WHERE Route__c = :controller.getId()]) {
  OutletWrapper olw = new OutletWrapper();
  olw.routeOutlet = outlet;
  outletWrapperList.add(olw);
}
In that case, following should work. Replace ellipsis in SOQL query with whatever other fields that are required.
padmini sspadmini ss
at which line in Apex code shall i modify it pls tell me?
jp1234jp1234
Line 18.  I see why my comment would have been confusing there.  I meant to say that you should replace the call to addRouteOutlet() method in your constructor with above code, not your controller :)
jp1234jp1234
Btw, if this has been fixed, you should mark the reply that work for you as a best answer as that will help people with similar issue :)
padmini sspadmini ss
i fixed the issue in the testclass not in the Apex code/controller. Thank you Jp1234 & pcon fro supporting me