- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 08:37 AM - edited 01-11-2024 09:19 AM
If I use this code in background script it computes the day correctly. But when I try to include in script include the day computation doesn't work. Please advise.?
Background script:
var incGr = new GlideRecord('incident'); incGr.get('e329de99731423002728660c4cf6a73c'); var createdTime = incGr.sys_created_on.getDisplayValue(); var lastUpdatedTime = incGr.sys_updated_on.getDisplayValue(); var timeDifference = gs.dateDiff(createdTime, lastUpdatedTime); var day = timeDifference.getDay(); day = parseInt(timeDifference); gs.info('day:' + day);
============================================
Script include
var incTimesArray = Class.create(); incTimesArray.prototype = Object.extendsObject(AbstractAjaxProcessor, { getTimeValues: function() { var incGr = new GlideRecord('incident'); incGr.get(this.getParameter('sysparm_inc_sys_id')); var createdTime = incGr.sys_created_on.getDisplayValue(); var lastUpdatedTime = incGr.sys_updated_on.getDisplayValue(); var timeDifference = gs.dateDiff(createdTime, lastUpdatedTime); var current_system_date = new GlideDateTime(); var timeDifference2 = gs.dateDiff(lastUpdatedTime, current_system_date); var day = timeDifference.getDay(); day = parseInt(timeDifference); var response = {}; response.day = day; return JSON.stringify(response); }, type: 'incTimesArray' });
==========
Client Script:
function onLoad() { var ga = new GlideAjax('incTimesArray'); ga.addParam('sysparm_name', "getTimeValues"); ga.addParam('sysparm_inc_sys_id', g_form.getUniqueValue()); ga.getXMLAnswer(function(answer) { var response = JSON.parse(answer); g_form.addInfoMessage.info('days past: ' + response.day); }); }
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 09:34 AM
Hi @Sri29
There are a few issues with the code in the Script Include that need to be addressed:
1. The `gs.dateDiff()` function returns a string representing the difference between two dates in the format "xd yh zm". It does not return a `GlideDateTime` object, so calling `.getDay()` on the result of `gs.dateDiff()` will not work as expected.
2. The `parseInt(timeDifference)` call is incorrect because `timeDifference` is a string that includes days, hours, and minutes. You cannot directly parse this string into an integer to get the number of days.
3. The `GlideDateTime` object `current_system_date` is created but not used in the Script Include.
Here's how you can modify the Script Include to correctly calculate the number of days:
var incTimesArray = Class.create();
incTimesArray.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getTimeValues: function() {
var incGr = new GlideRecord('incident');
incGr.get(this.getParameter('sysparm_inc_sys_id'));
var createdTime = incGr.sys_created_on.getDisplayValue();
var lastUpdatedTime = incGr.sys_updated_on.getDisplayValue();
var timeDifference = gs.dateDiff(createdTime, lastUpdatedTime, true); // true to get time difference in seconds
var day = Math.floor(timeDifference / (24 * 60 * 60)); // Convert seconds to days
var response = {};
response.day = day;
return JSON.stringify(response);
},
type: 'incTimesArray'
});
In the above code, I've made the following changes:
- I've added the `true` parameter to the `gs.dateDiff()` function call to get the time difference in seconds.
- I've calculated the number of days by dividing the time difference in seconds by the number of seconds in a day (24 hours * 60 minutes * 60 seconds).
- I've removed the unused `current_system_date` variable.
Additionally, there is a typo in the Client Script. The `g_form.addInfoMessage.info()` call should be `g_form.addInfoMessage()`.
Here's the corrected line:
g_form.addInfoMessage('days past: ' + response.day);
With these changes, the Script Include should correctly calculate the number of days between the `sys_created_on` and `sys_updated_on` dates for an incident record and return that value to the Client Script.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 09:34 AM
Hi @Sri29
There are a few issues with the code in the Script Include that need to be addressed:
1. The `gs.dateDiff()` function returns a string representing the difference between two dates in the format "xd yh zm". It does not return a `GlideDateTime` object, so calling `.getDay()` on the result of `gs.dateDiff()` will not work as expected.
2. The `parseInt(timeDifference)` call is incorrect because `timeDifference` is a string that includes days, hours, and minutes. You cannot directly parse this string into an integer to get the number of days.
3. The `GlideDateTime` object `current_system_date` is created but not used in the Script Include.
Here's how you can modify the Script Include to correctly calculate the number of days:
var incTimesArray = Class.create();
incTimesArray.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getTimeValues: function() {
var incGr = new GlideRecord('incident');
incGr.get(this.getParameter('sysparm_inc_sys_id'));
var createdTime = incGr.sys_created_on.getDisplayValue();
var lastUpdatedTime = incGr.sys_updated_on.getDisplayValue();
var timeDifference = gs.dateDiff(createdTime, lastUpdatedTime, true); // true to get time difference in seconds
var day = Math.floor(timeDifference / (24 * 60 * 60)); // Convert seconds to days
var response = {};
response.day = day;
return JSON.stringify(response);
},
type: 'incTimesArray'
});
In the above code, I've made the following changes:
- I've added the `true` parameter to the `gs.dateDiff()` function call to get the time difference in seconds.
- I've calculated the number of days by dividing the time difference in seconds by the number of seconds in a day (24 hours * 60 minutes * 60 seconds).
- I've removed the unused `current_system_date` variable.
Additionally, there is a typo in the Client Script. The `g_form.addInfoMessage.info()` call should be `g_form.addInfoMessage()`.
Here's the corrected line:
g_form.addInfoMessage('days past: ' + response.day);
With these changes, the Script Include should correctly calculate the number of days between the `sys_created_on` and `sys_updated_on` dates for an incident record and return that value to the Client Script.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 09:42 AM
Thanks @Iraj Shaikh for sharing the fix. You can discard the 'current_system_date' variable an other typo with info message.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 09:35 AM
What are you getting when trying via script include?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 09:40 AM
It doesn't return a value.