getting number of working days between start date and end date

akhil_kusa
ServiceNow Employee
ServiceNow Employee

I'm new to learning ServiceNow, and I'm developing a mock PTO (Paid Time Off) application. I'm encountering difficulties in calculating the business days between two dates and checking for insufficient PTO balance. Below are my client script and script includes. Could you please review them and provide corrections?

ClientScript :

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var start = g_form.getValue('start_date');
    var balance = parseFloat(g_form.getValue('balance'));


    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('PTOAjax');
    ga.addParam('sysparm_name', 'getDatediff');
    ga.addParam('sysparm_start', start);
    ga.addParam('sysparm_end', newValue);

    ga.getXML(getDuration);


    function getDuration(response, balance) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var days = (answer / (1000 * 60 * 60 * 24)) + 1;

        if (answer > balance) {
            g_form.addErrorMessage("Insufficient PTO");
            g_form.clearValue('duration');
            g_form.clearValue('end_date');

        } else {
            g_form.clearMessages();
            g_form.setValue('duration', days);
        }
    }

 

 

 

Script Include:

 

 

    getDatediff: function() {
        var startDate = new GlideDateTime();
        startDate.setDisplayValue(this.getParameter('sysparm_start'));
        var endDate = new GlideDateTime();
        endDate.setDisplayValue(this.getParameter('sysparm_end'));

        var schedule = new GlideSchedule('2d37aaa69781c2102z0036e71153afd9', gs.getProperty('glide.sys.default.tz'));

        var duration1 = schedule.duration(startDate, endDate);
        return duration1.getNumericValue();

    },

type: 'PTOAjax'

 

 

 

Appreciate your quick help!

 

@Ankur Bawiskar 

11 REPLIES 11

Deepak Shaerma
Kilo Sage

Hi @akhil_kusa 

Yes, obtaining the sys_id 
of a record by opening the form, right-clicking on the form’s header, and selecting “Copy sys_id” is an appropriate and commonly used method. But there is also a quick way to compare or watch your sys_id in real time is through  browser URL. Refer Screenshot below:

DeepakShaerma_0-1711179640580.png

 


Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma

Thanks @Deepak Shaerma 

 

I am little confused here. below is the screenshot if I open the empty form to submit a request. (This is where i was copying the sys_id). but the url dont have sys_id

akhil_kusa_0-1711179924620.png

and below is the screenshot after submitting the request (Still doesn't have duration value returned). url has sys_id but not same to the above

Screenshot 2024-03-23 at 02.47.49.png

 

both the sys_id's are different. I am not sure what to use.

 

Could please let me know which is the correct one to use. also i see it is unique but it keeps changing time to time.

Deepak Shaerma
Kilo Sage

Hi @akhil_kusa

Sys_id was not shown on URL when the form is empty but when you open a new form it holds the sys_id in backend database. you need to copy it from the form context menu and when is submitted. sys_id will shown on url also and the same as when form opens empty.

DeepakShaerma_0-1711180777940.png

 



Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot

- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma

Thanks @Deepak Shaerma 

 

If thats the case, I am using correct sys_id, and unable to return duration.

@akhil_kusa can you please explain me your requirements so that i am helping you to fix it.
Meanwhile Please Mark my responses Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma