Background script g_form is not defined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 01:02 PM
Running a background script against our location table, parent field (parent is a reference field)
What I am trying to do is search for any locations that have a parent field, with 32chars exactly delete them (I need to add the delete portion also). When I run the below background script. I get the following error -
Can anyone help? Thanks
Evaluator: org.mozilla.javascript.EcmaError: "g_form" is not defined. Caused by error in script at line 6
This line -
==> 6: var parent = g_form.getValue('parent');
var grloc = new GlideRecord('cmn_location');
grloc.query();
while(grloc.next()){
var parent = g_form.getValue('parent');
if (parent.toString().length == 32) {
gs.log('Location name has a parent with +32chars:' + grloc.name);
}
}
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 01:06 PM
Change your script to the following:
var grloc = new GlideRecord('cmn_location');
grloc.query();
while(grloc.next()){
var parent = grloc.getValue('parent');
if (parent.toString().length == 32) {
gs.log('Location name has a parent with +32chars:' + grloc.name);
}
}
You cannot use g_form in a server-side script.
Please mark this as correct/helpful if it resolved your issue!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 01:27 PM
The way allen has wrote the parent variable, doesnt error out but, doesnt seem to return the results I need. It seems to return all rows, and its actually looking at the locations sys id, not the parent length for some reason. The parent reference is also just a reference right back to the location table itself. Not sure if that makes a difference here.
When I run your version Elijah I get the error below
Evaluator: org.mozilla.javascript.EcmaError: Cannot convert null to an object. Caused by error in script at line 7 4: while(grloc.next()){ 5: 6: var parent = grloc.getValue('parent'); ==> 7: if (parent.toString().length == 32) { 8: gs.log('Location name has a parent with +32chars:' + grloc.name); 9: } 10: }

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 01:33 PM
Try:
var grloc = new GlideRecord('cmn_location');
// You need to add a query if you don't want all rows
// Something like below
// grloc.addQuery("field_name", "field_value")
grloc.query();
while(grloc.next()){
var parent = grloc.parent + "";
if (parent.length == 32) {
gs.log('Location name has a parent with +32chars:' + grloc.name);
}
}
If you don't want all rows returned you need to uncomment out the addQuery and add parameters.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 01:46 PM
Ok. But what I want returned is only the data with 32 chars as the parent name. It is not possible to do that in the first query, that would need to be passed to another query somehow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 01:53 PM
I think this will work. Thanks
var grloc = new GlideRecord('cmn_location');
grloc.query();
while(grloc.next()){
var parent = grloc.parent.name;
if (parent.toString().length == '32') {
// gs.log('String length is: ' + parent.length());
gs.log('Location name has a parent with +32chars: ' + grloc.name + 'parent name: ' + parent);
}
}