+ Start a Discussion
raorao 

adding functionality to save button

I want to add my own logic to the existing save button functionality.  I have task like, after saving the record I want redirect to another page. 
Scenario:  When we create a contact from account page, after saving the contact we will be redirected to newly created contact screen. But I want it to redirect to contact's account screen.
 
any kind of help is appreciated.
Best Answer chosen by Admin (Salesforce Developers) 
Ben4817Ben4817
This is completely possible.  I have done a similar action on a couple of my pages.  You can do this with either a custom controller, or a standard controller.  If you are using a custom controller, you need to insert something like the following lines in the save function:

        // After Save, navigate to the new Case page:
        PageReference contPage = new PageReference('/500/e?retURL=%2F' + contact.id + '&def_account_id=' + contact.accountid + '&def_contact_id=' + contact.id);
        contPage.setRedirect(true);
        return contPage;

If you are using a standard controller, you just need to add an attribute to your Save commandButton.  In mine, I want to run a javascript to dynamically set some of the fields in the page to which I am navigating, so I have the following:

        <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Verified" action="{!save}"
                oncomplete="newURL()"/>
        </apex:pageBlockButtons>
    <script>
      function newURL() {
          var os = "";

          if ("{!Contact.Account.OperatingSystem__c}" != "") os = "&00N50000001qAzA=" + "{!Contact.Account.OperatingSystem__c}";
          address = "https://na3.salesforce.com/500/e?retURL=%2F{!Contact.Id}&def_account_id={!contact.Account.Id}&def_contact_id={!Contact.Id}&RecordType=0125000000018E8&cancelURL=%2F{!contact.Id}&ent=Case" + os;
          window.parent.location.href = address;
      }
   </script>

You could probably just include the URL to which you want to navigate instead of the function call to newURL().  Hope this helps!

All Answers

ShamSham
By Default Salesforce always takes you to the Detail page of the saved record.

It is sort of unalterable, unless you override the New/Edit page of the Contact.
But overriding the Edit and New page will involve  work as you have create Custom VF page and handle the validation
and save logic.

Ben4817Ben4817
This is completely possible.  I have done a similar action on a couple of my pages.  You can do this with either a custom controller, or a standard controller.  If you are using a custom controller, you need to insert something like the following lines in the save function:

        // After Save, navigate to the new Case page:
        PageReference contPage = new PageReference('/500/e?retURL=%2F' + contact.id + '&def_account_id=' + contact.accountid + '&def_contact_id=' + contact.id);
        contPage.setRedirect(true);
        return contPage;

If you are using a standard controller, you just need to add an attribute to your Save commandButton.  In mine, I want to run a javascript to dynamically set some of the fields in the page to which I am navigating, so I have the following:

        <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Verified" action="{!save}"
                oncomplete="newURL()"/>
        </apex:pageBlockButtons>
    <script>
      function newURL() {
          var os = "";

          if ("{!Contact.Account.OperatingSystem__c}" != "") os = "&00N50000001qAzA=" + "{!Contact.Account.OperatingSystem__c}";
          address = "https://na3.salesforce.com/500/e?retURL=%2F{!Contact.Id}&def_account_id={!contact.Account.Id}&def_contact_id={!Contact.Id}&RecordType=0125000000018E8&cancelURL=%2F{!contact.Id}&ent=Case" + os;
          window.parent.location.href = address;
      }
   </script>

You could probably just include the URL to which you want to navigate instead of the function call to newURL().  Hope this helps!
This was selected as the best answer