Author Topic: Java script for default month and year prompts  (Read 6027 times)

Offline pcog

  • Full Member
  • ***
  • Join Date: Feb 2012
  • Posts: 33
  • Forum Citizenship: +0/-0
Java script for default month and year prompts
« 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 :)
« Last Edit: 06 Jan 2015 09:54:45 am by pcog »

Offline BigChris

  • Statesman
  • ******
  • Join Date: Apr 2013
  • Posts: 1,250
  • Forum Citizenship: +91/-0
Re: Java script for default month and year prompts
« Reply #1 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

C

Offline pcog

  • Full Member
  • ***
  • Join Date: Feb 2012
  • Posts: 33
  • Forum Citizenship: +0/-0
Re: Java script for default month and year prompts
« Reply #2 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 :)

Offline Nimrod Avissar

  • Statesman
  • ******
  • Join Date: Feb 2011
  • Posts: 556
  • Forum Citizenship: +86/-1
  • Try again. Fail again. Fail better.
Re: Java script for default month and year prompts
« Reply #3 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.
"I'm nobody, who are you?"

Offline CognosPaul

  • Global Moderator
  • Statesman
  • *****
  • Join Date: Jan 2009
  • Posts: 1,872
  • Forum Citizenship: +267/-1
    • Paul's Cognos Blog
Re: Java script for default month and year prompts
« Reply #4 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.

Offline Cognos Bee

  • Associate
  • **
  • Join Date: Feb 2015
  • Posts: 3
  • Forum Citizenship: +0/-0
Re: Java script for default month and year prompts
« Reply #5 on: 06 Feb 2015 09:40:44 am »
I'm looking for exactly this JS using 10.2.1

Thanks

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.

Offline Robl

  • Community Leader
  • *****
  • Join Date: Jul 2008
  • Posts: 199
  • Forum Citizenship: +4/-0
Re: Java script for default month and year prompts
« Reply #6 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.

Offline bdbits

  • Super Moderator
  • Statesman
  • ******
  • Join Date: Feb 2010
  • Posts: 1,822
  • Forum Citizenship: +106/-0
Re: Java script for default month and year prompts
« Reply #7 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.

Offline Chamopak

  • Senior Member
  • ****
  • Join Date: Feb 2015
  • Posts: 68
  • Forum Citizenship: +0/-0
Re: Java script for default month and year prompts
« Reply #8 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

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.