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
mickpeamickpea 

SaveAndReturn Controller Extension and Testing

Hello All,

 

I have a new issue where I am trying to add a SaveandReturn function to my visualforce page. So that when you click Save it brings you to the original page you started on prior to clicking New Child Record. I believe I have the functionality of this request. Now I am trying to create a test method for it. I feel I am very close.

 

public with sharing class SaveAndReturnController{

    // Field_Service__c fieldservice;
    private ApexPages.StandardController controller;
    public SaveAndReturnController(ApexPages.StandardController controller) {
    this.controller = controller;
    }

    public PageReference SaveAndReturn(){
        controller.save();
        PageReference returnPage = controller.cancel();
        returnPage.setRedirect(true);
        return returnPage;
    }
    
    static testmethod void testSaveAndReturnController(){

    Field_Service__c fieldService = new Field_Service__c();
    insert fieldService;
    ApexPages.StandardController controller = new ApexPages.standardController(fieldService);
  
    SaveAndReturnController testSARC = 
      new SaveAndReturnController(controller);
  
    system.assertEquals(testSARC.SaveAndReturn(), returnPage);
  }


}

 I am receiving an issue that "Variable does not exist: returnPage".

 

I attempted to resolve this by editing the code to this below:

public with sharing class SaveAndReturnController{

    // Field_Service__c fieldservice;
    private ApexPages.StandardController controller;
    public SaveAndReturnController(ApexPages.StandardController controller) {
    this.controller = controller;
    }

    public PageReference SaveAndReturn(){
        controller.save();
        PageReference returnPage = controller.cancel();
        returnPage.setRedirect(true);
        return returnPage;
    }
    
    static testmethod void testSaveAndReturnController(){

    Field_Service__c fieldService = new Field_Service__c();
    insert fieldService;
    ApexPages.StandardController controller = new ApexPages.standardController(fieldService);
  
    SaveAndReturnController testSARC = 
      new SaveAndReturnController(controller);
  
    system.assertEquals(controller.cancel().getURL(), testSARC.SaveAndReturn().getURL());
  }


}

 However, when I run the test it fails and gives 0% test coverage. Can anyone kindly explain what it is I am doing wrong? This is my first controller extension so any help is appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

When you deploy code to production, Salesforce ensures that all unit tests succeed.  You are now in the situation where you can't deploy code to production as there are existing unit test failures.  Welcome to the club - we've all been there.  

 

Unfortunately, you'll need the failing tests to be fixed before you can deploy your code.  If you can fix the test in the same sandbox that you are developing in, you can add the fixed code to your change set.  Otherwise you'll need whoever wrote the original code to deploy a fixed version.

All Answers

bob_buzzardbob_buzzard
What is the failure message that you are seeing?
mickpeamickpea

Well at first I did not realize that the test gave an error message, so after reading your reply I went to search for it. Turns out the test was failing because I was trying to insert a Field Service but had not specified sufficient parameters to get past my validation rules. I removed the insert and the test passed.

 

It may seem quite trivial but thanks for the heads up on the error message.

mickpeamickpea

Welp, it passed the initial test within my Sandbox. It was even working as designed when I tested it in the Sandbox.

 

However when I went to deploy the change set I received this message below:

 

error
This simulated deployment failed due to one or more errors.
  
Deployment Summary 
Change SetField Service (Save and Return)Deployed By 
Start Time2/19/2013 1:45 PMComponents2
End Time2/19/2013 1:45 PMApex Tests Run5
StatusFailedValidate Only
Component Deployment Results 
Failed ( 2 )
API NameTypeLineColumnProblem
MassUpdateSimpleControllerTest.singleUpdateTest()Class111 Failure Message: "System.NullPointerException: Attempt to de-reference a null object", Failure Stack Trace: "Class.MassUpdateSimpleController.__sfdc_fieldName: line 111, column 1 Class.MassUpdateSimpleControllerTest.singleUpdateTest: line 33, column 1"
Deploy Error   Average test coverage across all Apex Classes and Triggers is 71%, at least 75% test coverage is required.

 

I'm not sure how to decipher this.

bob_buzzardbob_buzzard

Is this an error message from the class that you are deploying, or is this an unrelated class? 

mickpeamickpea

The change set I am deploying consists of only the controller extension (SaveAndReturnController), posted below (and above). The failure message that it is providing me is confusing because it references items that are not even in my code. Quite frankly, they don't appear to be even related to the code that I am trying to deploy.

 

The code I am referencing is:

public with sharing class SaveAndReturnController{

    public SaveAndReturnController() {

    }


    private ApexPages.StandardController controller;
    public SaveAndReturnController(ApexPages.StandardController controller) {
    this.controller = controller;
    }

    public PageReference SaveAndReturn(){
        controller.save();
        PageReference returnPage = controller.cancel();
        returnPage.setRedirect(true);
        return returnPage;
    }
    
    static testMethod void testSaveAndReturnController(){

        Field_Service__c fieldService = new Field_Service__c();
        ApexPages.StandardController controller = new ApexPages.standardController(fieldService);  
        SaveAndReturnController testSARC = new SaveAndReturnController(controller);
  
    System.assertEquals(controller.cancel().getURL(), testSARC.SaveAndReturn().getURL());
  }
}

 Any ideas?

bob_buzzardbob_buzzard

When you deploy code to production, Salesforce ensures that all unit tests succeed.  You are now in the situation where you can't deploy code to production as there are existing unit test failures.  Welcome to the club - we've all been there.  

 

Unfortunately, you'll need the failing tests to be fixed before you can deploy your code.  If you can fix the test in the same sandbox that you are developing in, you can add the fixed code to your change set.  Otherwise you'll need whoever wrote the original code to deploy a fixed version.

This was selected as the best answer
mickpeamickpea

Quite frustrating. I did some research and came to that conclusion. Luckily it was a free package that was not quite needed so I simply uninstalled it.

 

I am going to just try to re-install the package. To my understanding this will not be a problem, correct?

bob_buzzardbob_buzzard
Yes, up until you next need to deploy code :)