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 

Passing PARAM to Apex method from a test class

HI 
My Vf code:
</apex:pageblock> 
    <apex:pageBlock title="New Route outlet" id="pb">
         <apex:variable var="rowNumber" value="{!0}"/>
         <apex:pageblockSection columns="1">
                 <apex:pageBlockTable title="Account" var="lro" value="{!outletWrapperList}"> 
                        <apex:column headerValue="No." style="width:20px; text-align:center;" headerClass="centertext">
                                 <apex:outputText value="{0}" style="text-align:center;"> 
                                         <apex:param value="{!rowNumber+1}" /> 
                                 </apex:outputText>
                         <apex:facet name="footer">
                                 <apex:commandlink action="{!addRouteoutlet}" value="Add Row" reRender="pb"/>
                         </apex:facet>
                        </apex:column> 
                        <apex:column headerValue="Action" >
                                 <apex:commandLink value="Delete" action="{!deleteRouteoutlet}" reRender="pb,mapPanel,map_canvas" oncomplete="showAddress();">
                                         <apex:param name="rowIndex" value="{!rowNumber}"/>
                                 </apex:commandlink>
                                 <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
                         </apex:column>   
                         
                            <apex:column headerValue="Account Name">
                                 <apex:inputField value="{!lro.routeOutlet.Account__c}">
                                 <apex:actionSupport event="onchange" action="{!addPopulatedAccounttoRouteOutlet}"  reRender="pb,mapPanel,map_canvas" oncomplete="showAddress();">
                                    <apex:param name="rowIndex" value="{!rowNumber-1}"/>
                                 </apex:actionSupport> 
                                 </apex:inputField>   
                            </apex:column>   
                         :
                         :

Apex Code:
public void addPopulatedAccounttoRouteOutlet(){
        rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
      :
      :
        
}
In Test class:

ApexPages.StandardController sc=new ApexPages.StandardController(rou);
  rotout.addRouteoutlet();
rotout.addPopulatedAccounttoRouteOutlet(); 

& i am getting Error:
System.NullPointerException: Argument cannot be null.
Stack Trace Class.RouteOutlet_v3.addPopulatedAccounttoRouteOutlet:

Geeting Error in this line.
 rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));

Pls do help me to complete the test class

Best Answer chosen by padmini ss
pconpcon
You also need to set the current page.  I typically do all of this before defining my controller
@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 roulet = new Route_Outlet__c(
            Account__c = a.Id,
            Route__c = rou.Id
        );
        insert roulet ;

        Integer index = 5;

        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();
    }
}

All Answers

pconpcon
Prior to calling the addPopulatedAccounToRouteOutlet you need to do the following:
 
Integer index = 5;

PageReference reference = Page.MyVisualforcePage;
reference.getParameters().put('rowIndex', index);

// Rest of test
pconpcon
I'm sorry, I forgot that getParameters is a string.  You can do:
 
Integer index = 5;

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

that will convert the index to a string and add it to the parameters list.
pconpcon
Can you please include your entire test method? (please use the "Add a code sample" button to increase readability)
padmini sspadmini ss
Hi Pcon, I changed to,
Integer index = 2;
PageReference reference = Page.RouteOutlet_v3;
reference.getParameters().put('rowIndex', index);


This is the Errro Msg:
 Incompatible value type Integer for MAP<String,String> at line.
This is the line:
reference.getParameters().put('rowIndex', index);
 
padmini sspadmini ss


Hi Pcon,
i changed the code...
Integer index = 5;

PageReference reference = Page.RouteOutlet_v3;
reference.getParameters().put('rowIndex', String.valueOf(index));    
      rotout.addPopulatedAccounttoRouteOutlet(); 
Now again am getting the Same error:

System.NullPointerException: Argument cannot be null.
@ the function call, 
public void addPopulatedAccounttoRouteOutlet(){
        rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
      :
      :
        
}
 rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));

padmini sspadmini ss
This is my code, not at completed just started to write,...

@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 roulet = new Route_Outlet__c (Account__c=a.ID,Route__c =rou.id);
               insert roulet ;
               
            
      ApexPages.StandardController sc=new ApexPages.StandardController(rou);
      RouteOutlet_v3 rotout = new RouteOutlet_v3(sc);
     

Integer index = 5;

PageReference reference = Page.RouteOutlet_v3;
reference.getParameters().put('rowIndex', String.valueOf(index));
      rotout.addRouteoutlet();
      //rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
      //rotout.addRouteoutlet();
      rotout.addPopulatedAccounttoRouteOutlet(); 
      rotout.deleteRouteoutlet();
      rotout.saveoutlet1();
       
      
      
    }
    
}
pconpcon
You also need to set the current page.  I typically do all of this before defining my controller
@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 roulet = new Route_Outlet__c(
            Account__c = a.Id,
            Route__c = rou.Id
        );
        insert roulet ;

        Integer index = 5;

        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();
    }
}
This was selected as the best answer
padmini sspadmini ss

This code worked for me , thank u Pcon,
Can u pls help me out here,
Getting error in this line.
 Account acc=[Select id,BillingStreet,BillingCity,BillingState,BillingCountry,BillingPostalCode,Phone from Account where Id=: olwWrap.routeOutlet.Account__c];

Error msg:

System.QueryException: List has no rows for assignment to SObject

pconpcon
That is because you do not have an account for the Account__c.  My guess is that if you were to print a debug message out you would see that olwWrap.routeOutlet.Account__c is null.  I would look at how olwWrap is generated and see how routeOutlet is populated.  I would recommend that you ask a new question on the forum for this topic so that it is easier to follow for people show come along later.  I would include the code that generate olwWrap as well as the what you are doing when you get that message.
padmini sspadmini ss
thank u i will post in a moment it self , pocon, thank u , thank u so much