add days for comparison to custom field

MaharshiC
Tera Contributor

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;
	  

	}
}

 

1 ACCEPTED SOLUTION

@MaharshiC 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

12 REPLIES 12

Ankur Bawiskar
Tera Patron
Tera Patron

@MaharshiC 

try this and enhance further

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()) {
    // Get associated workflow assignment
    var order = task.associated_work_order_number;
    var workflowSysId = order.assocaited_work_request_number.workflow;
    
    // Get planned duration from workflow assignment
    var workflow = new GlideRecord('x_iem_tqa_workflow_assignment');
    workflow.get(workflowSysId);
    var planned_duration = workflow.planned_site_acceptance_duration_for_auto_closure; // Number of days
    
    if (!planned_duration) continue; // Skip if no planned duration

    // Get sys_created_on and validation dates
    var createdDate = new GlideDateTime(task.sys_created_on);
    var validationDate = order.tc_validation_end_by_planned; // Should be GlideDateTime or date string

    // Add planned_duration days to both dates
    var createdPlus = new GlideDateTime(createdDate);
    createdPlus.addDays(planned_duration);

    var validationPlus = null;
    if (validationDate) {
        validationPlus = new GlideDateTime(validationDate);
        validationPlus.addDays(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 per your state values)
        task.state = 3; // Assuming 3 = Closed
        task.update();
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

Thanks for your reply. This line is not giving any result and my code is stop execution at this line.

 createdPlus.addDays(planned_duration);
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);
        createdPlus.addDays(planned_duration);   ///code stopping in this line
		gs.info('test115'+createdPlus);

        var validationPlus = null;
        if (validation) {
			
            validationPlus = new GlideDateTime(validation);
            validationPlus.addDays(planned_duration);
			gs.info('test3'+task.sys_id+validationPlus);
        }

        // Find the later of the two dates
        var maxDate = createdPlus;
        if (validationPlus && validationPlus.after(createdPlus)) {
			
            maxDate = validationPlus;
			gs.info('test2'+task.sys_id+maxDate);
        }

        // Check if now >= maxDate
        if (now.compareTo(maxDate) >= 0) {
            // Close the task (set state to Closed, e.g., state=3, adjust as per your state values)
          gs.info('test1'+task.sys_id);
          
        }
    }

}

  

@MaharshiC 

update as this

createdPlus.addDaysUTC(planned_duration);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

Tried it in background but it still did not work.

 var createdPlus = new GlideDateTime(created);
        gs.info('test25'+createdPlus);
        createdPlus.addDaysUTC(planned_duration);
        gs.info('test115'+createdPlus);

MaharshiC_0-1748345170791.png