I have a Visualforce Page with inputs, these input values are passed to a controller, query results are returned, and results are displayed in a datatable on the same VFP with inputs.

If the list I am populating in the controller is a size of 1.. I would like to open a primary tab of that object. If the list I am populating is a size of > 1, I want the results to continue displaying on the VFP with inputs. How do I accomplish this?

My logic was to was a use conditional to somehow callback a JS function on the VFP page, but that has been proven incorrect.

  • 4
    This is an X-Y Problem. There is a proper way to do whatever it is you're trying to do, but that isn't this way. Try asking a question about the original X, include as many details as possible, because this question is not clear at all.
    – sfdcfox
    Commented Jul 27, 2018 at 17:26
  • I have modified my question to be oriented to my overall objective.
    – S.B.
    Commented Jul 27, 2018 at 17:57

1 Answer 1


You can't directly call JavaScript from Apex. JavaScript executes on the client side, in the browser; Apex is executed on the Salesforce server as part of the page render process or in response to a request initiated by the client.

What you can do is initiate an operation, on the client side, in JavaScript, that calls out to the server-based Apex controller to obtain data or perform some action, and include a callback that executes with the result of that server operation.

That's not what the code snippet you found does (it actually doesn't do much of anything). There's several different ways to achieve this in Visualforce, including:

  • JavaScript Remoting, for data access and for complex client-server communication. Example is included on that documentation page.
  • Visualforce Remote Objects, for simple data access only.
  • The <apex:actionSupport> component, which fires a server action and can optionally execute JavaScript specified in an oncomplete attribute and rerender Visualforce page elements.
  • The <apex:actionFunction> component, which allows you to define JavaScript functions you can call in your scripts that invoke server-based Apex controller methods, and also supports an oncomplete handler and rerendering of Visualforce page elements.

Which of these tools you should be applying depends completely on your actual objective here, and may require you to make significant changes to how you understand the order of operations between the client and server code elements.

  • Action Function may be what I want. Invoke the action function, run a soql query for my object, if size >1, run my original action function, if size = 1 pass back the id of that object to the vfp and use sforce console tab method to open the primary tab? I'll give this a shot unless there is a better method.
    – S.B.
    Commented Jul 27, 2018 at 18:06
  • I am not intimately familiar with Console programming, but it sounds to me like you probably want either an Action Function or JavaScript Remoting.
    – David Reed
    Commented Jul 27, 2018 at 18:07
  • So the action function seems to be headed in the right direction, but I have a get/set boolean variable that I set to true within the controller. I debug and see the variable is set to true in the controller. I have an onComplete at the end of the action function that invokes a JS function on the VFP. However, when I reference the variable in that function it always returns false. I am setting it var booleanHolder = '{!booleanInController}'. What am I missing?
    – S.B.
    Commented Jul 27, 2018 at 19:33
  • 1
    You might need to open a new question and include your code for this one. I don't have a solution for you just based on the description - would need to see the code.
    – David Reed
    Commented Jul 27, 2018 at 20:00
  • 1
    All I needed to do even for my initial logic (the action function approach works better regardless) was to wrap my javascript function in an <apex:outpanel> and rerender the output panel Id instead of rendering the enclosing form or page. Solution found here: salesforce.stackexchange.com/questions/73424/…
    – S.B.
    Commented Jul 27, 2018 at 20:29

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