You need to sign in to do that
Don't have an account?

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.
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
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.
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:
I'm not sure how to decipher this.
Is this an error message from the class that you are deploying, or is this an unrelated class?
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:
Any ideas?
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.
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?