Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Unable to update field using scheduled job

divyanaraya
Tera Contributor
Hi ,
 
I have written the below scheduled job to update a field as true. But it is not happening. Please correct me.
 
var today = new GlideDate();
var reqItemsToNotify = [];
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()) {
        if (itemOptionGr.sc_item_option.value == today) {
            itemOptionGr.request_item.u_usb_access_revoke = true;
            itemOptionGr.update();
        }
    }
    
3 REPLIES 3

Marco Nappo_
Mega Guru

Let's review and correct your ServiceNow scheduled job script. There are a few common issues in your code:

  1. Date Comparison:
    itemOptionGr.sc_item_option.value and today are not directly comparable. GlideDate and String types need to be handled carefully.

  2. Field References:
    The way you reference fields in related tables may not be correct.

  3. 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();
    }
  }
}

mohdarbaz
Kilo Guru

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.

Robert H
Mega Sage

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