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
Paras JainParas Jain 

Test class only covers 83% but i need 100% coverage.

/*********************************************************************************************************************
* Name               : CPQ_CTRL_CheckOpenOpportunities                                                               *
* Description        : The class performs following functionality:                                                   *
*                      - This class is controller class for CPQ_CTRL_CheckOpenOpportunities_VFP_Opp VisualForce page.*                     
* Created Date       : 04-Dec-2015                                                                                   *
* Created By         : Accenture                                                                                     *
* Last Modified Date :                                                                                               *
* Last Modified By   :                                                                                               *
* -------------------------------------------------------------------------------------------------------------------*
* VERSION     AUTHOR      DATE                                                                                       *
*   1.0 -    Accenture   04-Dec-2015                                                                                 *
**********************************************************************************************************************/
public with sharing class CPQ_CTRL_CheckOpenOpportunities {
    public String accId{get;set;}
    public Opportunity opportunity{get;set;}
    public List<Opportunity> oppList{get;set;}
    public Boolean showAccountPgBlock {set;get;}
    public Boolean showOppListPgBlock {set;get;}    
    public Pagereference pgref;
    public String customLabelRecordTypeOpp = Label.lbl_Opportunity_New_Record;   
    
    /***********************************************************************************************
    * @Description : Controller                                                                    *
    *                This controller class fetches accountId & CustomLabel.                        *
    * @Params      : ApexPages.StandardController                                                  *
    * @Return      :                                                                               *    
    ***********************************************************************************************/
    public CPQ_CTRL_CheckOpenOpportunities(ApexPages.StandardController controller) {
        accId = apexpages.currentpage().getparameters().get('accid');
        opportunity= new Opportunity();
    }

 // Query on Record Type of Opportunity to fetch the RecordTypeId having name 'New'
    RecordType rt = [SELECT Id FROM RecordType where DeveloperName = : customLabelRecordTypeOpp AND sObjectType = 'Opportunity' limit 1];
    
    /****************************************************************************
     * @Description :This method check for the open Opportunities associated to the account .*
     * @Params      : none                                                    *
     * @Return      : pgref                                                  *    
     ****************************************************************************/

    public pagereference checkStageBeforeInsert() {    
        CPQ_UTIL_CommonMethod uHelper = new CPQ_UTIL_CommonMethod();
        oppList = uHelper.checkOpenOpportunities(accId);
    
        pgref = new pagereference('/006/e?retURL=%2F'+ accId +'&accid='+ accId + '&RecordType=' + rt.Id + '&ent=Opportunity');
        pgref.setRedirect(true);
        pgref.getParameters().put('nooverride', '0');

        if (accId != null) {
            showAccountPgBlock = false;
            showOppListPgBlock = true;
            if (oppList.size() > 0){
                return null;
            }else {   
                return pgref;
            }
        } 
        else{    
            showAccountPgBlock = true;
            showOppListPgBlock = false;
            return null;   
        }
    }
    
    /*********************************************************************************************
    *@Description : This method redirect the user to the Origin page *
    *@Params      : AccountId                                                                        *
    *@Return      : pgref                                                             *    
    *********************************************************************************************/  
    public pagereference cancelOpportunity() {    
      if(accId == null){   
          pgref = new pagereference('/006/o');        
      }
      else{
          pgref = new pagereference('/'+accId );    
      }      
        pgref.setRedirect(true);
        pgref.getParameters().put('nooverride', '0'); 
        return pgref;   
    }
    
    
   /**********************************************************************************************
    *@Description : This method returns the List of Open Opportunities                           *
    *@Params      : AccountId                                                                    *
    *@Return      : List of Open Opportunities                                                   *    
    *********************************************************************************************/     
    public pagereference opportunitiesOnAccount() {
        showOppListPgBlock = true;
        CPQ_UTIL_CommonMethod uHelper = new CPQ_UTIL_CommonMethod();
        oppList = uHelper.checkOpenOpportunities(opportunity.AccountId);
        if(oppList.size()==0){
            pgref = new pagereference('/006/e?retURL=%2F006%2Fo&accid=' + opportunity.AccountId + '&RecordType='+rt.Id+'&ent=Opportunity');
            pgref.setRedirect(true);
            pgref.getParameters().put('nooverride', '0');  
            return pgref;            
        }
        else{          
            return null;
        }
    }
    
    
   /*********************************************************************************************
    *@Description : This method is used to continue and create new Opportunity.                                       *
    *@Params      : void                                                                         *
    *@Return      : pgref                                                             *    
    *********************************************************************************************/    
    public pagereference newOpportunity(){   
        if (accId == null) {
            pgref = new pagereference('/006/e?retURL=%2F006%2Fo&accid=' + opportunity.AccountId + '&RecordType='+rt.Id+'&ent=Opportunity');
            pgref.setRedirect(true);
            pgref.getParameters().put('nooverride', '0');
            return pgref;
        } 
        else {
            return pgref;
        }    
    }

// End of Class.

// Test class for this...
@isTest
private class CPQ_CTRL_CheckOpenOpportunitiesTest {
    static testMethod void validateOpenOpportunitiesClass() 
    {

        Account testAccount= TestDataUtil.createAccount();
       /* Test.startTest();
       // Database.DeleteResult result = Database.insert(testAccount, false);
        Test.stopTest();
        */ 
        
        RecordType rt = [SELECT Id FROM RecordType where DeveloperName ='CPQ_New' AND sObjectType = 'Opportunity' limit 1];  
    
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        CPQ_CTRL_CheckOpenOpportunities testAccPlan = new CPQ_CTRL_CheckOpenOpportunities(sc);
        
        PageReference pageRef = Page.CPQ_CTRL_CheckOpenOpportunities_VFP_Opp;
        pageRef.getParameters().put('accId', testAccount.Id);
        pageRef.getParameters().put('RecordType', rt.Id);
        Test.setCurrentPage(pageRef);
        
        testAccPlan.checkStageBeforeInsert();
        testAccPlan.cancelOpportunity();
        testAccPlan.opportunitiesOnAccount();
        testAccPlan.newOpportunity();        
    }
}
Paras JainParas Jain
Account Id is showing null
Amol SolankiAmol Solanki
Hi Paras ,

Instead of using "apexpages.currentpage().getparameters().get('accid');"   use this.acct = (Account)controller.getRecord();.
Paras JainParas Jain
my standardController is of  Opportunity
Amol SolankiAmol Solanki
What is the url which is formed ??
Paras JainParas Jain
when i click on New Opportunity which is shown in Account Related List ...then i pass accountId with the page Name..
/apex/CheckOpenOpportunities_VFP_Opp?accid=0014B0000041RdJ