Recently a reader had an interesting problem. Her client needed a prompt page from which they could open different reports. As it stands now, the client needed to select their prompts, hit the finish button, and then click on the links. She wanted a way to skip the second step – select the prompts, and have those values pass in the drill through.
With the prompt API, and a bit of JS, this is incredibly easy.
The first step is to identify the target report. You can get the report search path through the properties. Click on the “View the search path, ID and URL Location” link, and a window will pop up with the path and other details.
Notice the double quotes? That can potentially cause trouble with the JavaScript, so I recommend using a URL Encoder: /content/folder[@name=’JavaScript Examples’]/folder[@name=’Drill Target’]/report[@name=’Target’] becomes %2Fcontent%2Ffolder%5B%40name%3D%27JavaScript%20Examples%27%5D%2Ffolder%5B%40name%3D%27Drill%20Target%27%5D%2Freport%5B%40name%3D%27Target%27%5D
The example I’m posting will be using the URL Encoded version.
The next step will be the JavaScript:
<script> var paulScripts = {} , oCR = cognos.Report.getReport("_THIS_") , gateway = window['oCV_THIS_'].getRV().getCV().sGateway , report1 = decodeURIComponent("%2Fcontent%2Ffolder%5B%40name%3D%27JavaScript%20Examples%27%5D%2Ffolder%5B%40name%3D%27Drill%20Target%27%5D%2Freport%5B%40name%3D%27Target%27%5D%0A%0A"); /* function paulScripts.URLEncode - Paul Mendelson = 2013-10-21 * Based on the JSONEncode function, this will return the use/display values of * a prompt in a format that can be based via POST or GET. * usage: 'p_Parameter1='+paulScripts.URLEncode(paulScripts.getControl('MyPrompt')) */ paulScripts.URLEncode = function(promptControl){ var urlData = '<selectChoices>' , aPromptValues = promptControl.getValues() , nonRange = [] , range =[]; if (aPromptValues.length == 0) {return false} for (var j=0; j< aPromptValues.length; j++) { var promptValue = aPromptValues[j]; if (promptValue.use) {// Non Range value nonRange.push("<selectOption useValue='"+promptValue.use + "' displayValue='"+ promptValue.display + "'/>"); } else { // Range value var rangeStart = promptValue.start , rangeEnd = promptValue.end , start, end, startDisp, endDisp; if (rangeStart && rangeEnd) { //has both Start and End (bounded) rangeStart.display?startDisp = " displayValue='"+ rangeStart.display+"'":""; rangeEnd.display?endDisp = " displayValue='"+ rangeEnd.display+"'":""; range.push("<selectBoundRange><start useValue='"+rangeStart.use +"'" + startDisp +"/><end useValue='"+rangeEnd.use +"'" + endDisp +"/></selectBoundRange>") } else if (rangeStart && !rangeEnd) {//unboundedEndRange rangeStart.display?startDisp = " displayValue='"+ rangeStart.display+"'":""; range.push("<selectUnboundedEndRange><start useValue='"+rangeStart.use +"'" + startDisp +"/></selectUnboundedEndRange>"); } else if (!rangeStart && rangeEnd) { rangeEnd.display?endDisp = " displayValue='"+ rangeEnd.display+"'":""; range.push("<selectUnboundedStartRange><end useValue='"+rangeEnd.use +"'" + endDisp +"/></selectUnboundedStartRange>"); } else { alert ("Range not set."); } // end if } // end if } // end for if(nonRange.length>0) urlData += nonRange.join(); if(range.length>0) urlData += range.join(); urlData+='</selectChoices>'; return urlData.replace(/&/g,'&'); } /* function paulScripts.getPrompts * Paul Mendelson - 2013-10-21 * Retrieves all the prompts on the page and returns the parameter values */ paulScripts.getPrompts = function(){ //find all of the controls and stick them in aPromptControls. var aPromptControls = oCR.prompt.getControls( ), prompts = []; //loop through the controls - finding the name and values and sticking them into the names array for(i=0;i<aPromptControls.length;i++) { if(aPromptControls[i].getValues().length == 0) continue; prompts.push(aPromptControls[i].getParameterName()); prompts.push(paulScripts.URLEncode (aPromptControls[i])); } return prompts; } paulScripts.runReport = function(reportName,reportPath,gateway, prompts){ var basicInfo = [ reportName, "toolbar=no,height=200,width=400" , 'ui.gateway' , gateway , 'ui.tool' , 'CognosViewer' , 'ui.action' , 'run' , 'ui.object' , reportPath , "run.prompt" ,"false" , "cv.toolbar" ,"false" , "cv.header" ,"false" , "run.prompt", "false" ]; basicInfo=basicInfo.concat(prompts); return cognosLaunchInWindow.apply(this,basicInfo); } </script>
Let’s go through the functions.
var paulScripts = {}
, oCR = cognos.Report.getReport(“_THIS_”)
, gateway = window[‘oCV_THIS_’].getRV().getCV().sGateway
, report1 = decodeURIComponent(“%2Fcontent%2Ffolder%5B%40name%3D%27JavaScript%20Examples%27%5D%2Ffolder%5B%40name%3D%27Drill%20Target%27%5D%2Freport%5B%40name%3D%27Target%27%5D%0A%0A”);
We’re defining the variables on the page. Notice that I’m hardcoding the report search path into a variable here. There’s actually no reason to do that, we could put it directly into the function, or put it into another prompt, or even insert it into a table and build a list with each row calling another report.
The gateway variable is pulling the gateway from the report you’re running. Another gateway could be entered here, if needed.
The paulScripts.URLEncode will take a prompt control, process the selected values, and spit out a string that Cognos can use to populate parameters.
paulScripts.getPrompts will look for every prompt on the page, and create an array of the parameter names and the selected strings. This makes updating the report simple – you won’t need to modify and JavaScript when adding or removing prompts.
paulScripts.runReport is the last function. It uses the cognosLaunchInWindow function to run a report in the specified window, using the specified search path, on the specified gateway, with the results of the paulScripts.getPrompts function.
To use it, all you need to do is creating an onclick event like this:
<input type="button" onclick="paulScripts.runReport('_blank',report1,gateway, paulScripts.getPrompts())" value="Run Report 1"/>
And finally, we can test it:
This report was built on 10.2.1, but should work on all versions 10.2 and up.
Passing Parameters Without Refresh (4817 downloads)
Hi CognosPaul, Could you please help me get this to work in cognos 10.1 ?
The output opens but, i dont see the rezise option on the right hand top corner.
Thanks
Naga
Hi Naga, the cognosLaunchInWindow function contains an abstraction of window.open. The second parameter, where I put toolbar=no, controls the appearance of the new window. Try setting that to ” or adding resizable=yes. Check out the w3schools article on window.open for more options.
Thanks a lot Paul, it worked. But how do get to see the cognos options on the right hand corner like import to excel incase the user wants to view the report in excel or pdf, re-run just in case they try to re-run the report from the same window rather than going to the parent report.
Dear Naga – You may try placing a button and provide drill-thru that exports the HTML contents to xls or to pdf, if the window does not have export option in the toolbar. Hope this helps.
The options to control the appearance of the toolbar is in the cognosLaunchInWindow function. Simply remove the ,”cv.toolbar” ,”false” line, or set that to true. Also, you could run the target report directly in Excel by adding ,”run.outputFormat”,”spreadsheetML” or in PDF with ,”run.outputFormat”,”pdf”.
Thanks for all your help Paul.
Hi Paul, I tried the above JS in my report. It works fine, but when I select an item in a prompt value whose usevalue/display value contains character & , the parameter isn’t passed to the target report. The report is TM1 cube based report, but I think it should work the same. Can you advise how this can be fixed? Thanks
Hi Antal,
The problem is with the way Cognos handles ampersands. It turns out they need to be escaped:
replace the return urlData with:
return urlData.replace(/&/g,’&’);
And I’ll fix the JS in the post.
Hi Paul,
How to do the same thing when the prompt value has ‘ (single quote). Parameter value doesn’t get pass when the value has single quote. Thanks.
Hi,
My report prompt page is having two multiselect optional checkbox prompt. if prompt one is checked then should display Next button. if user selected both check boxes then Finish should display and Next should not display.
This sounds like a good job for the Prompt API. Have both next and finish buttons hidden inside divs with style=”display:none”. Use the prompt api validator to check both prompts, and conditionally unhide the correct div as necessary.
Hi Paul,
I have tried your above JS, i get the drill down report to open up and run, but no parameters get passed to the report? How would your script know what parameters from initial report map to the drill down parameters? do the parameters names have to match, is that how it works? (i tried this and it didn’t work).
Also, do you have a way to refresh the prompt parameters? i have a need in a report to drill down into another report, currently we have a button which confirms/repromps/refresh page, but it takes a while since the SQL behind the table list is slow. I used a JS which can pull the selection of he prompts in a list, but nothing gets passed down to drill through report as it isnt saved into memory until we push the reprompt button.
Is there a JS which can just refresh only the prompts and not the entire report?
Hi Paul,
I have tried this JS in Cognos 11 and my report has multiselect value prompts and radio buttons. And also am using data source as SSAS cube. So all parameters are in MUN’s. But when i use this JS , it’s opening in new window but still asking parameters to pass to drill-through report. Can you please help me in this matter.
Many Thanks !
Kishore.
Are you trying to run an interactive or non-interactive report? C11 has a new mechanism for passing parameters. Take a look here: https://www.ibm.com/support/knowledgecenter/en/SSEP7J_11.1.0/com.ibm.swg.ba.cognos.ca_gtstd.doc/c_wig_cr_promptparameter_optionl.html
Hi,
Thanks for your reply. I want to pass Cognos 11 parameters ( MUN values in the URL ) to child report to dynamically ( interactive ). Because my source is SSAS cube. Please help me.
Many Thanks!
Kishore