Monday, January 29, 2007

How to automatically select the first prompt value

In the dashboard application I blogged about in the previous post, I now have added a prompt in the main portal report page showing the current period. When a new year arrives, the users often don't just want to see the few actual details if the new year, but also want to look back into the details of last year. This requirement calls for a prompted list of values.

Here is a screenshot of the actual prompt (red circle). The first value ('2007 t/m januari') is automatically selected. Mind there is NO prompt page! This is the first 'report' page that is presented to the user.



In Cognos it is possible to provide a Default Selection but this contains a list of static values. I cannot use this a each month there is another first value to be selected. So what I've created is a Value Prompt object presenting its values by a query with Use Values and Display Values, and have added an HTML item with a piece of JavaScript that selects the first value. The HTML item is added just after the Value Prompt object.

The JavaScript code in the HTML items looks like this:

<script type="text/javascript">
function init()
{

if

(document.formWarpRequest._oLstChoicesPeriode.options[3].selected == false)

{

if

(document.formWarpRequest._oLstChoicesPeriode.options[2].selected == false)

{

document.formWarpRequest._oLstChoicesPeriode.options[2].selected = true;

listBoxPeriode.autoSubmit();

}

}
}
</script>
<body onLoad=init()>


The first value to be selected in the list is actually the second value in the list object, which JavaScript sees as a list of three values in total. If no value is selected initially (third and second selections are false), the second value is selected by the script.

Tip: Did you know that the Cognos role "Consumers" needs to be given read permission to the Report Studio capability "HTML Items in Report" in order to get HTML item objects working in reports for Consumers? Please mind this and check the Tools > Capabilities in Cognos Connection!

12 comments:

Unknown said...

Hello Marc,
It's a great blog.However after applying the java script i cannot see the run with option toolbar in cognos viewer...i dont understand it shows it in ur screen shot. Have you done any other setting to make this toolbar visible.Please reply its urgent.Best regards Tarachand

Marc said...

Hi Tarachand,

Did you see the Run with Option toolbar before applying the java script? In my opinion, the java script does not change anything to the toolbar.

And I haven't done any other setting, except not to show the header line by adding &nh=1 to the url. But this doesn't affect the toolbar. Toolbar buttons can be hidden by changing system.xml, see the admin documentation for this.

Please mind this example applies to ReportNet, I haven't tried it in Cognos 8 yet.
Best regards, Marc.

Unknown said...

Hello Marc,

Thanks for replying. Yes the toolbars are visible before applying the Java script.Infact the init() in the script is making it hide...i guess it somehow overrides the cognos init() funtion and changes the defalt properties while rendering the cognos viewer page... i cannot copy ur code here as it doesnot allow HTML tags here...just have a look at ur code...

If i remove the init() i can see the toolbars, but the value prompt only shows the latest value doesnot submit the value it shows, as a result the filter is not applied and data for all the values are shown in the report,


This is happening coz listBoxPeriode.autoSubmit() does not work without init(). is there any work around with the script. i am working in Cognos 8, however it is the same with reportnet.

Best regards,
Tarachand

Marc said...

Hi Tarachand,

As I'm not familiar with the cognos init() function and not experienced enough with JavaScript to help you, I'd like to refer you to the Cognos Knowledge Base, as I also have my information from the KB, in this case document 1036117 (search 'javascript' and 'selected').

If this does not suffienciently help you, maybe you should report your case to Cognos Support.

Best regards, Marc.

Tom Mayer said...
This comment has been removed by a blog administrator.
divjeev said...

very detailed blog.you have listed down even the minutest of details.Thanks a lot

Anonymous said...

Hi Marc,
i just would like to say thank you for posting your knowledge here. I just want you to know that you've helped me ALOT today!! i was struggling in creating default prompts and automate submission of it, but after reading your method everything is working real well now. thanks again!

randy

Marc said...

Hi randy,

Thanks for your feedback! Keeps me motivated in informing you all with my experiences every once in a while.

Anonymous said...

Good stuff, I can't believe that Cognos doesn't make a standard functionality for this.

I'll have to dig arround but a while ago I wrote some code which had a work arround.

It basically loaded a parameter value with the default value. In javascript you can pickup the value of the paramter and pre-select the option with the value loaded from the parameter.

I did get it working but its a while ago.

CG said...

In Cognos Active report I have to set the current year for a Value promt in Check Box group format. Do you have HTML code for this?

Unknown said...

Thanks for Information COGNOS Online Training

Unknown said...

It was very nice article and it is very useful to Cognos learners.We also provide Cub training software online training.