Scheduled job not working for a scoped app

AmritaT
Tera Expert

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!

1 ACCEPTED SOLUTION

Barbara L - Gli
Tera Guru

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!

View solution in original post

4 REPLIES 4

Barbara L - Gli
Tera Guru

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!

Thanks for the quick response. I removed the double equals as you mentioned and it worked 🙂 

Rogers Cadenhe1
Giga Guru

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

Ankur Bawiskar
Tera Patron
Tera Patron

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

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