Tell me more ×
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.

Here's what I want to do. I'm overriding a new/edit button for tasks. when a VF page loads, have it check a condition in my extension. If that condition is met, show a pop-up and re-deirect the user to the previous page. Otherwise, I want to call a method in my extension class which returns a PageReference, and send them there.

Is this possible? It seems like javascript remoting is what people use when they want to call a method from a controller, not an extension.

Here's where I am so far

<apex:page standardController="Task" extensions="TaskExtension"  >
  <apex:form>
    <script>
          var displayPopUp = {!method_from_extension};
            if(displayPopUp == true ){
                alert('Condition Not met. Please enter in info'); 
                window.location ="/{!Id}"                   
                   }
                 else{
                {!send_me_to_a_VF_page}
                 }
    </script>    
</apex:form>

share|improve this question

2 Answers

up vote 2 down vote accepted

In your example, you don't need to call a method to get the data you're looking for. You can simply access a property in the extension.

Page:

<apex:page standardController="Task" extensions="TaskExtension">
  <script type="text/javascript">
    var __displayPopUp = {!condition_from_extension};
    var __taskId = "{!Id}";
    var __visualforcePageLocation = "{!VF_page_URL}";

    if(__displayPopUp === true) {
      alert('Condition Not met. Please enter in info.');
      window.location.href = "/" + __taskId;
    } 
    else {
      window.location.href = __visualforcePageLocation;
    }
  </script> 
</apex:page>

Extension:

public class TaskExtension
{
    // constructor
    public TaskExtension(ApexPages.StandardController stdController) { }

    // properties
    public String VF_page_URL { get { return Page.YOUR_VF_PAGE_NAME_HERE.getUrl(); } }
    public String condition_from_extension { get { return 'true'; } }
}
share|improve this answer
 
I'm getting an error- Error: Unknown property 'TaskStandardController.VF_page_URL' –  nivyaj Jun 12 at 17:26
 
I would absolutely not recommend checking that popup value as a string. Using double-equals (==) to compare objects is always a bad idea because it does implicit type conversion. If you are looking to compare boolean types, use boolean types. The purist way to do so is using JSENCODE() and the triple-equals operator. var __displayPopUp = {!JSENCODE(method_from_extension)}; and if(__displayPopUp === true) { where method_from_extension returns a boolean type. The benefits of JSENCODE here are negligible, but the security scanner will show a potential threat without it. –  Mark Pond Jun 12 at 17:27
 
Nice, I didn't know about JSENCODE. Thanks. –  Matthew Keefe Jun 12 at 18:30
 
@nivyaj did you add the extension code to your custom extension? –  Matthew Keefe Jun 12 at 18:33
 
JSENCODE doesn't work well with Boolean data types. It requires text. I updated the answer. –  Matthew Keefe Jun 12 at 19:05

Well there are a lot of different ways to approach this, but within the style you are following you could have something like

else {
    window.location = "{!urlForSuccessCondition}";
}

and in your controller extension have a method

public String getUrlForSuccessCondition() {
    PageReference nextPage = .... // Whatever logic you use to get the next page
    return nextPage.getUrl();
}
share|improve this answer
 
I'm getting an error- Error: Unknown property 'TaskStandardController.urlForSuccessCondition' –  nivyaj Jun 12 at 17:18
 
Did you add the function called getUrlForSuccessCondition to your controller extension? –  Doug B Jun 12 at 21:51

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.