On change Client Script to calculate number of days difference between Current date and End date in Request form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2022 11:29 PM
Hi,
Can anyone help.
I am able to calculate duration using business rule but not able to calculate the number of days using On Change client script to find days difference between current date and end date in servicenow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2022 11:36 PM
Hi Chirag,
Can you please share the script you have tried so far for us to understand more and help you
Thanks,
Bhavna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2022 11:43 PM
Hi Chirag,
You can use the below script,
var today_date = new GlideDate(); // current date
var enddate= '2023-06-16'; // you can get the end date
var days = Math.floor(gs.dateDiff(today_date, enddate, true) / 86400); //it will calculate the days diff'
Regards,
Naveen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2022 11:43 PM
Hi
You can use a Script Include and Client script to get to your requirement:
1) Create a Client Callable Script Include and use the script as below:
var gteDays = Class.create();
gteDays.prototype = Object.extendsObject(AbstractAjaxProcessor, {
fetchDate: function() {
var getEndDate = this.getParameter('sysparm_end');
var start = new GlideDateTime();
var end = new GlideDateTime(getEndDate);
var diff = GlideDateTime.subtract(start, end);
var days = diff.getRoundedDayPart();
return days.toString();
},
type: 'gteDays'
});
Now create a Client script can be on Load or On Change as you need and use the script as below:
I have done this for On Load you can do this for on Change just replace the fiels correctly in sample script shared above.
function onLoad() {
//Type appropriate comment here, and begin script below
var gaPhone = new GlideAjax('gteDays');
gaPhone.addParam('sysparm_name', 'fetchDate');
gaPhone.addParam('sysparm_end', g_form.getValue('end date field here'));
gaPhone.getXMLAnswer(_handleResponse);
function _handleResponse(response) {
var answer = response;
g_form.setValue('FIEld Name', answer); // Replace "FIEld Name" where you want to set the number of days
}
}
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke