How to Set an Active Report Variable in JavaScript

Yesterday I was asked if it was possible to set an Active Report variable in a Cognos Active Reports using JavaScript and the short answer was “No”. I have not yet found a way to do so directly. If you know how to do this, please share! However, changing A/R prompts is another matter entirely and since you can change the prompt, you can have the prompt set the variable.

I successfully did this a couple of years ago, and realize that I should have shared it. I guess better late than never. Here’s a report example that shows you how it’s done:
Alter an Active Report Variable.xml
Once you open the report in Cognos Report Studio, you’ll need to change the package and the data source in the SQL query object. The SQL’s written for Oracle, but you can alter it for your own database.

The report has several elements. First is the image tag that is necessary to implement the functions and the JavaScript that must run when the report first opens. The use of the image tag is explained in greater detail in my post Creating JavaScript Functions in Active Reports. This contains three functions:

  • getElementByARId – This function, along with some start-up code at the end of the onload event allows us to quickly find objects we need to reference. An Alternative to getElementById for Active Reports will describe this in full.
  • fireMouseDown – Takes a node in a radio button and simulates a click on that node. The three major browsers all need to do this differently.
  • selectRBListValue – Finds the radio button group you want to affect (list), searches for the node you want to select (node), and attempts to click on that node. Since the timing of the creation of the many elements in an Active Report can be tricky, it attempts to click on the node four times.

The other elements are:

  • A div tag (and closing tag) surrounding a button. This describes what to do when the button is clicked.
  • A data drop-down list to show the results of the button being clicked.
  • A div tag surrounding a data radio button group that we will use to manipulate the variable. This tag and its contents are hidden from the user’s view.

It looks like a lot, but once you understand how to implement it, this technique is fairly simple to deploy. It may have taken a long time for me to give this to you, but on the bright side, I can say that it has been tested thoroughly almost every business day for the last two years.