Cognos Analytics Custom Control JavaScript - How to Auto Submit a Page
- Data Nerd Josh

- Dec 20, 2022
- 2 min read
Updated: Sep 14, 2023
Opposed to hardcoding JavaScript into each report using HTML item objects, one of the amazing features of IBM Cognos Analytics is the ability to store your JavaScript files on the Cognos server and reference them across a multitude of reports. This allows you to change a piece of JavaScript in a single location opposed to updating each and every report. IBM has created documentation for adding JavaScript to reports.
With that introduction out of the way, let's get to the JavaScript example being discussed and shared.
One of my favorite things to do in IBM Cognos Analytics is to create a prompt page that is hidden to the end user but it's actually doing work behind the scenes to pull in some kind of information that'll be passed over as default parameters when the report runs. An example of this might be a situation where you need to run a query behind the scenes to to see what the latest data is in your table and then set that as the default date in your date prompt.

When you do something like this, you obviously will want this page to auto-submit instead of requiring the user to intervene and click the submit button on that prompt page. This is where the magic of the Custom Control JavaScript comes into play. Below is an example of a piece of JavaScript that can auto-submit a page after it has been saved to your Cognos server and pulled into your report by inserting a Custom Control from the Toolbox.
define( function() {
"use strict";
document.head.appendChild( document.createElement( "STYLE" ) ).innerHTML =
"TABLE.clsViewerProgress," +
"TABLE.clsViewerProgress *" +
"{" +
"visibility: hidden !important;" +
"}";
function Control()
{
};
Control.prototype.show = function( oControlHost )
{
var fDefaultSeconds = 0.05;
var fSeconds = ( oControlHost.configuration ? oControlHost.configuration.Seconds : fDefaultSeconds ) || fDefaultSeconds;
this.m_iTimer = setTimeout( oControlHost.finish.bind( oControlHost ), Math.round( fSeconds * 1000 ) );
};
Control.prototype.hide = function( oControlHost )
{
this.cancelRefreshTimer();
};
Control.prototype.destroy = function( oControlHost )
{
this.cancelRefreshTimer();
};
Control.prototype.getParameters = function( oControlHost )
{
// WARNING: Do not remove this method.
// Pages that don't contain prompt controls are cached by the viewer. The existence of a getParameters
// is used as the indicator that the custom control is prompt-like and will prevent caching.
};
Control.prototype.cancelRefreshTimer = function()
{
if ( this.m_iTimer )
{
clearTimeout( this.m_iTimer );
this.m_iTimer = null;
}
};
return Control;
});For any of the Custom Control JavaScript that you use, you will have to set some properties on the reporting side. Here are my recommended properties:
JS Configuration Example:
{
"Seconds": 0
}
Notes: Recommend setting UI Type to 'None'
.png)




Comments