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

@MaharshiC 

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.

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

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);

@MaharshiC 

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.

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

@Ankur Bawiskar ,

 

Yes i am passing integer field only

@MaharshiC 

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.

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