isInSchedule check whether dates are passing through the Schedule or not

salu
Mega Guru

ctomasi,

b-rad

Hello All,

I have created scheduledfor freeze period.I want to check my start date and end date are passing through schedule or not.Can some one help me to get a solution for this?

answer = ifScript();

function ifScript() {

  var scheduled_start_date = current.start_date;

  var scheduled_end_date = current.end_date;

  var schedRec = new GlideRecord('cmn_schedule');

  schedRec.get('name','Freeze Period');

  var sched = new GlideSchedule(schedRec.sys_id);

  var gdt_start = new GlideDateTime(scheduled_start_date);

  var gdt_end= new GlideDateTime(scheduled_end_date);

  if ((sched.isInSchedule(gdt_start))||(sched.isInSchedule(gdt_end))) {

  return 'yes';

  }

  else

  {

  return 'no';

  }

}

I have four scenario this needs return yes.

1) Planned start date is before schedule or End Date in within schedule .

2)Planned start date is within schedule or end date is in after schedule .

3)Planned start date and end date are within schedule .

4)Planned start date is before schedule and planned end date is after schedule and thist pass through the schedule or not

My 4th scenario is not working as per the above script.

Can some one please help me to fix it?

Thanks

saranya

1 ACCEPTED SOLUTION

Hello Chuck,



Thanks for the information.



I got the solution



answer = ifScript();


function ifScript() {



  var scheduled_start_date = current.start_date;


  var scheduled_end_date = current.end_date;




  var schedRec = new GlideRecord('cmn_schedule');


  schedRec.get('name','Freeze Period');


  var schedRec1 = new GlideRecord('cmn_schedule_span');


  schedRec1.addQuery('name','Freeze Period');


  schedRec1.query();


  var start;


  var end;


  if(schedRec1.next())


  {




  start=schedRec1.start_date_time.getDisplayValue();


  end=schedRec1.end_date_time.getDisplayValue();


  }



  var sched = new GlideSchedule(schedRec.sys_id);



  var gdt_start = new GlideDateTime(scheduled_start_date);


  var gdt_end= new GlideDateTime(scheduled_end_date);


  if ((sched.isInSchedule(gdt_start))||(sched.isInSchedule(gdt_end)) || (scheduled_start_date <= start && scheduled_end_date >= end)) {



  return 'yes';


  }


  else


  {



  return 'no';


  }


}


View solution in original post

2 REPLIES 2

Chuck Tomasi
Tera Patron

Hi Saranya,



isInSchedule() only checks if the data is within the one of the schedule's periods. I think your fault is in assuming there is a "before" and "after" to the schedule. If the schedule only has one period, sure, that might work, but the point of schedules is to make repeating easy. Example: Monday-Friday 0800-1700. That's every day, every week. What does "before" mean? Is 3:00AM before 0800 or after 1700? With a schedule a time is either in or out of that schedule, so your logic won't work here.


Hello Chuck,



Thanks for the information.



I got the solution



answer = ifScript();


function ifScript() {



  var scheduled_start_date = current.start_date;


  var scheduled_end_date = current.end_date;




  var schedRec = new GlideRecord('cmn_schedule');


  schedRec.get('name','Freeze Period');


  var schedRec1 = new GlideRecord('cmn_schedule_span');


  schedRec1.addQuery('name','Freeze Period');


  schedRec1.query();


  var start;


  var end;


  if(schedRec1.next())


  {




  start=schedRec1.start_date_time.getDisplayValue();


  end=schedRec1.end_date_time.getDisplayValue();


  }



  var sched = new GlideSchedule(schedRec.sys_id);



  var gdt_start = new GlideDateTime(scheduled_start_date);


  var gdt_end= new GlideDateTime(scheduled_end_date);


  if ((sched.isInSchedule(gdt_start))||(sched.isInSchedule(gdt_end)) || (scheduled_start_date <= start && scheduled_end_date >= end)) {



  return 'yes';


  }


  else


  {



  return 'no';


  }


}