- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2025 10:27 PM - edited 05-26-2025 10:27 PM
Hi,
I was writing a scheduled script but was stuck at one point. In the below script "planned duration" variable is basically the number of the days that needs to be added. So now we have 2 scenarios -- 1)add the planned duration days to sys created on 2)add the planned duration to the validation field .. Then we need to find the max/later of the 2 dates after adding and check if today's date is equal to or greater than that max date then we need to close the task
var now = new GlideDateTime();
var created ='';
var planned_duration='';
var validation ='';
var sumofcreatedandplanned='';
var task = new GlideRecord('x_iem_tqa_work_order_task');
task.addEncodedQuery("stateIN1,2^task_type=task_for_site_acceptance");
task.query();
while(task.next())
{
var order = task.associated_work_order_number;
var workflowassign = task.associated_work_order_number.assocaited_work_request_number.workflow;
created=task.sys_created_on.getGlideObject();
validation = task.associated_work_order_number.tc_validation_end_by_planned;
var workflow= new GlideRecord('x_iem_tqa_workflow_assignment');
workflow.addQuery('sys_id',workflowassign);
workflow.addEncodedQuery("planned_site_acceptance_duration_for_auto_closureISNOTEMPTY");
workflow.query();
if(workflow.next())
{
planned_duration=workflow.planned_site_acceptance_duration_for_auto_closure;
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2025 09:10 AM
try this
var now = new GlideDateTime();
var task = new GlideRecord('x_iem_tqa_work_order_task');
task.addEncodedQuery("stateIN1,2^task_type=task_for_site_acceptance");
task.query();
while (task.next()) {
var order = task.associated_work_order_number;
var workflowassign = order.assocaited_work_request_number.workflow;
// Get sys_created_on as GlideDateTime
var created = new GlideDateTime(task.sys_created_on);
// Get validation date as GlideDateTime if it exists
var validation = order.tc_validation_end_by_planned;
var validationPlus = null;
if (validation) {
validationPlus = new GlideDateTime(validation);
}
// Get planned duration as integer
var planned_duration = 0;
var workflow = new GlideRecord('x_iem_tqa_workflow_assignment');
workflow.addQuery('sys_id', workflowassign);
workflow.addEncodedQuery("planned_site_acceptance_duration_for_auto_closureISNOTEMPTY");
workflow.query();
if (workflow.next()) {
planned_duration = parseInt(workflow.planned_site_acceptance_duration_for_auto_closure, 10) || 0;
} else {
continue; // Skip if no planned duration
}
// Add planned_duration days to sys_created_on
var createdPlus = new GlideDateTime(task.sys_created_on);
createdPlus.addDaysUTC(planned_duration);
// Add planned_duration days to validation date if it exists
if (validationPlus) {
validationPlus.addDaysUTC(planned_duration);
}
// Find the later of the two dates
var maxDate = createdPlus;
if (validationPlus && validationPlus.after(createdPlus)) {
maxDate = validationPlus;
}
// Check if now >= maxDate
if (now.compareTo(maxDate) >= 0) {
// Close the task (set state to Closed, e.g., state=3, adjust as needed)
task.state = 3; // Change 3 to your actual "Closed" state value if different
task.update();
gs.info('Task ' + task.sys_id + ' closed as max date reached.');
}
}
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
05-27-2025 04:46 AM
you are running the script in scoped app?
till which line it's working fine?
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
05-27-2025 05:00 AM
Yes in scoped app. It ran till this.
var now = new GlideDateTime();
var created = '';
var planned_duration = '';
var validation = '';
var sumofcreatedandplanned = '';
gs.info('test7'+now);
var task = new GlideRecord('x_iem_tqa_work_order_task');
task.addEncodedQuery("stateIN1,2^task_type=task_for_site_acceptance");
task.query();
while (task.next()) {
gs.info('test6'+task.sys_id);
var order = task.associated_work_order_number;
var workflowassign = task.associated_work_order_number.assocaited_work_request_number.workflow;
created = task.sys_created_on;
validation = task.associated_work_order_number.tc_validation_end_by_planned;
var workflow = new GlideRecord('x_iem_tqa_workflow_assignment');
workflow.addQuery('sys_id', workflowassign);
workflow.addEncodedQuery("planned_site_acceptance_duration_for_auto_closureISNOTEMPTY");
workflow.query();
if (workflow.next()) {
gs.info('test4'+task.sys_id);
planned_duration = workflow.planned_site_acceptance_duration_for_auto_closure;
// Add planned_duration days to both dates
gs.info('test15'+task.sys_id+planned_duration);
var createdPlus = new GlideDateTime(created);
gs.info('test25'+createdPlus);
After this it stopped at - createdPlus.addDaysUTC(planned_duration);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2025 05:11 AM
what value are you passing to that addDaysUTC()?
It should be an integer and not duration type
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
05-27-2025 05:19 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2025 05:26 AM
can you share the complete code again here along with the logs you added and till where it worked
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