COGNOiSe.com - The IBM Cognos Community

IBM Cognos 10 Platform => Cognos 10 BI => Report Studio => Topic started by: pcog on 06 Jan 2015 09:06:43 AM

Title: Java script for default month and year prompts
Post by: pcog on 06 Jan 2015 09:06:43 AM
Does anyone have a JS for default selection of recent full month ( or previous month) and current year?

I'm using a relational model and my report should have prompt to give more freedom for users to select other months and years. so, I can't put a filter for previous month and current year. btw, I'm using a value prompts for both.
and on another note, i have a question...can we schedule a report which have a java script code embedded in it? I read somewhere that it is not possible.

Thanks in advance for your time and help :)
Title: Re: Java script for default month and year prompts
Post by: BigChris on 07 Jan 2015 03:14:33 AM
Hi Pcog,

I don't use javascript I'm afraid so I can't give you a direct solution, but I think you can probably do what you're trying to do without it. Have a look at this blog post that I wrote to remind me when I need it...and to save me rewriting the filter code each time:

http://cognostips.weebly.com/blog/date-prompts (http://cognostips.weebly.com/blog/date-prompts)

C
Title: Re: Java script for default month and year prompts
Post by: pcog on 09 Jan 2015 09:49:50 AM
Thanks Chris for the reply and I really appreciate your time :)

I understand what you did there, but in my case I have 2 prompts (Month & Year).
for one instance user can select those prompts interactively and run the report. But, by default prompt should be current month and current year. so, these will change dynamically. That is the reason I was looking for any JS. I tried to write a small case stmt for use value or display value for prompts, but its not working :(

Thanks again for the help though :)
Title: Re: Java script for default month and year prompts
Post by: navissar on 11 Jan 2015 05:27:20 AM
There are two ways to go.
1. Add to the prompt a static value with 1 as the use value and "Current Year" as the display value. Set the filter to something like:
(?prmYear?=1 and [Year]=extract(year, current_date)) OR ([Year]=?prmYear?)
And repeat for month.
2. Javascript to set defaults. I can give you the script, but you'll need to tell me if you're working Cognos 10.2 and up or 10.1 and down, because the methods vary.

If you go with the second method, you won't be able to default your schedules - JS won't work on report ran in the background. You'll have you use Event Studio to schedule the report with dynamic year-month.
Title: Re: Java script for default month and year prompts
Post by: CognosPaul on 11 Jan 2015 06:53:49 AM
You could use JavaScript to set the value on the page, but use a macro prompt default parameter to use the current month.

[Year] = #prompt('Year','integer',timestampMask($current_timestamp,'yyyy'))#
and [Month] = #prompt('Month','integer',timestampMask($current_timestamp,'mm'))#

or for previous month:
[Year] = #prompt('Year','integer',timestampMask(_add_months($current_timestamp,-1),'yyyy'))#
and [Month] = #prompt('Month','integer',timestampMask(_add_months($current_timestamp,-1),'mm'))#

This way the schedule would continue to work as expected, just don't select anything in the prompt.
Title: Re: Java script for default month and year prompts
Post by: Cognos Bee on 06 Feb 2015 09:40:44 AM
I'm looking for exactly this JS using 10.2.1

Thanks

Quote from: Nimrod Avissar on 11 Jan 2015 05:27:20 AM
There are two ways to go.
1. Add to the prompt a static value with 1 as the use value and "Current Year" as the display value. Set the filter to something like:
(?prmYear?=1 and [Year]=extract(year, current_date)) OR ([Year]=?prmYear?)
And repeat for month.
2. Javascript to set defaults. I can give you the script, but you'll need to tell me if you're working Cognos 10.2 and up or 10.1 and down, because the methods vary.

If you go with the second method, you won't be able to default your schedules - JS won't work on report ran in the background. You'll have you use Event Studio to schedule the report with dynamic year-month.
Title: Re: Java script for default month and year prompts
Post by: Robl on 06 Feb 2015 10:51:57 AM
Another very different idea.

Why not just use a regular date prompt.
Then use month and year functions to derive the month and year values for the filter.

If you put the date prompt in a block and set the block as cropped and 1.75(ish) cm high then it looks just like a year/month prompt.
In fact, if you have the date in the data you can use a first of month funtion on the prompt and data to have a working month prompt that always shows the current month.
Title: Re: Java script for default month and year prompts
Post by: bdbits on 09 Feb 2015 05:12:30 PM
I generally use something similar to CognosPaul's approach with prompt macros. If you find yourself using a dimensional source, roll the prompt macros into a filter() expression.

With the prompts, the defaults are generally at the top of my prompt anyway so even then, no need for javascript as they see the defaults in the prompts unless they select something.

It all works great, out-of-the-box functionality so it is unlikely to ever break, and as CognosPaul said no problems scheduling the reports if desired.
Title: Re: Java script for default month and year prompts
Post by: Chamopak on 20 Jan 2020 01:59:56 PM
I know I am like 9 years later, but does anyone have these steps in "for Dummies" style with screenshots?

Thank you  8) :P

Quote from: Nimrod Avissar on 11 Jan 2015 05:27:20 AM
There are two ways to go.
1. Add to the prompt a static value with 1 as the use value and "Current Year" as the display value. Set the filter to something like:
(?prmYear?=1 and [Year]=extract(year, current_date)) OR ([Year]=?prmYear?)
And repeat for month.
2. Javascript to set defaults. I can give you the script, but you'll need to tell me if you're working Cognos 10.2 and up or 10.1 and down, because the methods vary.

If you go with the second method, you won't be able to default your schedules - JS won't work on report ran in the background. You'll have you use Event Studio to schedule the report with dynamic year-month.