3 Replies Latest reply on Feb 12, 2020 12:31 PM by Paul Donders

    VF Button to Save and Redirect

    Erica Dean
      Share This:

      We are creating a visualforce page that enables record edits via a custom layout for the Incident object, and we need to add a button that will save the visualforce page, updating the record, and then return to either the current page or redirect to another page.

       

      We know we can create a button to save:

       

              <apex:commandButton action="{!save}" value="Recalculate Demand Score" />

       

      But this will return the user to the standard Salesforce layout (https://...salesforce.com/{Id}) not the current page (https://....visual.force.com/apex/BMC_RF_Demand_Scorecard?id={Id})

       

      We also know we can create a button to link to a different page:

       

              <apex:commandButton action="{!URLFOR("apex/BMC_RF_Demand_Scorecard?", null, [id=Id])}" value="Recalculate Demand Score" />

       

      Unfortunately, we can't figure out how to do both.

       

      Additionally, we want to create a button that will redirect to the record in the Console; however, the URLFOR function doesn't seem to support passing the variable into the middle of the URL vs. the end:

       

      <apex:commandButton action="{!URLFOR("/apex/BMCServiceDesk__RemedyforceConsole?record_id={Id}&objectName=Incident__c", null, [id=Id])}" value="Launch Remedyforce Console" />

       

      Any advice on either of these two topics would be much appreciated!

        • 1. Re: VF Button to Save and Redirect
          Paul Donders

          Hi Erica,

           

          In the apex class you can set this;

           

           

          To accomplish this, you will have to change the action attribute value from {!save} to {!saveAndRedirect}

          <apex:commandButton action="{! save}"value="Save"/> to <apex:commandButton action="{! saveAndRedirect}" value="Save"/>

           

          In the controller, you can write the following method which will do the standard save and redirect to another page

           

          public PageReference saveAndRedirect() {

            controller.save(); // This takes care of the details for you.

            PageReference newPage = Page.NewPage; // This will be the visualforce page or Url that we want to redirect to

            newPage.setRedirect(true);

            return newPage;

          }

           

           

          Paul Donders

          2Grips

          3 of 3 people found this helpful
          • 2. Re: VF Button to Save and Redirect
            Erica Dean

            Thanks, Paul.

             

            Any chance I can do this without a controller?  This remains an issue with me due to environmental constraints.

            • 3. Re: VF Button to Save and Redirect
              Paul Donders

              Hi Erica,

               

              Maybe with an example like below;

              Not sure it will do the trick. I stll would say use a very small controller extension with just this logic. (It will take a few lines)

               

              /e?retURL=/{!case.Id}&saveURL=/apex/YOURVFPAGEHERE

               

              To be honest I’m trying to get my head around the numbers of lines and limits you face. It makes me very curious, I’m almost convinced that thing can be more efficient.

               

              Anyway, not sure the answer meets your question.

               

              Paul Donders

              2Grips

              2 of 2 people found this helpful