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

Hi @Ankur Bawiskar ,

This is the entire code . It is working fine till this line -- gs.info('test25'+createdPlus). It completely stops in this line -- createdPlus.addDaysUTC(planned_duration);  .Even I tried to enter a static value like -- createdPlus.addDaysUTC(12);  still it doesnot work. Only if I completely comment out that line then the code moves on to next line. This is done in custom scope. Is there anything like addDays() does not work in custom scope

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.addDaysUTC(planned_duration);
		gs.info('test115'+createdPlus);

        var validationPlus = null;
        if (validation) {
			
            validationPlus = new GlideDateTime(validation);
            validationPlus.addDaysUTC(planned_duration);
			gs.info('test3'+task.sys_id+validationPlus);
        }
        gs.info('test113'+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 

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

rohansargar
Kilo Guru

Hello @MaharshiC 

Try using this script,

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 created = task.sys_created_on.getGlideObject(); // original creation date
    var validation = task.associated_work_order_number.tc_validation_end_by_planned; // GlideDateTime or null

    // Default to 0 in case planned duration is missing
    var planned_duration = 0;
    var workflowassign = task.associated_work_order_number.assocaited_work_request_number.workflow;

    var workflow = new GlideRecord('x_iem_tqa_workflow_assignment');
    if (workflow.get(workflowassign) && workflow.planned_site_acceptance_duration_for_auto_closure) {
        planned_duration = parseInt(workflow.planned_site_acceptance_duration_for_auto_closure);
    }

    // Add planned_duration to created date
    var created_plus_duration = new GlideDateTime(created);
    created_plus_duration.addDays(planned_duration);

    // Add planned_duration to validation date (only if it's not empty)
    var validation_plus_duration = null;
    if (validation) {
        validation_plus_duration = new GlideDateTime(validation);
        validation_plus_duration.addDays(planned_duration);
    }

    // Determine max of the two dates
    var latestDate = created_plus_duration;
    if (validation_plus_duration && validation_plus_duration.after(created_plus_duration)) {
        latestDate = validation_plus_duration;
    }

    // Check if now >= latestDate
    if (now.compareTo(latestDate) >= 0) {
        gs.info("Closing task: " + task.number + " as current time >= " + latestDate);
        task.state = 3; // Example: 3 might be "Closed", adjust as per your state model
        task.update();
    }
}

Mark helpful if your issue got resolved

Best Regards,

Rohan