Tuesday, March 6, 2012

Simple steps to change default property of Dates in Cognos 8/10.


Simple steps to change default property of Dates in Cognos 8/10.

The below example can be implemented in Cognos 8.3/8.4 and Cognos 10. By default there is no direct way to change default values of Cognos Date Prompt. However we can use Java Scripts to change the default Cognos prompt behaviour. Below is such an example to document this.

Below example demonstrated using Go Sales (Query) Package

Step 1)

Create a sample new report by adding following fields.

Product line, Product Type, Product and Introduction Date ( all are found in Sales(query) > Products )



Step 2)

Add one prompt page.



Step 3 :

Insert a table with one column and then add one date prompt as below :





Step 4)

Under Date Prompt Properties, name the Date control name as PDate as below (PDate can be replaced by any name you like) :


Step 5 : Insert a HTML item after the Date prompt .


Step 6: Go to HTML item properties and edit the code of HTML as below examples :



JavaScript to Set Default for Date Prompt to Next Day

<script language="javascript">
var dDate = new Date();
//Subtract one day
dDate.setDate(dDate.getDate()-1);
pickerControlPDate.setValue(getFormatDate(dDate, 0 , 'YMD')); 
</script>







Step 7 : Click on OK and run the prompt page.



Below are some more sample java scripts to change default prompt values.



JavaScript to Set Default for Date Prompt to Next Day

<script language="javascript">
var dDate = new Date();
//Subtract one day
dDate.setDate(dDate.getDate()+1);
pickerControlPDate.setValue(getFormatDate(dDate, 0 , 'YMD')); 
</script>

JavaScript to Set Default for Date Prompt to First of Current Year

<script language="javascript">
//Months are 0 to 11
var dDate = new Date();
dDate.setMonth(0);
dDate.setDate(1);
pickerControlPDate.setValue(getFormatDate(dDate, 0 , 'YMD')); 
</script>

JavaScript to Set Default for Date Prompt to Last of Current Year

<script language="javascript">

//Create date to pull next year
var nyDate = new Date();
nyDate.setDate(nyDate.getDate() + 365);

var dDate = new Date('January 1, ' + nyDate.getYear());

//Subtract 1 to get last of current year
dDate.setDate(dDate.getDate()- 1);

pickerControlEndDate.setValue(getFormatDate(dDate, 0 , 'YMD')); 
</script>

JavaScript to default day to first of current month

<script language="javascript">
//Default is today
var dDate = new Date();
dDate.setDate(1);
pickerControlPDate.setValue(getFormatDate(dDate, 0 , 'YMD')); 
</script>

JavaScript to default day to last of current month

<script language="javascript">
var dDate = new Date();
//Get date 1 month later
dDate.setMonth( dDate.getMonth() + 1);
//Switch to first of next month
dDate.setDate(1);
//Subtract 1 to get last of current month
dDate.setDate(dDate.getDate()-1);
pickerControlEndDate.setValue(getFormatDate(dDate, 0 , 'YMD')); 
</script>

JavaScript to default day to first of previous month

<script language="javascript">
//Default date contains current month/year, so set day = 1
var dDate = new Date();
dDate.setDate(1);
//This goes back to last day of previous month
dDate.setDate(dDate.getDate()- 1);
//Now set day = 1 again
dDate.setDate(1)
pickerControlPDate.setValue(getFormatDate(dDate, 0 , 'YMD')); 
</script>

JavaScript to default day to last of previous month

<script language="javascript">
//Default date contains current month/year, so set day = 1
var dDate = new Date();
dDate.setDate(1);
//This goes back to last day of previous month
dDate.setDate(dDate.getDate()- 1);
pickerControlEndDate.setValue(getFormatDate(dDate, 0 , 'YMD')); 
</script>
JavaScript to default day to last year same date

<script>
function subtractYear ()
{
var dtToday = new Date();
var dtLastYearDay = new Date( dtToday - 31536000000 );

// NOTE 3153600000 = 365 Days * 24 hours * 60 (minutes per hour) * 60 (seconds per minute) * 1000 milliseconds per second)
var strLastYearday = [dtLastYearDay.getUTCFullYear(), dtLastYearDay.getMonth()+1, dtLastYearDay.getDate()].join("-");

return strLastYearday;
}
pickerControlPDate.setValue( subtractYear() );
</script>

4 comments:

  1. Worked like a charm.. and then I tried to schedule a task and it failed.. I guess the prompt page needs to load first in order for the javascript code to execute?

    ReplyDelete
  2. Please tell me how to change the First and Last date dynamically.

    ReplyDelete