Javascript in Report Studio
First of all, scripts may break when upgrading versions. There are new objects available to use and some taken away with each release. I will be talking about Cognos 8.3 for the purposes of this blog.
I want to show you a basic script that can be very useful. The idea for this came from a client who asked me to solve a problem. They had a report that had 2 optional prompts. The problem was some users were not making use of either. This was resulting in very large result sets being returned.
What we wanted to accomplish was requiring the user to choose a value from either prompt. It did not matter which one. To accomplish this I put two list prompts on a prompt page. One was called om (I am making use of the provided sample data) for order method and the other was called pl for product line. An html item was inserted near the top of the prompt page which includes my function.
<script type=”text/javascript”>
function checkNumSelected()
{
// The following 4 lines of code are needed in Cognos 8.3
var fW = (typeof getFormWarpRequest == “function” ?
getFormWarpRequest() : document.forms["formWarpRequest"]);
// if it’s undefined, check if it is running in a portlet.
if ( !fW || fW == undefined) { fW = ( formWarpRequest_THIS_ ?
formWarpRequest_THIS_ : formWarpRequest_NS_ );}
//setting up my variables
var countom=0;
var countpl=0;
var omcount = fW._oLstChoicesom;
var plcount=fW._oLstChoicespl;
// count how many choices were selected in the Order method prompt
for (var i=0; i < omcount.length; i++)
{
if (omcount.options[i].selected)
countom = countom +1 ;
}
// count how many choices were selected in the Product Line prompt
for (var i=0; i < plcount.length; i++)
{
if (plcount.options[i].selected)
countpl = countpl +1 ;
}
// If at least one option was selected in wither prompt call the promptbuttonfinish fuction
// If nothing was selected throw up an alert box with directions
if (countom >=1 || countpl>=1) {
promptButtonFinish();
}
else if (countom == 0 && countpl==0) {
alert(“You have not selected a value!\n Select a value in one of the prompts.”);
}
}
</script>
In order to call this function I deleted the finish button on the prompt page and created my own through an HTML item with the following code
//<input type=”button” OnClick=”checkNumSelected()” value=”Finish”