COGNOiSe.com - The IBM Cognos Community

IBM Cognos Analytics Platform => Cognos Analytics => Reporting => Topic started by: dougp on 13 Jul 2018 05:49:24 PM

Title: Controlling Prompts with JavaScript
Post by: dougp on 13 Jul 2018 05:49:24 PM
I want more control over my prompt pages.

For example, I want to set all of my prompts to "not required" and parameterized filters to "optional" because there is no one prompt that should be required.  However, I don't want to allow the user to download the internet.  They should have to use some prompts, just not specific ones.  Maybe I want them to filter the report by a city and a year, or maybe a country and a month.  If I require city, they can't see the whole country without selecting every city in that country.  Painful.

Or maybe I just want to always has my date prompt default to one week ago, or maybe the last work day before today.

I have much of my prompt script worked out, but here are a few items I'm having trouble with.


Does anyone else here have experience with this?  Do you have the same problem?  Are there other problems I haven't noticed yet?  Did you find workarounds?
Title: Re: Controlling Prompts with JavaScript
Post by: dougp on 05 May 2021 01:54:49 PM
Still no love...

I have a page module that includes the code below.  There is no effect on the Finish button.

I know I'm not the only one with this need.  Has anyone else here solved this?  Is there a different approach that will work?

this.oConfig = {"RequiredPromptCount":3};

if (this.oConfig.RequiredPromptCount) {
// loop through the prompts on the page
// increment a counter if a prompt is populated
// When the counter equals RequiredPromptCount, we're done.  Enable the Finish button.
// If the counter never grows to RequiredPromptCount, disable the Finish button.
// This should probably occur within the prompt validator function for every prompt.
//log("RequiredPromptCount:  ", this.oConfig.RequiredPromptCount);
var rpc = this.oConfig.RequiredPromptCount;
var ap = oPage.getAllPromptControls();
ap.forEach(function(p) {
//log("RequiredPromptCount:  Prompt", p.name);
// When the page loads, each prompt gets a setValidator function created.
p.setValidator(function () {
// While the user is interacting with the report, this code runs...
//log(p.name, "");
var c = rpc;
var i = 0;
var a = oPage.getAllPromptControls();
a.forEach(function(e) {
//log("    Prompt Validation:  Prompt" + e.name, JSON.stringify(e.getValues()));
//log("    Prompt Validation:  Prompt" + e.name, e.getValues()[0].use == undefined);
i += e.getValues()[0].use == undefined || e.getValues()[0].use == "" ? 0 : 1;
//log("    Prompt Validation:  Prompt" + e.name, i);
});
//log("PromptCount", i);
//log("RequiredPromptCount", c);
log("Valid Prompt", i >= c);
// ...but whether the function returns true or false has no effect on the Finish button.
return i >= c;
});
});
}
Title: Re: Controlling Prompts with JavaScript
Post by: dougp on 06 May 2021 10:33:01 AM
I worked this out in two different ways:

1.  Apparently I had solved this problem a while back, but forgot how I did it.  There's a file in my script library (https://github.com/dougpulse/Cognos/tree/master/CognosScripts) named PromptButton.js.  This includes code that does not allow the "Finish" action to take place without using the button, so it should work with auto-submit prompt controls that are intended to populate downstream prompts on the same page.  If auto-submit is intended to run the "Finish" action, that may not work.  It's also a little more flexible than (2), but that can easily be fixed.

2.  I created a new script based on something that I documented that I found here (http://www.cognoise.com/index.php/topic,34689.0/topicseen.html), but clearly, that's not the correct post.  (Maybe some Cognoise topic IDs got shuffled by a system restore?)  This one makes no accommodation for behavior when the auto-submit property is set.  This one currently only performs the "Finish" action, but could be enhanced to allow any action.  It is now in my library as PromptFinish.js.

Either one of these appear to meet my current need.  If you want to use these scripts, please keep in mind that extensive testing was not performed.  I don't know what they may do in a context other than where I am using them.