Dynamic default value for prompt

There is a report which allows users to select a shipment month. The Shipment Month dimension has values up to current month. However, users frequently select prior month. They want the prompt to have prior month selected by default.

Getting ready

Take any report that filters on Shipment Month Key. Add a value prompt to the Shipment Month Key on the prompt page.

How to do it...

  1. Select the value prompt and define its sorting such that the Shipment Month Keys are populated in descending order.
  2. Now, as we know the dimension is always populated up to the current month and values are sorted in descending order, we will write a code that selects second value from top by default.
  3. For that, add an HTML Item just before the Shipment Month value prompt. Define the HTML as: <span id = 'A1'>
  4. Add another HTML item just after the value prompt and define it as: </span>
    How to do it...
  5. Now add another HTML item to the prompt page footer, just after the Finish button.
  6. Define it as:
    <script>
    var theSpan = document.getElementById("A1");
    var a = theSpan.getElementsByTagName("select");   /* This stmt return an array of all value prompts within span */
    for( var i = a.length-1; i >= 0; i-- )  /* now loop through the elements */
    { 	var prompts = a[i];
    if( prompts.id.match(/PRMT_SV_/))
     {	prompts.selectedIndex = 3;  } /* This selects the second options from top */
    canSubmitPrompt();
    }
    </script>
  7. Execute the report to test it.

How it works...

The logic used here is that we first sort the months in descending order and then select the second option from top. As the values populated from the database are up to the latest month, the second value from top will be the previous month.

As mentioned at the beginning of the chapter, Report Studio prompt pages are similar to any other HTML pages with most of the controls being standard web controls. The HTML item in Report Studio is a powerful component which allows us to embed our own code within the page generated by Cognos.

When we put a JavaScript within an HTML item, it is automatically executed when the page loads.

SPAN

With Cognos 8.3, the report viewer architecture has been majorly changed. With CRN 8.1 and 8.2, it was a common practice to define a NAME or ID for the prompt controls and use that to manipulate controls at runtime through JavaScript.

However, version 8.3 onwards, the IDs of the controls are generated randomly and are not fixed. So, it is a little difficult to get hold of a control. For this reason, we have defined a SPAN around the control that we want to manipulate.

By wrapping the control within SPAN tags, we will reduce the scope of our search in JavaScript.

GetElementsByTagName

As we want to capture the Value Prompt within span, we search elements with select tag within the span A1.

If we want to perform same operation on multiple value prompts, we can put them all within same span. The GetElementsByTagName function returns an array of elements with the specified tag.

SelectedIndex

Once a value prompt object is captured in a variable, we can set its SelectedIndex property to set the selection to required value.

CanSubmitPrompt

In prior versions, we used the CheckData() function to submit the prompt value. This means Report Studio will accept the value and adornments will disappear. However, 8.3 onwards, we can use a global CanSubmitPrompt() function for the same purpose.

There's more...

A more suitable example of 'dynamic selection' will be iterating through the value prompt options and selecting one based on a condition.

You can use the Java functions to capture system date and accordingly work out the Prior Month. Then traverse through all the values and select an appropriate one. Similarly, you can iterate through all the prompt values and select the required entry based on value instead of hard-coding the selectedIndex to 3.

Please refer to one such example on the IBM website at this URL: http://www-01.ibm.com/support/docview.wss?uid=swg21343424

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset