5

I'm stuck with onclick and oncomplete. looking for a solution on this.

I have a controller that simply saves a Case record. I want to display a Message to User that 'Your case is saved with {!caseId}' mapped in the alert and on Clicking OK on alert box, it should navigate to case record.

Here is what i have done so far.

Controller:

public with sharing class CaseController{

  public Case c {get;set;}
  public String message {get;set;}
  public String caseId {get;set;}

  public CaseController() {
         c = new Case(Status='Submitted',Origin='Community Portal',Subject='',Description='');
         message = ''; // initial message.
  }

  public PageReference save() { 
        if(String.isBlank(c.Subject)){
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'error.'));
    return null;
        }
        else{
        insert c;
        message = 'Your case is saved with ' + c.id;
    caseId = c.id;
        }

    /* tried this but it did not work
        PageReference pageRef = new PageReference('/' + c.id);
        pageRef.setRedirect(true);           
        return pageRef; 
    */

    return null;
  }
}

Controller VF:

<apex:page controller="ActionFunctionDemoController">  
<apex:form >

  <apex:outputPanel id="jspanel">
  <script>  
  function afterSave() {
      var cId = '{!caseId}';
    alert('{!message}');
    window.location = '/' + cId;
  }
  </script>
  </apex:outputPanel>

  <apex:actionFunction name="doControllerSave" action="{!save}" rerender="jspanel"/>

  <apex:pageBlock id="blockSection">
  <apex:pageMessages id="msgs" />
  <apex:pageBlockButtons >
    <apex:commandButton onclick="doControllerSave();" 
      oncomplete="afterSave()" value="Save" reRender="blocksection,msgs"/>
  </apex:pageBlockButtons>
  <apex:pageMessages />

  <apex:pageBlockSection columns="1">
    <apex:inputField value="{!c.Subject}"/>  
    <apex:inputField value="{!c.Description}"/>  
  </apex:pageBlockSection>

  </apex:pageBlock>
  </apex:form>
</apex:page>

i'm unable to redirect to case record page. Am i missing something here ?

2
  • What do you mean when you say your attempt "did not work"? What happened when you tried it?
    – Rob
    Sep 11 '15 at 22:41
  • Case is getting created in the background, but there is no Redirection is not happening. Alert message is popping up correctly.
    – AjaySFDC
    Sep 11 '15 at 22:44
4

So when you create the script and use the binding the value of the binding is the what it was when the page loads.

If all you wanted to do was redirect to the new case (without a message) that is easy:

  1. Create an actionFunction that does this:

     <apex:actionFunction name="gotoCase" action="{!gotoCase}" rerender="msgs"/>
    

then code

public pagereference gotoCase(){
  return new PageReference('/' + caseID);
}

Getting an alert (which should not bee needed as you are redirecting) is a bit trickier and can be done by rerendering a script section (just one way of doing it)

Keep your script at the top like this:

<script>  
   var cId;

  function afterSave() {
    alert('{!message}');
    window.location = '/' + cId;
  }
  </script>

Create a rerendered panel with the variable

<apex:outPutPanel id="script_block">
    <script>
        cId = '{!caseId}';
    </script>
</apex:outPutPanel>

Then change your command button to (to update the var cId):

<apex:commandButton onclick="doControllerSave();" 
      oncomplete="afterSave()" value="Save" reRender="script_block,msgs"/> 

The rerender happens before the oncomplete to the var cId will have the new caseId. But if you are redirecting to the case you could simply show a message "Your case has been created and you will be taken to it now" or similar and you can do away with all the trickery in the code.

Note A more appropriate way to do this could be with a remote action as well.\

To simplify here is working class and vf page as an example

CLASS

public class myTest{

    public id aId {get;set;}


    public void seeAID(){
        aId = [Select ID From Account limit 1].id;
    }

}

VF Page

<apex:page Controller="myTest" cache="false">

<script>

var aId;

function testMe(){
alert(aId);
   window.location = '/' + aId;
}
</script>

<apex:form >
<apex:commandButton value="click" action="{!seeAID}" oncomplete="testMe();" rerender="script_block"/>
</apex:form>


<apex:outPutPanel id="script_block">
<script>
 aId = '{!aId}';
</script>
</apex:outPutPanel>

</apex:page>
8
  • No. I want an alert message of the Case Id and then the redirect should fire. OnComplete should do both alert and redirection to new created case record. Is this possible ?
    – AjaySFDC
    Sep 11 '15 at 22:50
  • @AjaySFDC - See answer. I was still formulating it when you posted your comment. Again, realize that the value of cId in the initial script block will never change unless you do as above.
    – Eric
    Sep 11 '15 at 22:52
  • Hi Eric i tried your approach. Still its not getting redirected to the record.
    – AjaySFDC
    Sep 11 '15 at 23:07
  • What does you console log say? log the value of cId in the afterSave method and report back. You are most likely doing something else incorrect as the above works if done properly.
    – Eric
    Sep 11 '15 at 23:08
  • @AjaySFDC - See answer with working example code
    – Eric
    Sep 11 '15 at 23:14

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.