Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

When the user clicks the command button, the action does not fire, but the redirect to the new url does. I want the users record to save (validation rules, etc to get called first), then if it saves ok, let them proceed to the new url.

<apex:page standardController="Opportunity"  standardStylesheets="true" extensions="OpportunityController" >
<apex:form >
<center>
<apex:commandButton action="{!save}"  onComplete="openWindow()" value="Change Record Type" />

<script>
function openWindow(){
window.parent.parent.window.location = ("{!redirect}");
}
</script>

</center>
</apex:form>
 </apex:page>
share|improve this question

2 Answers 2

up vote 5 down vote accepted
  1. onComplete depends on the request being an AJAX request. This only happens when reRender is present, even if it has no value. The most usual means of invoking an AJAX request is to use reRender="form", and using <apex:form id="form"> instead of not providing an ID.

  2. You won't be able to determine if the page had an error directly in your function. onComplete always fires, even if there were errors. A common mechanism to pass a value back from the controller is through an apex:inputHidden. What follows is a common example of how you might detect if the function should redirect.

.

<script>
function onComplete() {
    var hasErrorElement = document.getElementById("{!$Component.form.hasError}");
    if(hasErrorElement.value === "false") {
        // perform logic here
    }
}
</script>

<apex:form id="form">
    <apex:commandButton action="{!action}" reRender="form" onComplete="onComplete()" value="Go!"/>
    <apex:inputHidden value="{!hasError}" id="hasError"/>
</apex:form>
share|improve this answer
    
good point! 1 up :) –  subodhbahl Sep 13 '13 at 18:21
    
@sfdcfox can you please elabborate some more on #2 and how i should proceed? –  nivyaj Sep 13 '13 at 19:09
2  
There's an example in the source I provided. hasError could be a string, boolean value, etc. I'd probably recommend a string just for type safety concerns, though. –  sfdcfox Sep 13 '13 at 19:32
    
@sfdcfox how do i capture the errors in the controller? I need to instantiate the "hasError" value –  nivyaj Sep 23 '13 at 13:41
    
hasErrors would be a public string hasErrors { get; set; }. You'd set it just like any other variable. –  sfdcfox Sep 23 '13 at 14:36

Think this way now:

1. You want the action to do the logic.
2. Why not make the action do the redirection too?

Follow the code below:

public class OpportunityController {

 //Constructor
 public OpportunityController(){
    //Do onload logic
 }

 //action
 Public PageReference save(){
      //Do the logic for the save

      //Save the record 

      //Redirect
      PageReference redirect = new PageReference ('http://google.com');
      redirect.setRedirect(true);
      return redirect;
 }

}

I feel this should work. Please give it a go!

share|improve this answer
    
That's perfectly valid, and the most usual way of accomplishing this task. Sometimes, though, you want to do something else besides just a simple redirect that you might want to do client-side, which is why onComplete exists. But this answer would definitely work. –  sfdcfox Sep 13 '13 at 18:52
    
Thanks @sfdcfox but my answer was the common way to do it, you approached it differently. Kudos for that! I learnt something today :) –  subodhbahl Sep 13 '13 at 19:03
1  
I definitely agree that for a simple redirect, PageReference is the way to go. –  sfdcfox Sep 13 '13 at 19:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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