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
jls_74_txjls_74_tx 

Save and Redirect If No PageMessages

I'm a newbie to controllers but I compiled the following controller from various posts on the board, to use on a custom object on a force.com site.  After the user clicks a command button to save the form, I want SF to check for errors (using the <apex:PageMessages /> tag) and if no errors, save the form and redirect to www.tarl.com.  If there are errors, then I want the standard error messages to show and stay on the page.

 

The controller fails the deployment and I don't know enough about controllers to identify the problem. If someone could please help me, I would greatly appreciate it...and Kudos!

 

Thank you in advance!

 

public class SaveAndRedirectController
{
    public ApexPages.StandardController controller;

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

public PageReference SaveAndRedirect() {
    try {
        this.controller.Save();
    }  catch(DmlException ex){
        //this catches the errors and ensures they register on the page
        ApexPages.addMessages(ex);
    }
 
 
    //if there's an error message, perform a refresh
    // if not, redirect to google.com
    if (ApexPages.hasMessages()) {
        return null;
    } else {
        return new PageReference('http://www.tarl.com');  
    }
}
}

 

sfdcfoxsfdcfox

The problem is that no exception will be thrown using StandardController.save(). Instead, you have to check its return value:

 

public PageReference save() {
  PageReference ref = controller.save();
  if(ref!=null) { // Save was okay, so redirect.
    ref = new PageReference('http://www.tarl.com/');
  }
  return ref;
}

 

asish1989asish1989

HI

You need to write test class for deployment and code coverage should be 75%

@isTest
private class MileageTrackerTestSuite {

    static testMethod void runPositiveTestCases() { 

AccountPlan__c testAccountPlanInsert = new AccountPlan__c(name = 'Account Plan Test123',Account__c = companyString , accountmanager__c = Userinfo.getUserId());
                  
        insert testAccountPlanInsert;
        
         ApexPages.StandardController sc = new ApexPages.StandardController(testAccountPlanInsert);
        SaveAndRedirectController src=new SaveAndRedirectController (sc);
        src.SaveAndRedirect();
    }

}
       

 I am sharing some link about test class,please go through it .

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm

 

http://boards.developerforce.com/t5/Apex-Code-Development/Test-Class-for-a-StandardController-Extension/td-p/189828

 

When your code coverage will be more then 75% then you can definitely deploy your class

 

If this post answers your question please mark it asl solved and give kudos for this post

 

Thanks

 

jls_74_txjls_74_tx

Thank you sfdcfox, but that failed also.  Here is my complete code.

 

public class SaveAndRedirectController
{
    public ApexPages.StandardController controller;

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

public PageReference save() {
  PageReference ref = controller.save();
  if(ref!=null) { // Save was okay, so redirect.
    ref = new PageReference('http://www.tarl.com/');
  }
  return ref;
}
}

 

sfdcfoxsfdcfox

Sorry, change save() to saveAndRedirect(), that should be okay.

jls_74_txjls_74_tx

Sorry, but that failed also.  It is at 71% coverage.  Any other ideas?

 

public class SaveAndRedirectController
{
    public ApexPages.StandardController controller;

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

public PageReference saveAndRedirect() {
  PageReference ref = controller.save();
  if(ref!=null) { // Save was okay, so redirect.
    ref = new PageReference('http://www.tarl.com/');
  }
  return ref;
}
}

 

jls_74_txjls_74_tx

asish1989, I have created several extensions to sort tables and grab small sets of data and I have never had to create a test class for them to deploy properly.  The inbound change set runs the test and it deploys automatically.

sfdcfoxsfdcfox
If this code's coverage is only 71%, then it sounds like there was an error saving. Check your test code. We use this pattern frequently, and it works for us.