Search code examples
business-intelligencecognos

How to count selected values of a prompt value parameter in cognos


I have a prompt page and a page with a list. The user selects the values from the prompt page and the report page is then generated. A usual report.

The prompt page has a multi select list box with 20 values.

What i want to do is: When the user selects up to 3 values from the list box then in the report header show the values with ParamDisplayValue('param1'), if the user select more than 3 show ") items selected" eg: "5 items selected"


Solution

  • You can use the Cognos JavaScript API to store the selection count in a hidden prompt for later use. Here are the steps:

    1. Create a new text prompt
    2. Fill in the new prompt's name property (called 'countPrompt' in the sample code below)
    3. Fill in the original prompt's name property (called 'multiPrompt' in the sample code)
    4. Add an HTML item to the bottom of the page
    5. Add the JavaScript that follows this list
    6. When you run the report the new prompt should change to reflect the number of items that are selected in the original prompt.
    7. Once you have verified that it's working you can hide the new prompt (set Visible: No)

    <script>
    var report = cognos.Report.getReport('_THIS_');
    var multiPrompt = report.prompt.getControlByName('multiPrompt');
    var countPrompt = report.prompt.getControlByName('countPrompt');
    
    multiPrompt.setValidator(validateMultiPrompt);
    
    function validateMultiPrompt(values) {
        if (values && values.length > 0) {
            countPrompt.addValues([{'use':values.length}]);
        } else {
            countPrompt.addValues([{'use':'0'}]);
        }
        return true;
    }
    </script>
    

    On your report you can have a report expression like so:

    if (ParamDisplayValue('param2') in ('1','2','3')) 
    then (ParamDisplayValue('param1'))
    else (ParamDisplayValue('param2') + ' items selected')
    

    This expression assumes that the multi-prompt is assigned param1 and the new hidden prompt is assigned param2.

    For this to work the multi-select prompt must be required. Otherwise, when nothing is selected your item would read '0 items selected'. This can be worked around by adding a render variable to the item which only renders the item when param1 is not missing or param2 equals '0'.