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"  >
          var displayPopUp = {!method_from_extension};
            if(displayPopUp == true ){
                alert('Condition Not met. Please enter in info'); 
                window.location ="/{!Id}"                   


3 Answers 3


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.


<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;


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'; } }
  • I'm getting an error- Error: Unknown property 'TaskStandardController.VF_page_URL' Jun 12, 2013 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, 2013 at 17:27
  • Nice, I didn't know about JSENCODE. Thanks.
    – Matt K
    Jun 12, 2013 at 18:30
  • @nivyaj did you add the extension code to your custom extension?
    – Matt K
    Jun 12, 2013 at 18:33
  • JSENCODE doesn't work well with Boolean data types. It requires text. I updated the answer.
    – Matt K
    Jun 12, 2013 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();
  • I'm getting an error- Error: Unknown property 'TaskStandardController.urlForSuccessCondition' Jun 12, 2013 at 17:18
  • Did you add the function called getUrlForSuccessCondition to your controller extension?
    – Doug B
    Jun 12, 2013 at 21:51

Hey Nivyaj using a getmethod will help u pass parameters which are not part of Std controller and obj. But I have a diff issue here. I am inserting child records after clone link in the related list which calls a method in extension and then sets the variable that I wd require in JS variable to create the URL and send user to next page which wd need the ID of record created through clone.

  • Hi, and welcome to SFSE. If you have a new question, you need to ask it in a new question. Please edit your current one to fit the Q&A style of SFSE.
    – Saariko
    Jan 13, 2014 at 20:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .