- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 12:03 AM - edited 12-23-2024 12:07 AM
const scTaskGr = new GlideRecord('sc_task');
scTaskGr.addActiveQuery();
scTaskGr.addQuery('cat_item', '55ae8727e05b461093590cbbafe05143');
scTaskGr.addQuery('request_item.stage', 'fulfillment');
scTaskGr.query();
while (scTaskGr.next()) {
const start = scTaskGr.variables.target_commencement_date;
const end = scTaskGr.variables.target_completion_date;
scTaskGr.variables.days_left_before_target_completion_date = GlideDate.subtract(start, end).getDisplayValue();
scTaskGr.updateWithReferences();
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 01:03 AM
then store the duration
const scTaskGr = new GlideRecord('sc_task');
scTaskGr.addActiveQuery();
scTaskGr.addQuery('cat_item', '55ae8727e05b461093590cbbafe05143');
scTaskGr.addQuery('request_item.stage', 'fulfillment');
scTaskGr.query();
while (scTaskGr.next()) {
const start = scTaskGr.variables.target_commencement_date;
const end = scTaskGr.variables.target_completion_date;
var ritm = new GlideRecord('sc_req_item');
ritm.get(scTaskGr.request_item);
ritm.variables.days_left_before_target_completion_date = GlideDate.subtract(start, end).getDisplayValue();
ritm.update();
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 10:23 PM
Hi,
Thanks for this, but this one worked for me. I've used var instead of const inside the while loop. And I've accessed the variables in sc_task table.
gs.info('Set Remaining Days Left Before Target Completion Date Scheduled Job is running...');
var scTaskGr = new GlideRecord('sc_task');
scTaskGr.addActiveQuery();
scTaskGr.addQuery('cat_item', '55ae8727e05b461093590cbbafe05143');
scTaskGr.addQuery('request_item.stage', 'fulfillment');
scTaskGr.orderBy('number');
scTaskGr.query();
gs.info('Row Count: ' + scTaskGr.getRowCount());
while (scTaskGr.next()) {
// gs.info('Current: ' + scTaskGr.variables.days_left_completion_date);
var start = new GlideDate();
start.setValue(scTaskGr.variables.target_commencement_date);
var end = new GlideDate();
end.setValue(scTaskGr.variables.target_completion_date);
var daysLeft = GlideDate.subtract(start, end);
gs.info('SCTASK Number: ' + scTaskGr.number);
gs.info('Target Commencement Date: ' + start);
gs.info('Target Completion Date: ' + end + '\n');
gs.info("Days Left: " + daysLeft.getDisplayValue());
if (start.getDisplayValue() == end.getDisplayValue()) {
scTaskGr.variables.days_left_completion_date = '0 Day';
} else {
scTaskGr.variables.days_left_completion_date = daysLeft.getDisplayValue();
}
scTaskGr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 10:23 PM
Hi,
Thanks for this, but this one worked for me. I've used var instead of const inside the while loop. And I've accessed the variables in sc_task table.
gs.info('Set Remaining Days Left Before Target Completion Date Scheduled Job is running...');
var scTaskGr = new GlideRecord('sc_task');
scTaskGr.addActiveQuery();
scTaskGr.addQuery('cat_item', '55ae8727e05b461093590cbbafe05143');
scTaskGr.addQuery('request_item.stage', 'fulfillment');
scTaskGr.orderBy('number');
scTaskGr.query();
gs.info('Row Count: ' + scTaskGr.getRowCount());
while (scTaskGr.next()) {
// gs.info('Current: ' + scTaskGr.variables.days_left_completion_date);
var start = new GlideDate();
start.setValue(scTaskGr.variables.target_commencement_date);
var end = new GlideDate();
end.setValue(scTaskGr.variables.target_completion_date);
var daysLeft = GlideDate.subtract(start, end);
gs.info('SCTASK Number: ' + scTaskGr.number);
gs.info('Target Commencement Date: ' + start);
gs.info('Target Completion Date: ' + end + '\n');
gs.info("Days Left: " + daysLeft.getDisplayValue());
if (start.getDisplayValue() == end.getDisplayValue()) {
scTaskGr.variables.days_left_completion_date = '0 Day';
} else {
scTaskGr.variables.days_left_completion_date = daysLeft.getDisplayValue();
}
scTaskGr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 10:53 PM
I believe I have provided you the correct answer.
you could have simply updated the variable from const to var
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 03:48 AM
Yes, it is possible to update a variable in a variable editor using a scheduled job. Your approach to calculating the Days Before Target Completion Date by subtracting two date fields is valid. However, there are a few things that need to be adjusted in your script, as well as a clarification regarding how you are working with the variables in the sc_task record and updating them
(function() {
const scTaskGr = new GlideRecord('sc_task');
scTaskGr.addActiveQuery();
scTaskGr.addQuery('cat_item', '55ae8727e05b461093590cbbafe05143');
scTaskGr.addQuery('request_item.stage', 'fulfillment');
scTaskGr.query();
while (scTaskGr.next()) {
const start = scTaskGr.variables.target_commencement_date;
const end = scTaskGr.variables.target_completion_date;
if (start && end) {
const startDateTime = new GlideDateTime(start);
const endDateTime = new GlideDateTime(end);
const diffInMillis = endDateTime.getNumericValue() - startDateTime.getNumericValue();
const diffInDays = diffInMillis / (1000 * 60 * 60 * 24);
scTaskGr.variables.days_left_before_target_completion_date = diffInDays;
scTaskGr.update();
Handling Null or Missing Dates: The script assumes that both target_commencement_date and target_completion_date are available. If either is missing, it will skip that task. You may want to add additional logic to handle cases where the dates are not set.
Logging and Error Handling: You may want to add some logging for better tracking of script execution, especially if you need to debug any issues with the data
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 10:26 PM
Hi,
Target Commencement Date and Target Completion Date are mandatory in the form. I also adding logging with gs.info for debugging like you suggested.
Thanks