Our Blog

Resetting prompts to default values in Cognos

There are times when users may want to reset their prompt selections. If you have a dozen prompts, it can be a chore to manually reset each prompt control. With the new Prompt API in Cognos, resetting all the prompts, with a single click, is a very simple task.

By looping through the results of the getControl, applying clearValues to each control, you can clear each prompt:

var paulScripts ={}

, oCR = cognos.Report.getReport("_THIS_");

paulScripts.clearPrompts = function(){

var ctrls = oCR.prompt.getControls()

, len = ctrls.length;

for(var i=0;i<len;++i){

ctrls[i].clearValues();

}

};

That will remove the selected values from every prompt. But what if want to reset to the default values, or you want to skip certain prompts? Instead, it would be better to create a function that will accept an array of controls, and their correct defaults.

First we have the prompt getting.  Easy enough.

paulScripts.getControl = function(promptName) {

return oCR.prompt.getControlByName(promptName);

};

Next we have a function that takes a prompt name, and the default values. If there isn’t a default value, it clears it. We also need to have a special case to clear a date prompt. For some reason when the Prompt API attempts to clear a date prompt, it simply sets it to today’s date. This isn’t so great in cases where you actually need it to be null.

paulScripts.resetPrompt = function(promptName,defaultValue){

if(!defaultValue) {

paulScripts.getControl(promptName).clearValues();

}

else if(defaultValue=='clearDate') {

document.getElementById('txtDate' + paulScripts.getControl(promptName)._id_).value='';

}

else {

paulScripts.getControl(promptName).setValues(defaultValue);

}

}

Now the function that we use to reset the prompts.

/* paulScripts.resetPrompts
* Paul Mendelson - 2014-03-30
* This will take an array containing JSON describing the default values for each
* prompt. Passing false will resolve to clearing the prompt. This uses the the
* standard Prompt API JSON, so {'use':'stuff'} or {'start':{'use':'2012-02-01'}} etc.

*/
paulScripts.resetPrompts = function(promptValues){

var promptLen = promptValues.length;

for (var i=0;i<promptLen;++i){

paulScripts.resetPrompt(promptValues[i].name,promptValues[i].defaults)

}
}

We call that function by passing it an array of prompt names, and the default values if any.

paulScripts.resetPrompts(

[

{'name':'singleText','defaults':[ {'use' : 'Default text for the singleText'} ]}

, {'name':'multiText','defaults':

[{'use' : 'Default 1 for the multiText'}

, {'use' : 'Default 2 for the multiText'}

, {'use' : 'Default 3 for the multiText'} ]}

, {'name':'singleValue','defaults':false}

]
);

1. Text Prompts

In that example we have three prompts, singleText, multiText, and singleValue. The name parameter is the name of the prompt control. Notice that the defaults for the singleText and multiText prompts are arrays, this is actually the JSON that Cognos uses to populate the prompts. The singleValue prompt is being passed false, which the resetPrompt function will recognize as the command to clear the prompt.

We could also reference an existing prompt on the page to set the default:

paulScripts.resetPrompts(

[
{'name':'multiValue','defaults':paulScripts.getControl('multiValueDefaults').getValues()}

]
)

2. getting from another prompt

In that case, there is another prompt control on the page called “multiValueDefaults” that contains the correct values for the multiValue prompt. Instead of hardcoding the default values, we can use that to get them. getValues(true) would use all of the values in the prompt, even the ones not selected.

Date prompts can also be used updated with this method. First let’s define a simple date library:

// simple date libraries
// please don't use these. There are plenty of really awesome date libraries. As long as the library can return yyyy-mm-dd you can use it.
paulScripts.date={};

paulScripts.date.getSunday=function () {

var td = new Date()

, dw = td.getDay();

return new Date(td.setDate(td.getDate() - dw));
};

paulScripts.date.getSaturday=function () {

var td = new Date()

, dw = td.getDay();

return new Date(td.setDate(td.getDate() + (6-dw)));
};

Date.prototype.iso8601= function() {

var yyyy = this.getFullYear().toString()

, mm = (this.getMonth()+1).toString()

, dd = this.getDate().toString();

return yyyy + '-' + (mm.length==2?mm:'0'+mm) + '-' + (dd.length==2?dd:'0'+dd);
};

And then we can reference those functions:

paulScripts.resetPrompts(

[

{'name':'singleDate','defaults':'clearDate'}

, {'name':'dateRange','defaults':[

{'start':

{'use':paulScripts.date.getSunday().iso8601()}

,'end':

{'use': paulScripts.date.getSaturday().iso8601()}

}

]}

, {'name':'multiDateRange','defaults':

[

{'start':

{'use':paulScripts.date.getSunday().iso8601()}

,'end':

{'use': paulScripts.date.getSaturday().iso8601()}

}

,{'end':

{'use': paulScripts.date.getSaturday().iso8601()}

}

,{'start':

{'use':paulScripts.date.getSunday().iso8601()}

}

]
}

]

3. date prompts

The singleDate prompt will be nulled. The date range prompt will get Sunday to Saturday, while the multi date range prompt will get the sun-sat range, everything after Saturday, and everything up to Sunday.

Stick the resetPrompts() function in a button, and sit back with the warm glow of a job well done.

Reset prompts report.xml
Reset prompts scripts.xml

This entry was posted in Business and Strategy, Technical Concepts and Ideas, Tips and tricks and tagged , , , , . Bookmark the permalink.

Comments are closed.

PerformanceG2 Menu