Background script to update Change Requests producing nullpointerexception on gr.update
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2018 06:13 AM
I have a requirement to update all change requests with a worknote that includes a short note and also includes the name of the configuration item associated with the CR.
I've got a background script written that can successfully update CR's that have a configuration item with a note and the configuration item name.
I've discovered a large number of CR's that do not have configuration items entered into the CMDB_CI field. (A result of UI policies changing over time). Since I have CR's with no configuration item I need to add a different note to the worknotes of these CR's that advises no configuration item was ever selected.
Here is the script I'm running:
var gr = new GlideRecord('change_request');
var note = "This record did not have a Configuration Item attached.";
gr.addNullQuery('cmdb_ci');
gr.query();
while(gr.next()){
gr.work_notes = note;
gr.update();
}
When I run this script I get the following error:
JavaScript evaluation error on:
var gr = new GlideRecord('change_request');
var note = "This record has been modified as part of the CMDB clean-up project. This record did not have an attached Configuration Item at the time of CMDB clean-up.";
gr.addNullQuery('cmdb_ci');
gr.query();
while(gr.next()){
gr.work_notes = note;
gr.update();
}
Root cause of JavaScriptException: java.lang.NullPointerException
: java.lang.NullPointerException:
Any ideas on what is happening here?
- Labels:
-
Change Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2018 06:46 AM
Hmm...curious if you just try to gs.print the records as a test to see what show's up....wondering if that NullPointerException is due to either not working correctly or not finding anything (just saying). Can you do that to see if records show up?
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2018 06:57 AM
Good idea and interesting result. When I replace gr.update with gs.print I get the following:
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2018 07:13 AM
Still looking at some stuff, but I think that from what I'm finding, you can't addNullQuery for a reference field...which is why it's coming back as undefined. Per: https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=r_ScopedGlideRecordAddNullQuery_Stri... and other searching I've done.
Random stuff I'm seeing is saying to do maybe a regular addQuery for that field and then maybe do it this:
gr.addQuery('cmdb_ci', 'NULL');
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2018 08:20 AM
Allen,
Thanks for pointing me in the right direction.
gr.addQuery('cmdb_ci', 'NULL');
did not work, nor did
gr.addQuery('cmdb_ci, null);
However, after giving this some thought I realized why this is not working. To your point, cmdb_ci is a reference field, so to query it for a null value I should formulate my query as follows:
gr.addQuery('cmdb_ci.name', null);
Using the query above my background script is now executing without any error.
Thanks again for your guidance!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2018 08:29 AM
Glad you got it sorted out!
Feel free to mark any reply as Helpful/Correct, if applicable.
Take care!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!