Tuesday, March 1, 2011

Using Javascript to change first line and default selection in a Value Prompt in Cognos 8

This is my first expierence using Javascript in Cognos 8 Report. Although, I have not find the solution myself,  I think it is worth to blog it for future usage.
When using a value prompt select box, the title usually comes from the Query Subject header which is often the database column name. Cognos automatically assigns column name from package as title in the prompt select box. As a result, when users build or run the report, they want the value prompt title to be more meaningful than a col001 or whatever column name the DBA has assigned when designing the database. It is worth to mention that in a "perfect world" where all Framework Manager packages are designed correctly, this kind of issue never occur.
 
The first step is to embed the Value Prompt into a Span HTML markup. You do this by drag and drop 2 HTML elements from the Toolbox tab before and after the value prompt list box.



 The HTML code will be for the first (left) element:

and for the 2nd HTML element (right):

The second step is to add a 3rd HTML element at the end of the form, at the right of the Finish button.


This 3rd HTML element will contain Javascript code which dynamically changes the List Box title property and if needed, the List Box default selected value.

The code is self-explanatory and looks as follows:

<script type="text/javascript">

var theSpan = document.getElementById("Span1");
var theSelect = theSpan.getElementsByTagName("select");
theSelect[0].options[0].text = 'A more meaningful title';

theSelect[0].options[2].selected=true;
canSubmitPrompt();

</script>
The theSpan variable stores the SPAN markup which is used to capture the select box into the theSelect variable. The next line changes the title using the text property of the first option (options[0]). The following line sets the 3rd option (options[2]) selected property to true to make it a default selection.
Finally the canSubmitPrompt() activates the Finish button.