NW Business Intelligence

thoughts on technology, B.I., and more…

Archive for June, 2007

Cognos default prompt selection

Posted by Brad Greene on June 5, 2007

Ever wanted to select the first value in a Cognos Report Studio value prompt? Here is some Javascript that should do the trick for you. It includes some very basic error handling which Cognos knowledge base articles overlook.

First, I assume you know how to use Report Studio already. Second, this script was written to work with Cognos 8 [ed. in 2007]. It may work fine with earlier releases but I did not test it for those. Third, if you use this script you take responsibility for it not me. 

Step 1 – Go to your report’s prompt page and select the Value Prompt you want to work with. I assume that you are populating it with a query against some data source although this should work with a static list as well.

Step 2 – At the bottom of the properties list under Misc. is the Name property. Enter a name that makes sense. Shorter is better but it needs to be unique for the prompt page.

Step 3 – Now insert an HTML item somewhere on the prompt page. If you don’t know what this is STOP and read the User Guide.

Step 4 – Enter the script text below as a framework to work with. You will have to edit some items based on the name(s) you have given the value prompt objects. Include only the comments you need for best performance.

function init()
{
// replace ValuePrompt1 with the name you gave yours in 3 places document.formWarpRequest._oLstChoicesValuePrompt1[0].text = 'Please select a value:'
try {
document.formWarpRequest._oLstChoicesValuePrompt1.options[2].selected = true;
}
catch(err)
{
// do nothing, this is probably a null list returned by query
}
listBoxValuePrompt1.checkData();
// the code is repeated for each prompt, replace ValuePrompt2 with the name you gave yours in 3 places
document.formWarpRequest._oLstChoicesValuePrompt2[0].text = 'Please select a value:'
try {
document.formWarpRequest._oLstChoicesValuePrompt2.options[2].selected = true;
}
catch(err)
{
// do nothing, this is probably a null list returned by query
}
listBoxValuePrompt2.checkData();
}

Comments and Summary

The script selects the first value returned for the list from the query. Don’t be confused by the array pointer being 2. First of all the array, or list of prompt values, starts with 0 and Cognos inserts 2 entries of its own so you are in effect, skipping over those first 2 to the third (or position 2 in the array).

The other useful addition here is the “try / catch” lines. This pair is wrapping around the line that tries to set the prompt to the value you want. It is possible that a query might not return anything and the list would not have 3 entries. If that happens and the script tries to select position 2 (options[2].selected = true) Javascript will throw an error and the script will halt execution. Any code you have after the one that fails will not get executed. We prevent that from occurring by”catching” the error and, in this case, doing nothing. If there were a need to alert the user you could insert an alert(”) function to display a pop-up.

The final point to note is that all the prompt manipulation should be done in one script. The example above shows 2 being altered but it could be 1 or 10 depending on your requirements. Just copy the set of lines for each prompt and edit the prompt name.

Last. Test, test, test. Try your report in all supported browsers (IE, FF) too. Don’t replicate the code to a bunch of other reports until all testing has been done. You’ll save a lot of editing that way.

Posted in Tech Tips | Comments Off on Cognos default prompt selection