+ Start a Discussion
Karthik YRCKarthik YRC 

Kindly tell me how to write Test Class for Below method

public PageReference saveAndBind(){ 
        
        PageReference pgRef = controller.save();
        Site__c site;
        
        Map<string,string> URLParameters = ApexPages.currentPage().getParameters();
        String returnPage; 
        if(URLParameters.containsKey('returnBack')){
            returnPage = URLParameters.get('returnBack');
            pgRef = new PageReference('/' + returnPage); 
        }
        System.debug('Page Reference URL is :' + pgRef);
        if(null != pgRef){
            site = new Site__c();
            if (null != returnPage && returnPage.length() >=3 && returnPage.substring(0,3).equals(DescribeUtility.getObjectKeyPrefix('Account'))){
                site.Account__c = returnPage;
            }
            else if(null != returnPage && returnPage.length() >=3 && returnPage.substring(0,3).equals(DescribeUtility.getObjectKeyPrefix('Opportunity')))
            {
                if(site.opportunity__r.site_name__c!=null)
                    site.name=site.opportunity__r.site_name__c;
                else 
                    site.name= addr.address_line_1__c +' '+addr.city__c+' '+addr.state__c;    
                    site.Opportunity__c = returnPage;
                  system.debug('@@-->Opportunity');
            }
            
            // Added by Piyush for Item 5568
            else if(null != returnPage && returnPage.length() >=3 && returnPage.substring(0,3).equals(DescribeUtility.getObjectKeyPrefix('Contract')))
            {
                site.Contract__c = returnPage;
                  system.debug('@@-->Contrct');
            }
            
            site.Address__c = controller.getRecord().ID;
            site.zip__c = addr.Postal_Code__c;
            Insert site;
        }
        return pgRef;
    }
    
     public String getAccountKeyPrefix(){
        Schema.DescribeSObjectResult dsr = Account.sObjectType.getDescribe();
        return dsr.getKeyPrefix();
     }
}