Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Help needed with below script

dvelloriy
Kilo Sage

Hello Community,

I need some help with below code.

Requirement: Populate Due date on SC Task records from delivery time (in Business days) specified on catalog item.

 

I have created below script, it is working as expected however i need to account for just business days.

So if a request is created today for "Ask a Question" item with delivery date (5 business days), due date on task should be July 16th.

 

Before insert >> Condition - Item.delivery_time is not empty

 

Script: 

var intDuration = current.cat_item.delivery_time.dateNumericValue();

var gdtDueDate = new GlideDateTime();

gdtDueDate.add(intDuration);

current.due_date = gdtDueDate;

current.update();

 

 

Please advise,

1 ACCEPTED SOLUTION

Hello @dvelloriy ,

Here is the updated code. Give it a try.

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

    // Add your code here
    var duration = parseInt(current.cat_item.delivery_time.getDisplayValue());
    var todayDate = new GlideDateTime();
    var dueDate = addDays(todayDate, duration);
    current.due_date = dueDate;

    function addDays(todayDate, duration) {
        var count = 0;
        var gdt = new GlideDateTime(todayDate);
        while (count < duration) {
            gdt.addDaysLocalTime(1);
            var dayWeek = gdt.getDayOfWeekLocalTime();
            if (dayWeek != 6 && dayWeek != 7) { // 6 = Saturday, 7 = Sunday
                count++;
            }
        }
        return gdt;
    }
})(current, previous);

 

If this response resolves your query, kindly mark it as both helpful and correct.

Thanks,

Alka

View solution in original post

8 REPLIES 8

Community Alums
Not applicable

Hi @dvelloriy ,

 

Here is an updated code to consider the business days-

 

(function executeRule(current, previous /*null when async*/) {
    var intDuration = parseInt(current.cat_item.delivery_time);
    var gdtDueDate = new GlideDateTime();
    // Function to add business days
    function addBusinessDays(startDate, days) {
        var count = 0;
        var gdt = new GlideDateTime(startDate); // new GlideDateTime to avoid modifying the earlier one
        while (count < days) {
            gdt.addDays(1);
            var dayOfWeek = gdt.getDayOfWeekUTC();
            if (dayOfWeek != 6 && dayOfWeek != 0) { // 6 = Saturday, 0 = Sunday
                count++;
            }
        }
        return gdt;
    }
    var dueDate = addBusinessDays(gdtDueDate, intDuration);
    current.due_date = dueDate;
})(current, previous);

 

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

Thanks Sanjay. not sure what went wrong here.

But after updating your script, due date is coming as "2030-10-24 17:49:58"

There is something wrong i believe. 

So i checked the xml of catalog item where delivery date was set as 5 days.

In the xml, the delivery time is coming as 1970-01-06 00:00:00

Is it something to do with that?

Community Alums
Not applicable

Hi @dvelloriy ,
Please try this updated code-

(function executeRule(current, previous /*null when async*/) {
    if (!current.cat_item.delivery_time) {
        return;
    }
    var intDuration = parseInt(current.cat_item.delivery_time);
    var gdtDueDate = new GlideDateTime();
    function addBusinessDays(startDate, days) {
        var count = 0;
        var gdt = new GlideDateTime(startDate);

        while (count < days) {
            gdt.addDaysLocalTime(1);
            var dayOfWeek = gdt.getDayOfWeekLocalTime();
            if (dayOfWeek != 6 && dayOfWeek != 7) { // 6 = Saturday, 7 = Sunday
                count++;
            }
        }

        return gdt;
    }
    var dueDate = addBusinessDays(gdtDueDate, intDuration);
    current.due_date = dueDate;
})(current, previous);

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

still not working, not its returning 2032-01-27 18:52:20