Due date based on the Employment start date

Naga Ravindra R
Kilo Sage

Hello all,

I was trying to set the due date on one HR task with the help of After business rule. Following is my code

(function executeRule(current, previous /*null when async*/ ) {

        // Add your code here
        var sche = '08fcd0830a0a0b2600079f56b1adb9ae';
        var getSub = current.parent.subject_person;
        var hrProf = new GlideRecord('sn_hr_core_profile');
        hrProf.addQuery('user', getSub); 
        hrProf.query();
        if (hrProf.next()) {
            var ESD = hrProf.employment_start_date;
            gs.info("ESD= " + ESD);
            var ESDGDT = new GlideDateTime(ESD);
            gs.info("ESDGDT = " + ESDGDT);
            var dc = new DurationCalculator();
            dc.setSchedule(sche);
            gs.info('passing scheduleinto duration');
            var passsched = new GlideSchedule(sche);
            gs.info('passing scheduleinto glideschedule');
            for (var i = 0; i < 5; i++) {
                ESDGDT.addDaysLocalTime(-1);
                if (!sched.isInSchedule(ESDGDT)) {
                    i = i - 1;
                }
            }
            gs.info("ESDGDT= " + ESDGDT);
            current.due_date = ESDGDT;
            current.update();
        }
})(current, previous);

 

My Requirement is, I have to update the due date of HR Task to 5 working days before employment start date. Any help or suggestions on how to achieve??

suppose if today is employment start date(10/8/2022)..then due date should be  (3/8/2022)

 
3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

check this link on how to subtract and get the working day

subtract time from schedule to update task due date

Calculate Date By Adding or Subtracting Time With A Schedule

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar 

Thanks for your help.

I was doing this with the help of DurationCalculator Script include.

Here is my code:

  var sysID = "090eecae0a0a0b260077e1dfa71da828"; //sys_id of schedule
    var getSub = current.parent.subject_person;
    var hrProf = new GlideRecord('sn_hr_core_profile');
    hrProf.addQuery('user', getSub);
    hrProf.query();
    if (hrProf.next()) {
        var ESD = hrProf.employment_start_date;
        var ESDGDT = new GlideDateTime(ESD);
        var dc = new global.DurationCalculator();
        dc.setSchedule(sysID);
        var sched = new GlideSchedule(sysID);
        for (i = 0; i < 5; i++) {
            ESDGDT.addDaysLocalTime(-1);
            if (!sched.isInSchedule(ESDGDT)) {
                i = i - 1;
            }
        }
    }
    gs.info("Employment due date=" + ESDGDT);
    current.due_date = ESDGDT;
  

This is working fine and also it is updating the due date. But when I create any case through lifecycle events, the script creates a loop. 

Can you provide your input on where exactly I need to change the code?

 

Thanks in advance.

Hi,

why not use before Business rule and current.update() will not be required

It's not recommended to use current.update() in BR

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader