+ Start a Discussion
Shawn Reichner 29Shawn Reichner 29 

Test class for VF Page Controller - HELP

Hello awesome devs...I have a good one here today! 

The company has had a Consulting group create a VF page and controller to do the following function.  It seems that thier Test class was removed from our environment somehow as this is in our production org, however no test class provides any code coverage any longer.  I need to make a small edit to the VF page, and want to ensure that the controller has proper coverage after I make the small tweak, but again I can not find any test class that provides coverage.  Thsi was done a few years back so the change sets that included this info has expired and we no longer have visibility into it.  So I need to create a new test class, but am unsure how to for this piece of particular code. 

Can anyone help to help me create a test class for the folllowing code?

On Object A - the new button is pressed, which navigates you to the VF page which allows you to either

A: select an existing account with a specific record type or:
B: if not found the next section of the VF page will allow you to create a new Account record to use for the custom object upon saving the VF page. 

Here is the controller code for this VF page
 
public class new_testVfpage {
    
    Public Purchase_request__c pr{get;set;}
    Public string accountname{get;set;}
    Public string phonenumber{get;set;}
    Public string Billingstreet{get;set;}
    Public string Billingcity{get;set;}
    public string contactname{get;set;}
    public string contactemail{get;set;}
    Public String billingzipcode{get;set;}
    public Account account {get;set;}
    Public string testfield{get;set;}
    public boolean isExistError{get;set;}
    public boolean isNullError{get;set;}

    public new_testVfpage(ApexPages.StandardController sc) {
        pr= (Purchase_request__c )sc.getRecord();
        account=new account();
        pr.Renewal__c='Existing Vendor';
        Id rtid= Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR Existing Vendor, No V-Qs /w Submit').getRecordTypeId();
        pr.recordtypeid =rtid;
        isExistError=false;
        
    }
    
    Public pagereference newsave(){
        integer needDesc = 0;
        integer descMax = 0;
        testfield=pr.Vendor_Name__c;

        //Identifiy if Insert or Update Vendor Type of Record Type
        Id vendorId = Schema.SobjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();
        List<Account> accList = [SELECT Id FROM Account where Id = :pr.Vendor_Name__c AND RecordType.Id =: vendorId];

        if(accList.IsEmpty()){//insert
            Id prrtid= Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR All Questions /w Submit Button').getRecordTypeId();
            pr.RecordTypeId = prrtid; //'01270000000UQfyAAG'; //PR All Questions /w Submit Button
        }else{//Update
            Id prrtid = Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR Existing Vendor, No V-Qs /w Submit').getRecordTypeId();
            pr.RecordTypeId = prrtid; //'01270000000UQg1AAG'; //PR Existing Vendor, No V-Qs /w Submit
        }
        
       //CheckVendor Name is Empty
       if(testfield == null || testfield ==''){   
          //When Account Name has value    
          if(accountname!= '' && accountname!= null){
              account.name=accountname;
              isExistError=false;
              //New Vendor will check the following fields
              if((account.Will_vendor_store_NPPI_or_PCI_data__c != 'Yes' && account.Will_vendor_store_NPPI_or_PCI_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store NPPI or PCI data? is required'));
                  isExistError=true;
              }
              
              if((account.Will_vendor_store_PHI_data__c != 'Yes' && account.Will_vendor_store_PHI_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store PHI data?'));
                  isExistError=true;
              }
              
              if((account.Will_vendor_store_Armor_corporate_data__c != 'Yes' && account.Will_vendor_store_Armor_corporate_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store Armor corporate data? is required'));
                  isExistError=true;
              }
              
              if((account.Will_vendor_access_Armor_or_Cust_data__c != 'Yes' && account.Will_vendor_access_Armor_or_Cust_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor access Armor or Customer Data? is required'));
                  isExistError=true; 
              }
              
              if((account.Are_vendor_services_Critical_to_Armor__c!= 'Yes' && account.Are_vendor_services_Critical_to_Armor__c!= 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Are vendor services critical to Armor? is required'));
                  isExistError=true;
              }
              //removes error if all the condition of required fields for new vendor is Yes
              if((account.Will_vendor_store_NPPI_or_PCI_data__c != '' && account.Will_vendor_store_NPPI_or_PCI_data__c != null && account.Will_vendor_store_NPPI_or_PCI_data__c != '--None--')
              && (account.Will_vendor_store_PHI_data__c != '' && account.Will_vendor_store_PHI_data__c != null && account.Will_vendor_store_PHI_data__c != '--None--')
              && (account.Will_vendor_store_Armor_corporate_data__c != '' && account.Will_vendor_store_Armor_corporate_data__c != null && account.Will_vendor_store_Armor_corporate_data__c != '--None--')
              && (account.Will_vendor_access_Armor_or_Cust_data__c != '' && account.Will_vendor_access_Armor_or_Cust_data__c != null && account.Will_vendor_access_Armor_or_Cust_data__c != '--None--')
              && (account.Are_vendor_services_Critical_to_Armor__c != '' && account.Are_vendor_services_Critical_to_Armor__c != null && account.Are_vendor_services_Critical_to_Armor__c != '--None--')){
                  isExistError=false;
              }
              
                //Error if NPPI/PCI desc is null when account.Will_vendor_store_PHI_data__c value is yes
                if(account.Will_vendor_store_NPPI_or_PCI_data__c == 'Yes' ){
                    if(account.NPPI_PCI_If_Yes_enter_description__c != null){
                       isExistError=false;
                       isNullError=false;
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'NPPI/PCI is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true;
                    }
            
                }else{
                    if(account.Will_vendor_store_NPPI_or_PCI_data__c == 'No'){
                        isExistError=false;
                        isNullError=false;
                    }else{
                        isExistError=true;
                    }
                }
            
                //Error if PHI desc is null when account.Will_vendor_store_PHI_data__c value is yes
                //if(account.Will_vendor_store_PHI_data__c == 'Yes' ){
                if(account.Will_vendor_store_PHI_data__c == 'Yes' ){
                    if(account.PHI_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'PHI is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true; 
                    }
                
                }else{
                    if(account.Will_vendor_store_PHI_data__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true;
                    }
                }
            
                //Error if Corp Data desc is null when Corp Data value is yes
                if(account.Will_vendor_store_Armor_corporate_data__c == 'Yes' ){
                    if(account.Corp_Data_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Corp Data is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true; 
                    }
                
                }else{
                    if(account.Will_vendor_store_Armor_corporate_data__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true;
                    }
                }
            
                //Error if Access Data desc is null when Access Data value is yes
                if(account.Will_vendor_access_Armor_or_Cust_data__c == 'Yes' ){
                    if(account.Cust_Data_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Access Data is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true; 
                    }
                
                }else{
                    if(account.Will_vendor_access_Armor_or_Cust_data__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true; 
                    }
                }
                
                //Error if Critical desc is null when account.Are_vendor_services_Critical_to_Armor__c value is yes
                if(account.Are_vendor_services_Critical_to_Armor__c == 'Yes' ){
                    if(account.Critical_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Critical is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true;    
                    }
                
                }else{
                    if(account.Are_vendor_services_Critical_to_Armor__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true;   
                    }
                }
                
                //removes error if all required fields are not null
                if((account.Will_vendor_store_NPPI_or_PCI_data__c == 'Yes')
                  && (account.Will_vendor_store_PHI_data__c == 'Yes' )
                  && (account.Will_vendor_store_Armor_corporate_data__c == 'Yes')
                  && (account.Will_vendor_access_Armor_or_Cust_data__c == 'Yes')
                  && (account.Are_vendor_services_Critical_to_Armor__c == 'Yes')){
                    //Description null check                      
                    if((account.NPPI_PCI_If_Yes_enter_description__c != null && account.NPPI_PCI_If_Yes_enter_description__c != '')
                     &&(account.PHI_If_Yes_enter_description__c != null && account.PHI_If_Yes_enter_description__c != '')
                     &&(account.Corp_Data_If_Yes_enter_description__c != null && account.Corp_Data_If_Yes_enter_description__c != '')
                     &&(account.Cust_Data_If_Yes_enter_description__c != null && account.Cust_Data_If_Yes_enter_description__c != '')
                     &&(account.Critical_If_Yes_enter_description__c != null && account.Critical_If_Yes_enter_description__c != '')
                    ){
                        isExistError=false;
                    }else{
                        isExistError=true;
                    }
                 }
          }else{
              ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Account Name is required'));
              isExistError=true;
          }
          
          
                
        Id accrtid= Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();
            account.recordtypeid=accrtid;
       
        contact c=new contact(); 
        if(contactname!= '' && contactname !=null){
           c.lastName=contactname;
        }
        if(isExistError==true){
            return null;
        }else{    
         upsert account;
         Upsert Pr; 
        }
         
        if(contactname != '' && contactname !=null){
        c.accountid=account.id;
        c.LeadSource='PR VF Page - Manually';
        insert c;
        }
         
        pr.Vendor_Name__c=account.id;
        pr.Renewal__c='New Vendor';
        upsert Pr; 
        }
        if(accountname!= '' && accountname!= null){
            upsert Pr;
            return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id); 
        }else{
            if(testfield == null || testfield ==''){
                return null; 
            }else{
                upsert Pr;
                return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id); 
            } 
        }   
             
    }
    
}

 
Best Answer chosen by Shawn Reichner 29
Alain CabonAlain Cabon
Hi Shawn,

The class seems impressive (big) but it is quite simple. It is an extension of a standard controller.

The class rules just three objects: a Purchase_request__c (master object for the standard controller)  a related account  (List<Account> accList = [SELECT Id FROM Account where Id = :pr.Vendor_Name__c AND RecordType.Id =: vendorId];) and a contact for this account.

The main problem is the combinations of variables for the errors according the values of fields of account. The test class will contains several creations of accounts for having all these combinations (but by copy/paste, it is not so difficult).

This blog is a reference: Jeff Douglas
http://blog.jeffdouglas.com/2010/06/02/testing-salesforce-com-controller-extensions/

MyController ext; => new_testVfpage ext;
MasterObject__c  masterObject => Purchase_request__c pr

ApexPages.StandardController con = new ApexPages.StandardController(masterObject);
ext = new MyController(con);

will become:

// creations of objects Purchase_request__c  pr and the Account related to pr
​Account acc = new Account( ... );
insert acc;
Purchase_request__c pr = new  Purchase_request__c( ...Vendor_Name__c = acc.id ... );
insert pr;
ApexPages.StandardController con = new ApexPages.StandardController(pr);
new_testVfpage ext = new new_testVfpage(con);
ext.accountname = 'my account name';
ext. newsave();

Other developers will perhaps post a more detailed solution but the principle is always the same when you have a template (here Jeff Douglas' example)

All Answers

Alain CabonAlain Cabon
Hi Shawn,

The class seems impressive (big) but it is quite simple. It is an extension of a standard controller.

The class rules just three objects: a Purchase_request__c (master object for the standard controller)  a related account  (List<Account> accList = [SELECT Id FROM Account where Id = :pr.Vendor_Name__c AND RecordType.Id =: vendorId];) and a contact for this account.

The main problem is the combinations of variables for the errors according the values of fields of account. The test class will contains several creations of accounts for having all these combinations (but by copy/paste, it is not so difficult).

This blog is a reference: Jeff Douglas
http://blog.jeffdouglas.com/2010/06/02/testing-salesforce-com-controller-extensions/

MyController ext; => new_testVfpage ext;
MasterObject__c  masterObject => Purchase_request__c pr

ApexPages.StandardController con = new ApexPages.StandardController(masterObject);
ext = new MyController(con);

will become:

// creations of objects Purchase_request__c  pr and the Account related to pr
​Account acc = new Account( ... );
insert acc;
Purchase_request__c pr = new  Purchase_request__c( ...Vendor_Name__c = acc.id ... );
insert pr;
ApexPages.StandardController con = new ApexPages.StandardController(pr);
new_testVfpage ext = new new_testVfpage(con);
ext.accountname = 'my account name';
ext. newsave();

Other developers will perhaps post a more detailed solution but the principle is always the same when you have a template (here Jeff Douglas' example)
This was selected as the best answer
Shawn Reichner 29Shawn Reichner 29
Alain,

Thank you again for your help.  I am still confused however, as I am attempting to go your toute, but I can only get 20 percent code coverage. What am i doing incorrectly?

Here is the class again and the following lines do not have code coverage...also following is the test class as I currently have it. 

Lines 5,6,7,9,10,14,47 through 215, 230,235,236,239 through 247 and lastly 254. 

Please help....
 
public class new_testVfpage {
    
    Public Purchase_request__c pr{get;set;}
    Public string accountname{get;set;}
    Public string phonenumber{get;set;}
    Public string Billingstreet{get;set;}
    Public string Billingcity{get;set;}
    public string contactname{get;set;}
    public string contactemail{get;set;}
    Public String billingzipcode{get;set;}
    public Account account {get;set;}
    Public string testfield{get;set;}
    public boolean isExistError{get;set;}
    public boolean isNullError{get;set;}

    public new_testVfpage(ApexPages.StandardController sc) {
        pr= (Purchase_request__c )sc.getRecord();
        account=new account();
        pr.Renewal__c='Existing Vendor';
        Id rtid= Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR Existing Vendor, No V-Qs /w Submit').getRecordTypeId();
        pr.recordtypeid =rtid;
        isExistError=false;
        
    }
    
    Public pagereference newsave(){
        integer needDesc = 0;
        integer descMax = 0;
        testfield=pr.Vendor_Name__c;

        //Identifiy if Insert or Update Vendor Type of Record Type
        Id vendorId = Schema.SobjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();
        List<Account> accList = [SELECT Id FROM Account where Id = :pr.Vendor_Name__c AND RecordType.Id =: vendorId];

        if(accList.IsEmpty()){//insert
            Id prrtid= Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR All Questions /w Submit Button').getRecordTypeId();
            pr.RecordTypeId = prrtid; //'01270000000UQfyAAG'; //PR All Questions /w Submit Button
        }else{//Update
            Id prrtid = Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR Existing Vendor, No V-Qs /w Submit').getRecordTypeId();
            pr.RecordTypeId = prrtid; //'01270000000UQg1AAG'; //PR Existing Vendor, No V-Qs /w Submit
        }
        
       //CheckVendor Name is Empty
       if(testfield == null || testfield ==''){   
          //When Account Name has value    
          if(accountname!= '' && accountname!= null){
              account.name=accountname;
              isExistError=false;
              //New Vendor will check the following fields
              if((account.Will_vendor_store_NPPI_or_PCI_data__c != 'Yes' && account.Will_vendor_store_NPPI_or_PCI_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store NPPI or PCI data? is required'));
                  isExistError=true;
              }
              
              if((account.Will_vendor_store_PHI_data__c != 'Yes' && account.Will_vendor_store_PHI_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store PHI data?'));
                  isExistError=true;
              }
              
              if((account.Will_vendor_store_Armor_corporate_data__c != 'Yes' && account.Will_vendor_store_Armor_corporate_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store Armor corporate data? is required'));
                  isExistError=true;
              }
              
              if((account.Will_vendor_access_Armor_or_Cust_data__c != 'Yes' && account.Will_vendor_access_Armor_or_Cust_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor access Armor or Customer Data? is required'));
                  isExistError=true; 
              }
              
              if((account.Are_vendor_services_Critical_to_Armor__c!= 'Yes' && account.Are_vendor_services_Critical_to_Armor__c!= 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Are vendor services critical to Armor? is required'));
                  isExistError=true;
              }
              //removes error if all the condition of required fields for new vendor is Yes
              if((account.Will_vendor_store_NPPI_or_PCI_data__c != '' && account.Will_vendor_store_NPPI_or_PCI_data__c != null && account.Will_vendor_store_NPPI_or_PCI_data__c != '--None--')
              && (account.Will_vendor_store_PHI_data__c != '' && account.Will_vendor_store_PHI_data__c != null && account.Will_vendor_store_PHI_data__c != '--None--')
              && (account.Will_vendor_store_Armor_corporate_data__c != '' && account.Will_vendor_store_Armor_corporate_data__c != null && account.Will_vendor_store_Armor_corporate_data__c != '--None--')
              && (account.Will_vendor_access_Armor_or_Cust_data__c != '' && account.Will_vendor_access_Armor_or_Cust_data__c != null && account.Will_vendor_access_Armor_or_Cust_data__c != '--None--')
              && (account.Are_vendor_services_Critical_to_Armor__c != '' && account.Are_vendor_services_Critical_to_Armor__c != null && account.Are_vendor_services_Critical_to_Armor__c != '--None--')){
                  isExistError=false;
              }
              
                //Error if NPPI/PCI desc is null when account.Will_vendor_store_PHI_data__c value is yes
                if(account.Will_vendor_store_NPPI_or_PCI_data__c == 'Yes' ){
                    if(account.NPPI_PCI_If_Yes_enter_description__c != null){
                       isExistError=false;
                       isNullError=false;
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'NPPI/PCI is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true;
                    }
            
                }else{
                    if(account.Will_vendor_store_NPPI_or_PCI_data__c == 'No'){
                        isExistError=false;
                        isNullError=false;
                    }else{
                        isExistError=true;
                    }
                }
            
                //Error if PHI desc is null when account.Will_vendor_store_PHI_data__c value is yes
                //if(account.Will_vendor_store_PHI_data__c == 'Yes' ){
                if(account.Will_vendor_store_PHI_data__c == 'Yes' ){
                    if(account.PHI_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'PHI is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true; 
                    }
                
                }else{
                    if(account.Will_vendor_store_PHI_data__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true;
                    }
                }
            
                //Error if Corp Data desc is null when Corp Data value is yes
                if(account.Will_vendor_store_Armor_corporate_data__c == 'Yes' ){
                    if(account.Corp_Data_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Corp Data is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true; 
                    }
                
                }else{
                    if(account.Will_vendor_store_Armor_corporate_data__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true;
                    }
                }
            
                //Error if Access Data desc is null when Access Data value is yes
                if(account.Will_vendor_access_Armor_or_Cust_data__c == 'Yes' ){
                    if(account.Cust_Data_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Access Data is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true; 
                    }
                
                }else{
                    if(account.Will_vendor_access_Armor_or_Cust_data__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true; 
                    }
                }
                
                //Error if Critical desc is null when account.Are_vendor_services_Critical_to_Armor__c value is yes
                if(account.Are_vendor_services_Critical_to_Armor__c == 'Yes' ){
                    if(account.Critical_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Critical is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true;    
                    }
                
                }else{
                    if(account.Are_vendor_services_Critical_to_Armor__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true;   
                    }
                }
                
                //removes error if all required fields are not null
                if((account.Will_vendor_store_NPPI_or_PCI_data__c == 'Yes')
                  && (account.Will_vendor_store_PHI_data__c == 'Yes' )
                  && (account.Will_vendor_store_Armor_corporate_data__c == 'Yes')
                  && (account.Will_vendor_access_Armor_or_Cust_data__c == 'Yes')
                  && (account.Are_vendor_services_Critical_to_Armor__c == 'Yes')){
                    //Description null check                      
                    if((account.NPPI_PCI_If_Yes_enter_description__c != null && account.NPPI_PCI_If_Yes_enter_description__c != '')
                     &&(account.PHI_If_Yes_enter_description__c != null && account.PHI_If_Yes_enter_description__c != '')
                     &&(account.Corp_Data_If_Yes_enter_description__c != null && account.Corp_Data_If_Yes_enter_description__c != '')
                     &&(account.Cust_Data_If_Yes_enter_description__c != null && account.Cust_Data_If_Yes_enter_description__c != '')
                     &&(account.Critical_If_Yes_enter_description__c != null && account.Critical_If_Yes_enter_description__c != '')
                    ){
                        isExistError=false;
                    }else{
                        isExistError=true;
                    }
                 }
          }else{
              ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Account Name is required'));
              isExistError=true;
          }
          
          
                
        Id accrtid= Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();
            account.recordtypeid=accrtid;
       
        contact c=new contact(); 
        if(contactname!= '' && contactname !=null){
           c.lastName=contactname;
        }
        if(isExistError==true){
            return null;
        }else{    
         upsert account;
         Upsert Pr; 
        }
         
        if(contactname != '' && contactname !=null){
        c.accountid=account.id;
        c.LeadSource='PR VF Page - Manually';
        insert c;
        }
         
        pr.Vendor_Name__c=account.id;
        pr.Renewal__c='New Vendor';
        upsert Pr; 
        }
        if(accountname!= '' && accountname!= null){
            upsert Pr;
            return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id); 
        }else{
            if(testfield == null || testfield ==''){
                return null; 
            }else{
                upsert Pr;
                return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id); 
            } 
        }   
             
    }
    
}

Test class so far....

@isTest
public class new_testVFPage_Test {

    static testMethod void tm1() {
        
        Id vendorId = Schema.SobjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();
        
        Account acc = new Account();
        acc.Name = 'Test Account';
        acc.Status__c = 'Active';
        acc.RecordTypeId = vendorId;
        insert acc;
        
        Purchase_Request__c pr = new Purchase_Request__c();
        pr.Vendor_Name__c = acc.Id;
        insert pr;
        
        ApexPages.StandardController con = new ApexPages.StandardController(pr);
        new_testVFpage ext = new new_testVFpage(con);
        ext.accountname = 'my account name';
        ext.newsave();
        
    }
    
    static testMethod void tm2(){
        
        Account acc = new Account();
        acc.Name = 'Test Account2';
        acc.Status__c = 'Active';
        insert acc;
        
        Purchase_Request__c pr2 = new Purchase_Request__c();
        insert pr2;
        
        ApexPages.StandardController con2 = new ApexPages.StandardController(pr2);
        new_testVFpage ext2 = new new_testVFpage(con2);
        ext2.newsave();
    }
    
    static testMethod void tm3(){
        
        Account acc = new Account();
        acc.Name = 'Test Account3';
        acc.Status__c = 'Active';
        acc.BillingStreet = '123 Anywhere Street';
        acc.BillingCity = 'Frisco';
        Acc.BillingStateCode = 'TX';
        acc.BillingCountryCode = 'US';
        insert acc;
        
        Contact c = new Contact();
        c.FirstName = 'Test';
        c.LastName = 'User';
        c.Email = 'test@test.com';
        c.Phone = '1112223333';
        insert c;
        
        Purchase_Request__c pr3 = new Purchase_Request__c();
        pr3.Vendor_Name__c = acc.Id;
        insert pr3;
        
        ApexPages.StandardController con3 = new ApexPages.StandardController(pr3);
        new_testVFpage ext3 = new new_testVFpage(con3);
        ext3.newsave();
    }
    
    static testMethod void tm4(){
        
        Id vendorId = Schema.SobjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();
        
        Account acc = new Account();
        acc.Name = 'Test Account4';
        acc.Status__c = 'Active';
        acc.BillingStreet = '123 Anywhere Street';
        acc.BillingCity = 'Frisco';
        Acc.BillingStateCode = 'TX';
        acc.BillingCountryCode = 'US';
        acc.Will_vendor_store_NPPI_or_PCI_data__c = 'None';
        acc.Will_vendor_store_PHI_data__c = 'None';
        acc.RecordTypeId = vendorId;
        
        insert acc;
        
        Contact c = new Contact();
        c.FirstName = 'Test';
        c.LastName = 'User';
        c.Email = 'test@test.com';
        c.Phone = '1112223333';
        c.AccountId = acc.Id;
        insert c;
        
        Purchase_Request__c pr4 = new Purchase_Request__c();
        pr4.Vendor_Name__c = acc.Id;
        insert pr4;
        
        ApexPages.StandardController con4 = new ApexPages.StandardController(pr4);
        new_testVFpage ext4 = new new_testVFpage(con4);
        ext4.newsave();
    }
    
}

 
Alain CabonAlain Cabon

Hi Shawn

As you have posted a new question with your code, you will get many answers.  

public Account account {get;set;}  // fields on screen not already saved 

Account acc2 = new Account( . myfields initialisation ie: Will_vendor_store_Armor_corporate_data__c == 'Yes'....); 
// insert not needed for acc2 but ext4.account = acc2; is mandatory before the call ext4.newsave();

ApexPages.StandardController con4 = new ApexPages.StandardController(pr4);
new_testVFpage ext4 = new new_testVFpage(con4);
ext4.account = acc2     // calls the setter of account inside the controller. so all the fields account.myfields will be filed 
ext4.newsave();

Shawn Reichner 29Shawn Reichner 29
I am sorry I am not understanding what it is you just posted Alain.  Can you help me understand what it is you are trying to convey to me?  I am hoping for more answers as I am completely lost here and just need this to pass....can you start another example that would walk me  through the scenarios?

Thank you so much for your help, I really appreciate it, I just need a little more info...

Shawn
Shawn Reichner 29Shawn Reichner 29
Alain, appreciate any help you can provide....I added the line ext4.Account - acc2; however that did not increase any of the code coverage.  Again can you explain a little better, maybe with an example of how you would handle the first error message so that I have an example to work off of?  Thanks again for yoru help here, I am just not getting it. :(

Shawn
Shawn Reichner 29Shawn Reichner 29
Alain,  Nevermind on this request I got it tested to 97 percent by playong around with the code a little more.  Thanks for your help as it got me started, but I had to figure out the rest on my own, which is a great learning experience.  Thanks again.