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.

Custom Assignment Rule Script

browne26
Tera Contributor

Hello! 

 

I am trying to troubleshoot a custom script that should be assigning VITs to the group in the variable u_event_group. This script is assigning records that do not have this custom dictionary entry and/or the dictionary entry is empty. If anyone could review this script and identify potential bugs, I would greatly appreciate it. 

 

var gr = new GlideRecord('server');
var ciSID = current.sys_id;

gr.addQuery('sys_id', ciSID);
gr.query();

if (gr.next()) {
    var eventGroup = gr.getValue('u_event_group');

    if (eventGroup && eventGroup !== '') {
        current.assignment_group = eventGroup;
    } else {
        // No group found
    }
} else {
    // No group found
}
1 ACCEPTED SOLUTION

Medi C
Giga Sage
Giga Sage

@browne26 

Your script looks like fine, but there are a couple of areas that could lead to unexpected behavior, especially when you're checking for the u_event_group value

 

var gr = new GlideRecord('server');
var ciSID = current.sys_id;

gr.addQuery('sys_id', ciSID);
gr.query();

if (gr.getRowCount() > 0) {
    gr.next();  // Make sure we're processing the first record
    var eventGroup = gr.getValue('u_event_group');

    if (eventGroup) {
        current.assignment_group = eventGroup;
    } else {
        gs.log('No event group found for CI: ' + ciSID);
        // Optionally handle case where no event group is found
    }
} else {
    gs.log('No server record found for CI: ' + ciSID);
    // Optionally handle the case where no server record is found
}

 

 

  • Added getRowCount() to check for multiple records and ensure you're only processing one.
  • Simplified the check for an empty eventGroup by just using if (eventGroup).
  • Added gs.log calls to provide visibility into the script's behavior.

 

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

View solution in original post

1 REPLY 1

Medi C
Giga Sage
Giga Sage

@browne26 

Your script looks like fine, but there are a couple of areas that could lead to unexpected behavior, especially when you're checking for the u_event_group value

 

var gr = new GlideRecord('server');
var ciSID = current.sys_id;

gr.addQuery('sys_id', ciSID);
gr.query();

if (gr.getRowCount() > 0) {
    gr.next();  // Make sure we're processing the first record
    var eventGroup = gr.getValue('u_event_group');

    if (eventGroup) {
        current.assignment_group = eventGroup;
    } else {
        gs.log('No event group found for CI: ' + ciSID);
        // Optionally handle case where no event group is found
    }
} else {
    gs.log('No server record found for CI: ' + ciSID);
    // Optionally handle the case where no server record is found
}

 

 

  • Added getRowCount() to check for multiple records and ensure you're only processing one.
  • Simplified the check for an empty eventGroup by just using if (eventGroup).
  • Added gs.log calls to provide visibility into the script's behavior.

 

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.