Unable to update field using scheduled job
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 07:18 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 07:21 AM - edited 05-20-2025 07:23 AM
Let's review and correct your ServiceNow scheduled job script. There are a few common issues in your code:
Date Comparison:
itemOptionGr.sc_item_option.value and today are not directly comparable. GlideDate and String types need to be handled carefully.Field References:
The way you reference fields in related tables may not be correct.Updating Related Records:
You are trying to update a field on the request_item table from a sc_item_option_mtom GlideRecord. This requires fetching and updating the related record.
Let's address each point and provide a corrected version.
1. Date Comparison Fix
today is a GlideDate object. You should use getValue() to get the string value for comparison.
itemOptionGr.sc_item_option.value may be a string or GlideDateTime, depending on the field type. Use getDisplayValue() or getValue() accordingly.
2. Correct Field References
To access the related request_item record, you need to use a GlideRecord on sc_req_item and update the field there.
3. Correct Script
Here’s a corrected version of your script:
var today = new GlideDate().getValue(); // 'yyyy-MM-dd'
var itemOptionGr = new GlideRecord('sc_item_option_mtom');
itemOptionGr.addQuery('request_item.cat_item.name', 'USB Request');
itemOptionGr.addQuery('request_item.stateNOT IN','19,21');
itemOptionGr.addQuery('sc_item_option.item_option_new.question_text', 'End Date');
itemOptionGr.query();
while (itemOptionGr.next()) {
var endDate = itemOptionGr.sc_item_option.value; // This is usually a string 'yyyy-MM-dd'
if (endDate == today) {
// Get the related request item record
var reqItemGr = new GlideRecord('sc_req_item');
if (reqItemGr.get(itemOptionGr.request_item.toString())) {
reqItemGr.u_usb_access_revoke = true;
reqItemGr.update();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 07:44 AM
Hi @divyanaraya ,
Please use the updated script:
var today = new GlideDate();
var todayStr = today.getByFormat('yyyy-MM-dd'); // ✅ Convert today's date to string format for comparison
var itemOptionGr = new GlideRecord('sc_item_option_mtom');
itemOptionGr.addQuery('request_item.cat_item.name', 'USB Request');
itemOptionGr.addQuery('request_item.stateNOT IN','19,21');
itemOptionGr.addQuery('sc_item_option.item_option_new.question_text', 'End Date');
itemOptionGr.query();
while (itemOptionGr.next()) {
var endDate = itemOptionGr.sc_item_option.value; // ✅ Get the value of the End Date field
if (endDate == todayStr) { // ✅ Compare string values instead of objects
var reqItem = itemOptionGr.request_item.getRefRecord(); // ✅ Get the full Request Item record
reqItem.u_usb_access_revoke = true; // ✅ Set the custom field to true
reqItem.update(); // ✅ Save the change to the Request Item
}
}
If my response helped, please mark it correct/helpful and close the thread so that it benefits future readers.
Regards,
Mohd Arbaz.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 10:02 AM
Hello @divyanaraya ,
This can be achieved using a much simpler approach:
var today = new GlideDate();
var grReq = new GlideRecord('sc_req_item');
grReq.addQuery('cat_item.name', 'USB Request');
grReq.addQuery('state', 'NOT IN', '19,21');
grReq.query();
while (grReq.next()) {
if (grReq.variables.end_date == today) {
grReq.setValue('u_usb_access_revoke', '1');
grReq.update();
}
}
I have assumed your "End Date" question's internal name is "end_date". If not then please adjust the script to use the correct name.
Regards,
Robert