A List Prompt From an Actual List

A colleague recently used this technique on my current project, so I want to make sure that credit was given where credit is due.

From Paul’s Cognos Blog: Checkbox List Prompt

Advertisements

Changing a Value Prompt in Report Studio using JavaScript

There have been many occasions where I have needed to change the filters on a report by having the user click on data on the report.  For example if I had a listing of cars, class, and number sold, and the user clicked on “Sedan” under the Class column, I would want to filter the report to only show sedans.

In Cognos 8 Report Studio, I accomplished this by having  a drill-through on the Class column back to the same report, passing the data item into the Class prompt.  This was an imperfect solution for a number of reasons, but it was usually good enough.  However, Cognos 10’s introduction of Business Insight  made this technique a bit less than “good enough”.  If I were to add the report to a Business Insight workspace, clicking on “Sedan” would not update the report I was looking at.  Instead, it would open a new copy of the report in a new window.

So the drill-through technique is no longer an option. My new technique for accomplishing this is to use Javascript to change the prompt. The following technique works with Cognos 10.1.1 Report Studio and I believe it’ll work with Cognos 8 as well. However, remember that with any JavaScript used in Cognos, an update to another version of Cognos (even a minor one) could break it.

Set Up the Prompt

This technique requires that you add the prompt to your report page (regardless of whether or not you have a copy of the prompt on a  prompt page).  I like to put these near the top of the report so the user can immediately see what filters are being applied.  However, you can put it anywhere on the report page that you want. 

  1. Add the prompt to the report page
  2. Before the prompt, add an HTML item with the following HTML code:
    <div id=”VP_TestPrompt”>

    Give the div tag an appropriate ID.  If you have more than one prompt, give each one a unique name.

  3. After the prompt, add an HTML item to close the div tag:
    </div>
  4. After the closing div tag, add a third HTML item with the following function:
    <script type=”text/javascript”>
    function selectPromptByValue(divId, val) {
       var oVPCont = document.getElementById(divId);
       var oVP = oVPCont.getElementsByTagName(“select”);
       var i = oVP[0].options.length;
          while (i) {
          if (oVP[0].options[–i].value==val) {
             oVP[0].options[i].selected=1;
             promptAction(‘finish’);
             i=0;
          }
       }
    }
    </script>

    If you have multiple prompts, you will only need this HTML item once on the report page.  The first parameter tells it which prompt to reference.

Set Up the Link

Now you just need to call the function selectPromptByValue, passing in the name of the div tag (set in step 2 above) and the value you want to pass into the value prompt.
Ther are a couple of ways to do this.  For most reports you could use a hyperlink to call the function (ex: javascript:selectPromptByValue(“VP_TestPrompt”, “A”);).  However, this does not work when running the report from a Business Insight workspace because BI incorrectly assumes that the hyperlink (anchor tag) will be taking you to another web page.  This results in a warning about navigating away from the page.

Instead I use a simple text object and wrap it in another div tag with an onclick event.

  1. Add your text.  To make it look more like a hyperlink, you may wish to underline it and set the color to blue.
  2. Add an HTML item before the text:
     <div style=”cursor:pointer”; onclick=’javascript:selectPromptByValue(“VP_TestPrompt”, “A”);’>

    The first parameter should be the id of the div tag you set before the prompt.  The second parameter is the value in the prompt that you want to select.
    If the link is to be data driven (ex: in a list), then you will need to make the ULR Source Type a report expression and parse it together so that it looks like the above example.

  3. Add an HTML item after the text to close the div tag:
     </div>

Example Report

To see how this technique works, copy the text from this Word document into your clipboard, open Report Studio, and select Open Report from Clipboard.