- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2019 09:52 AM
I have created a scheduled job which should run periodically and check the DATECLOSED time on my form.
As per the requirement it should change the state to closed when the DATECLOSED is today.
Given below is my script :
function checkDate(){
var Today = new GlideDateTime().getDisplayValue();
var dateTime = new GlideRecord("x_sood2_contract_m_contract_management_table");
dateTime.addQuery('u_state', '==', '103');
dateTime.query();
while (dateTime.next()) {
var dateClosed = dateTime.u_date_time_closed.getDisplayValue();
//gs.info("Today = " +Today);
//gs.info("DateClosed = " +dateClosed);
if(Today >= dateClosed){
reAssignState(dateTime);
}
}
}
function reAssignState(dateTime){
//gs.addInfoMessage(dateClosed);
//gs.addInfoMessage("Working");
dateTime.u_state = 111;
dateTime.update();
}
checkDate();
Not sure where is the error here. Please advice. Thanks!
Solved! Go to Solution.
- Labels:
-
Scoped App Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2019 10:00 AM
At line 4, you only want a single equals sign in the query
dateTime.addQuery('u_state', '=', '103');
That's the only thing I see off hand, hope this helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2019 10:00 AM
At line 4, you only want a single equals sign in the query
dateTime.addQuery('u_state', '=', '103');
That's the only thing I see off hand, hope this helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2019 11:46 AM
Thanks for the quick response. I removed the double equals as you mentioned and it worked 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2019 10:12 AM
You're using getDisplayValue() to get a string representation of two date/time values (Today and u_date_time_closed) and then trying to compare them with a >= operator.
You can't do a date comparison on strings using >, <, >= or <=.
An alternative would be to query the table for records with a u_date_time_closed from today and state=103:
dateTime.addEncodedQuery(
'state=103^u_date_time_closedONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()'
);
This query should find only those records you want to update (assuming I understand the problem correctly).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2019 10:18 AM
Hi Amrita,
First of all few important things to keep in mind:
1) Don't use schedule jobs to update records instead use fix scripts
2) fix scripts give an advantage that they can be tracked i.e. they can be stopped from being executed
3) also in the fix script use try catch block; what happens using this is even if error is present while updating 1 record the next records will still get updated and the code won't break since you are catching the exception
4) always use setLimit(5) or setLimit(10) i.e. try to update first 5 or 10 records and then do bulk update; in this way you can verify whether update happened properly for those 5 or 10 records
function checkDate(){
try{
var Today = new GlideDateTime().getDisplayValue();
var dateTime = new GlideRecord("x_sood2_contract_m_contract_management_table");
dateTime.addQuery('u_state', '=', '103'); // use single comparison
dateTime.query();
while (dateTime.next()) {
var dateClosed = dateTime.u_date_time_closed.getDisplayValue();
//gs.info("Today = " +Today);
//gs.info("DateClosed = " +dateClosed);
if(Today >= dateClosed){
reAssignState(dateTime);
}
}
}
catch(ex){
gs.info("Error for checkDate is:" + ex);
}
}
function reAssignState(dateTime){
//gs.addInfoMessage(dateClosed);
//gs.addInfoMessage("Working");
try{
dateTime.u_state = 111;
dateTime.update();
}
catch(ex){
gs.info("Error for reAssignState is: " + ex);
}
}
checkDate();
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader