Difference between two dates

praveen_rajan
Tera Contributor

Hi all, using if condition in workflow need to find the difference between two dates (start date and end date entered in catalog variable), if the difference date is more than 10 days, need to assign any value to assignment group in ServiceNow.

2 ACCEPTED SOLUTIONS

AnubhavRitolia
Mega Sage
Mega Sage

Hi @praveen_rajan 

 

You need to write advance code as below:

 

var gdt1 = GlideDateTime(variable.<start_date>);
var gdt2 = GlideDateTime(variable.<start_date>);
var duration1 = gs.dateDiff(gdt1.getDisplayValue(), gdt2.getDisplayValue(), false); // this will return the difference in seconds

var tendays = 10*24*60*60; // Convert 10 days into seconds

if(duration1>tendays)
{
return 'yes';
} else {
return 'no';
}

 

Now based on If or Else you can Set Value of Assignment Group.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

Steven Parker
Giga Sage

I would have a Run Script at the beginning of my workflow where I calculate the date difference and then convert it into days and store that information in a scratchpad variable so it can be used throughout the workflow.  Like the following:

 

 

var startDate = new GlideDateTime(current.variables.start_date);
var endDate = new GlideDateTime(current.variables.end_date);

var diff = gs.dateDiff(startDate.getDisplayValue(),endDate.getDisplayValue(),true); //This would return seconds
var days = diff/86400; //Convert the seconds to Days

workflow.scratchpad.numberOfDays = days;

//If you wanted to assign the RITM to a assignment group you could do this:
//if(workflow.scratchpad.numberOfDays > '10'){
//	current.assignment_group = '679434f053231300e321ddeeff7b12d8';//Help Desk Assignment Group
//} else {
//	current.assignment_group = '477a05d153013010b846ddeeff7b1225';//App Enginge Studio Team
//}

 

 

 From there, in the Script section of a Catalog Task (check Advanced checkbox), you could set the assignment group like this:

 

if(workflow.scratchpad.numberOfDays > '10'){
	task.assignment_group = '679434f053231300e321ddeeff7b12d8';//Help Desk Assignment Group
} else {
	task.assignment_group = '477a05d153013010b846ddeeff7b1225';//App Enginge Studio Team
}

 

 

Just replace the sys_ids above with the sys_ids from your instance for the groups you want to assign to.


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

View solution in original post

4 REPLIES 4

AnubhavRitolia
Mega Sage
Mega Sage

Hi @praveen_rajan 

 

You need to write advance code as below:

 

var gdt1 = GlideDateTime(variable.<start_date>);
var gdt2 = GlideDateTime(variable.<start_date>);
var duration1 = gs.dateDiff(gdt1.getDisplayValue(), gdt2.getDisplayValue(), false); // this will return the difference in seconds

var tendays = 10*24*60*60; // Convert 10 days into seconds

if(duration1>tendays)
{
return 'yes';
} else {
return 'no';
}

 

Now based on If or Else you can Set Value of Assignment Group.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Please Elaborate how to set the assignment group based on If else condition.

 

Steven Parker
Giga Sage

I would have a Run Script at the beginning of my workflow where I calculate the date difference and then convert it into days and store that information in a scratchpad variable so it can be used throughout the workflow.  Like the following:

 

 

var startDate = new GlideDateTime(current.variables.start_date);
var endDate = new GlideDateTime(current.variables.end_date);

var diff = gs.dateDiff(startDate.getDisplayValue(),endDate.getDisplayValue(),true); //This would return seconds
var days = diff/86400; //Convert the seconds to Days

workflow.scratchpad.numberOfDays = days;

//If you wanted to assign the RITM to a assignment group you could do this:
//if(workflow.scratchpad.numberOfDays > '10'){
//	current.assignment_group = '679434f053231300e321ddeeff7b12d8';//Help Desk Assignment Group
//} else {
//	current.assignment_group = '477a05d153013010b846ddeeff7b1225';//App Enginge Studio Team
//}

 

 

 From there, in the Script section of a Catalog Task (check Advanced checkbox), you could set the assignment group like this:

 

if(workflow.scratchpad.numberOfDays > '10'){
	task.assignment_group = '679434f053231300e321ddeeff7b12d8';//Help Desk Assignment Group
} else {
	task.assignment_group = '477a05d153013010b846ddeeff7b1225';//App Enginge Studio Team
}

 

 

Just replace the sys_ids above with the sys_ids from your instance for the groups you want to assign to.


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

how to insert (return "yes" and return "no") inside your code??