Need to restrict Fiscal periods on the demand cost plans based on the selected date

Sanjeeva Nagend
Tera Contributor
Hi All,
 
We have to restrict the fiscal period on cost plan table.
 
SanjeevaNagend_0-1724733683553.png

 

 
We have a new field called fiscal period. We need to compare fiscal periods with the freeze date field. I am converting it from date to date/time code was not working.
If the freeze date was yesterdays date then current month should not be visible today, if it is future then current month also should be visible. 
 
Below script was not working as expected.
 
var grFiscalPeriod1 = new GlideRecord('fiscal_period');
            grFiscalPeriod1.addEncodedQuery('fiscal_type=month');
            grFiscalPeriod1.query();

            while (grFiscalPeriod1.next()) {
                var new_date = grFiscalPeriod1._freeze_date;
                var time = '11:59:59';

                var date_timeee = new_date + ' '+ time;

                var dat = new GlideDateTime(date_timeee);
              var ans1 = new GlideScheduleDateTime();                
                ans1.setValue(dat);        

                var today1 = new GlideDateTime();
           
                if (dat >= today1) {
                    //count++;
                    list += (',' + grFiscalPeriod1.sys_id);
                }



            }
3 REPLIES 3

Najmuddin Mohd
Mega Sage

Hi @Sanjeeva Nagend ,

_freeze_date is this an OOB field or custom field on the cost plan ?

Regards,
Najmuddin.

Najmuddin Mohd
Mega Sage

Hi @Sanjeeva Nagend ,

I have create a custom field in my instance u_freeze_date.


If the freeze date is not filled, then it will compare start date time of the fiscal period 

If the freeze date is filled, then it will compare the start date time and the Freeze date of the fiscal period.

 

var grFiscalPeriod = new GlideRecord('fiscal_period');
//grFiscalPeriod.addQuery('sys_id', '');
grFiscalPeriod.query();


var array = [];

while (grFiscalPeriod.next()) {
    var startTime = new GlideScheduleDateTime();
    startTime.setValue(grFiscalPeriod.start_date_time);

    var today = new GlideDateTime();
    var compareStartDate = startTime >= today;

	/****
	 * If Freeze date is not present
	 ****/

    if (grFiscalPeriod.u_freeze_date.isNil()) {
		//gs.info('In first IF');


		/******
		 * Compare Start date
		********/

        if (compareStartDate) {
			//gs.info('In second IF');
            array.push(grFiscalPeriod.name.toString());
        }
    } else {
		//gs.info('In Fist Else');

        var todayFreeze = new GlideDate();
        var compareFreezeDate = grFiscalPeriod.u_freeze_date >= todayFreeze;

        // gs.info('Today ' + todayFreeze);
        // gs.info('Freeze date:' + grFiscalPeriod.u_freeze_date);

        // gs.info(compareFreezeDate);

        /******
		 * Compare Start date and Freeze Date	
		********/

        if (compareStartDate && compareFreezeDate) {

			//gs.info('IN if of ELSE');
            //count++;
            //array.push(grFiscalPeriod.sys_id.toString());
			array.push(grFiscalPeriod.name.toString());
        }
    }

}
gs.info(array);
gs.info(array.length);

 


Run this script in the scripts - background, by taking one particular record sys_id in the second line of code to validate your requirement.

Hope this information helps you.

Kindly mark it as Helpful, if this information helps and Accept the solution.

Regards,
Najmuddin.

Hi Nizamuddin,

 

Thanks for your reply.

I have tried your script, for every fiscal period date was tomorrows date. But current month is not visible.

 

var grFiscalPeriod = new GlideRecord('fiscal_period');
grFiscalPeriod.addEncodedQuery('fiscal_type=month');
grFiscalPeriod.query();
var array = [];
while (grFiscalPeriod.next()) {
    var startTime = new GlideScheduleDateTime();
    startTime.setValue(grFiscalPeriod.start_date_time);

    var today = new GlideDateTime();
    var compareStartDate = startTime >= today;

    if (grFiscalPeriod.u_physical_progress_freeze_date.isNil()) {
       
        if (compareStartDate) {
           
            array.push(grFiscalPeriod.name.toString());
        }
    } else {
       
        var todayFreeze = new GlideDate();
        var compareFreezeDate = grFiscalPeriod.u_physical_progress_freeze_date >= todayFreeze;

         if (compareStartDate && compareFreezeDate) {
            array.push(grFiscalPeriod.name.toString());
        }
    }

}
gs.info(array);
gs.info(array.length);
 
 
 
 
 
 
 
 
I have written below script but this one also didn't give the current month.
 
var list = '';
var grFiscalPeriod1 = new GlideRecord('fiscal_period');
            grFiscalPeriod1.addEncodedQuery('fiscal_type=month');
            grFiscalPeriod1.query();

            while (grFiscalPeriod1.next()) {

                var dat = grFiscalPeriod1.u_physical_progress_freeze_date;
                var date_time = new GlideDateTime(dat);

                var ans = new GlideScheduleDateTime();
                ans.setValue(grFiscalPeriod1.start_date_time);

                var today = new GlideDateTime();
                if ((date_time >= today && ans >= today) || (ans>= date_time && ans >= today)){
                    list += (',' + grFiscalPeriod1.sys_id);
                }

            }
            gs.info(list);
            gs.info(list.length);