- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 03:48 AM
Hi Team,
As per the requirement schedule job should change the incident state to 'Work in progress' if field date of 'follow up date' is less then current date and the incident state is one of 'awaiting user information' or 'awaiting vendor information'.
Follow up date is a date/time field.
This schedule run will run every morning at 7 am IST and will make the changes.
However the issue is this script is changing the incident state to WIP to those incident also where follow up date is greater then current date.
Below is my code that we are using.
var gr = new GlideRecord("incident");
gr.addEncodedQuery('stateIN9,-5,6^u_followupdateISNOTEMPTY');
gr.query();
while (gr.next()) {
var currDate = gs.now();
var followDate = gr.u_followupdate;
if (currDate > followDate) {
gr.state = '2';
gr.update();
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 03:51 AM
Hello @Prashant_13S ,
The issue with your script arises from comparing the currDate (current date) and followDate (follow-up date) as strings rather than date objects, which can lead to incorrect results.
Here’s a corrected version of your script
var gr = new GlideRecord("incident");
gr.addEncodedQuery('stateIN9,-5,6^u_followupdateISNOTEMPTY');
gr.query();
while (gr.next()) {
var currDate = new GlideDateTime();
var followDate = new GlideDateTime(gr.u_followupdate);
if (followDate < currDate) {
gr.state = '2';
gr.update();
}
}
If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.
Thanks,
Amitoj Wadhera
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 03:51 AM
Hello @Prashant_13S ,
The issue with your script arises from comparing the currDate (current date) and followDate (follow-up date) as strings rather than date objects, which can lead to incorrect results.
Here’s a corrected version of your script
var gr = new GlideRecord("incident");
gr.addEncodedQuery('stateIN9,-5,6^u_followupdateISNOTEMPTY');
gr.query();
while (gr.next()) {
var currDate = new GlideDateTime();
var followDate = new GlideDateTime(gr.u_followupdate);
if (followDate < currDate) {
gr.state = '2';
gr.update();
}
}
If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.
Thanks,
Amitoj Wadhera
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 05:12 AM - edited 05-28-2024 05:13 AM
Hi @Prashant_13S ,
If you want to do any query on dateTime field you need to change them first to new GlideDateTime(field). Please refer below code
var gr = new GlideRecord("incident");
gr.addEncodedQuery('stateIN9,-5,6^u_followupdateISNOTEMPTY');
gr.query();
while (gr.next()) {
var currDate = new GlideDateTime();
var followDate = new GlideDateTime(gr.u_followupdate);
if (currDate > followDate) {
gr.state = '2';
gr.update();
}
}
You can also use GlideDateTime predefined function to compare two dates gs.info(currDate.compareTo(followDate));
You can refer below code
var initDate = new GlideDateTime("2011-08-01 12:00:00");
var compDate1 = new GlideDateTime("2011-08-01 12:00:00");
var compDate2 = new GlideDateTime("2011-07-31 12:00:00");
var compDate3 = new GlideDateTime("2011-08-04 16:00:00");
gs.info(initDate.compareTo(compDate1)); // Equals (0)
gs.info(initDate.compareTo(compDate2)); // initDate is after compDate2 (1)
gs.info(initDate.compareTo(compDate3)); // initDate is before compDate3 (-1)
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak