getting number of working days between start date and end date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2024 10:09 PM - edited 03-23-2024 01:04 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2024 12:41 AM
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:
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2024 12:51 AM
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
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
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2024 12:59 AM
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2024 01:03 AM - edited 03-23-2024 01:05 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2024 01:07 AM
@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