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
chandu kumarchandu kumar 

unit test code for pagereference method

Hi,
I need a test code for this pagereference method ..its with for loops and if conditions..so help me to findout this.
Thanks in advance.
public PageReference Del (){
        Integer dmlrows = Limits.getLimitDMLRows();
        Integer curdmlrows = Limits.getDMLRows(); 
        Integer remainingdmlrows = dmlrows - curdmlrows;
        
        Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('index'));  
        
        if (newRFPProdList.get(indexVal - 1).id!=null)
            delRFPProdList.add(newRFPProdList.get(indexVal - 1));
        
        Id prodid = newRFPProdList.get(indexVal - 1).Product__c;  
        system.debug('selectedprodid'+prodid);
        try{
        delete [select ID from rfp_question__c where rfp__c = :rfpID and product__c =: prodid];
        remainingdmlrows = dmlrows - curdmlrows;
        
        newprd.remove(indexVal - 1);
        newRFPProdList.remove(indexVal - 1);
        
        if(delRFPProdList.size()>0)
            delete delRFPProdList;
        delRFPProdList.clear();
        }
        catch (Exception e) {
          String  message  = '[ PACRES ] Exception: ' + e.getMessage() + '; line: ' + e.getLineNumber() ;
            //ApexPages.addMessage(message);
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, '[ PACRES ] Exception: ' + e.getMessage() ));
            exceptionMessages += message + '\n\n';
        }
        return null;
    }
Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi Chandu,
 
Try to optimize your code and move the DML out of loop.
 
For Test coverage write a separate method and execute whole code for 1 or 2 records. It wil l increase code coverage.
 
 
chandu kumarchandu kumar
Hi ankit,
i am tried but but no use.
i am new to this.canyou please give a sample test code for those?
sandeep reddy 37sandeep reddy 37
---->apex class
public PageReference linkIsClickedAction(){
PageReference redirectPage = new PageReference('/apex/StandardCampaign'); redirectPage.getParameters().put(ApexPages.currentPage().getParameters().get('id'),'recordId');
redirectPage.setRedirect(true);
return redirectPage; }
------>>test class


static testmethod testMyMethod(){
campaignmanageclass obj=new campaignmanageclass();
PageReference pageRef = obj.linkIsClickedAction();
//Beginning of System Asserts, verify pageRef and URL First
System.assertNotEquals(null,pageRef);
System.assertEquals('/apex/StandardCampaign',pageRef.getUrl());
//Verify the pageRef parameters as well Map<String,String> pageParameters = pageRef.getParameters(); System.assertEquals(1,pageParameters.values().size());
System.assertEquals('recordId',pageParameters.get('id')); }
sweta singh 58sweta singh 58
Hi Chandu,

Were you able to cover -  Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('index'));  
in test class?

I am unable to cover it .Throws error:-Method does not exist or incorrect signature: void put(String, Integer) from the type Map<String,String> in PageRef.getparameters()
Test Class -
opportunityassetcontroller.wrapperSelectedOli a2 = new opportunityassetcontroller.wrapperSelectedOli(true,oli);
            PageReference pageRef = Page.OpportunityAssetControllerNewPage;
               PageRef.getParameters().put('id',oli.Id);
            Test.setCurrentPage(pageRef);
            ApexPages.StandardController cntrl  = new ApexPages.StandardController(oli);
            opportunityassetcontroller OAC = new opportunityassetcontroller(cntrl);
            integer indexval = Integer.valueof(system.currentpagereference().getparameters().get('index'));
            //PageRef.getparameters().put('index',indexval);

Controller-
public void selectOli(){
        integer indexval = Integer.valueof(system.currentpagereference().getparameters().get('index'));
        /* Get the ID of the selected OLI*/    
        String seloliid = System.currentPagereference().getParameters().get('Olid');
        system.debug('@@@seloliid'+seloliid);
        system.debug('@@@indexval'+indexval);
        system.debug('@@insidemapSelectedOli'+mapSelectedOli);
        system.debug('@@insideoliid'+oliid);
        
        olirecordselected = mapSelectedOli.get(seloliid);
        system.debug('@@@olirecordselected'+olirecordselected);
        system.debug('@@wrapperSelectedOli.size()'+wrapperSelectedOli.size());
        for(integer i=0; i < wrapperSelectedOli.size(); i++){
            if(i == indexVal - 1)
            wrapperSelectedOli[i].isSelected = true;
        else
            wrapperSelectedOli[i].isSelected = false;
    }}

Thanks,
Sweta