In Salesforce1, I want to redirect back to Salesforce1 on a command button after the action runs. If there is an error, it should stop redirecting. I tried adding sforce.one.navigateToSObject on onComplete, which redirects even when there is error on the page resulting from the validations from the apex action on the command button. How can I make it redirect only when save actions runs successfully?

  • You should post your code, that would be helpful. – Mike Katulka Jun 3 '15 at 19:31
  • <apex:commandButton action="{!save}" rerender="id here" onComplete="sforce.one.navigateToSObject()?"> – user4402671 Jun 3 '15 at 20:30
  • do you intend to use lightening framework (specifically aura components)? Or do you plan to use Visualforce pages in Salesforce1 app? – Anshul Jun 3 '15 at 21:25
  • no aura components. just VF page. How would aura components help? – user4402671 Jun 4 '15 at 14:19

Alright, so based on your comment, you've got a VF page and Controller. You've got an action in the controller that returns void or null (since it's salesforce1). You've got a command button in the VF page calling the action.

Probably the simplest thing you could do is in the oncomplete of the button, call a custom Javascript function. Like this one, which will handle the navigation.


  <apex:commandButton action="{!save}" rerender="messages, othercontent" onComplete="postSave(); return false;">

  <apex:pagemessages id="messages" />

  <apex:outputPanel id="otherContent">


    function postSave() {
        // here you just check to see the status of the save, you might be looking for the id of 
        // the record if it's insert, or something else if it's update.  If it's update, then 
        // setup a controller boolean property like "isSaveError" and set this in your save 
        // action, and check for that here.
        if('{!Account.id}' != ''){
            // the record saved, redirect
        // else, do nothing the error should be showing on page  
| improve this answer | |
  • I just added sforce.one.back(false); in the postSave. It redirects correctly to the previous page in one/one.app in Chrome. But not on the actual device. The same code works on <button onclick="sforce.one.back(false); return false;" >Cancel</button> sforce.one.navigateToSObject('{!Account.id}'); does not work either. Not sure why. – user4402671 Aug 3 '15 at 15:18

This does not address your question directly with your current setup, but I have experienced a lot more control for this type of scenario with JavaScript remoting in Salesforce1. I do not know of any direct way to automatically navigate after using the standard commandButton component since it really isn't set up for client-side navigation (it would be great to be proven wrong!).

With JavaScript remoting you will need to do a lot more work for validation etc., but you gain control over taking action on a success or failure after the Controller method completes (will need to implement Save). There may be a better way to accomplish your goal, but consider this route as well:


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.