+ Start a Discussion
Ashok Kumar NayakAshok Kumar Nayak 

Please suggest a test class for my controller...

public with sharing class LP_CreateNewCasePage_Controller 
{

public String Id;
public Id ParentId;
 
 public PageReference OtherRedirect(){
   
    try{
          System.debug('Selected RecordType is'+selectedrecordtype);
          
           Id recTypeId = [SELECT id from RecordType WHERE id=:selectedrecordtype].id;
           
           PageReference p = new PageReference('/' + Case.SObjectType.getDescribe().getKeyPrefix() + '/e');
           Map<String, String> m = p.getParameters();
   
                if((string)ApexPages.currentPage().getParameters().get('retURL').substring(1,4)!='500')
                {
                ParentId = ApexPages.currentPage().getParameters().get('retURL').substring(1,16);
                Id=(string)ParentId;
                if(Id.Startswith('003')){
                m.putAll(ApexPages.currentPage().getParameters());
                m.put('nooverride', '1');
                m.put('RecordType',recTypeId);                        
                p.setRedirect(true);
                    }
                if(Id.Startswith('001')){
                m.putAll(ApexPages.currentPage().getParameters());
                m.put('nooverride', '1');
                m.put('RecordType',recTypeId);                        
                p.setRedirect(true);               
                    }
                return p;
                }

                else{
                
                m.put('nooverride', '1');
                m.put('RecordType',recTypeId);                        
                p.setRedirect(true);
                return p;

                }
   }
   catch(Exception l_objEx)
        {            
            String l_stMsg = 'Exception while redirecting to Case Creation Page.';
            String l_stErrMessage = l_objEx.getTypeName()+': '+l_stMsg;
            System.debug(l_stErrMessage);
            return null;
        }
   }
   }
karthikeyan perumalkarthikeyan perumal
Hello 

use below code.. but this not exact code for your test class.. 

you have to change according to your page 

Note: in the below code you have to change the pag name insted of this "Yourpagenmae" 
 
@isTest
private class LP_CreateNewCasePage_Controller_Test
{
static testMethod void OtherRedirect_Test() {

  System.Test.startTest();
   LP_CreateNewCasePage_Controller TestController= new LP_CreateNewCasePage_Controller ();
    PageReference pageRef = Page.Yourpagenmae;
    Test.setCurrentPage(pageRef);    
    TestController.OtherRedirect();   
  System.Test.stopTest();
}

static testMethod void OtherRedirect_Test1() {

  System.Test.startTest();
   LP_CreateNewCasePage_Controller TestController= new LP_CreateNewCasePage_Controller ();
    PageReference pageRef = Page.Yourpagenmae;
    Test.setCurrentPage(pageRef);
    pageRef.getParameters().put('retURL','003');
    TestController.OtherRedirect();   
  System.Test.stopTest();
}

static testMethod void OtherRedirect_Test2() {

  System.Test.startTest();
   LP_CreateNewCasePage_Controller TestController= new LP_CreateNewCasePage_Controller ();
    PageReference pageRef = Page.Yourpagenmae;
    Test.setCurrentPage(pageRef);
    pageRef.getParameters().put('retURL','001');
    TestController.OtherRedirect();   
  System.Test.stopTest();
}

}

Hope this will help you

Thanks
karthik
Ashok Kumar NayakAshok Kumar Nayak

Hi Karthik,

I tried with this, adding my page name. but its not getting covered at all. 

I'm just autopopulating the account and contact while creating a case with a VF page overiden for the New Button for Case SObject.

Thanks & Regards

Ashok