The CreatorCon Call for Content is officially open! Get started here.

Having issue with Before business rule not populating fields on incident form

Rhonda9
Tera Expert

Hello, I created a business rule that is not working as expected.   I was asked to auto assign incidents if the service offering is "acd" or "call center" on the UI side to the affected user's location location support  to the Assigned to field and the location assignment group to the assignment group field fore saving the form.  What am I doing wrong?

 Insert and update is checked

 condition :    current.service_offering == 'ACD' || current.service_offering == 'Call Center'

(function executeRule(current, previous /*null when async*/) {

// Check if the affected user (caller_id) is set
if (current.caller_id) {
// Fetch the affected user's record
var userGr = new GlideRecord('sys_user');
if (userGr.get(current.caller_id)) {
// Get the user's location
var userLocation = userGr.location; // Assuming this is the field for location

// Fetch the location record
var locationGr = new GlideRecord('cmn_location');
if (locationGr.get(userLocation)) {
// Set the Assignment Group field to the location's Location Support
current.assignment_group = locationGr.u_location_support; // Adjust field name if necessary

// Set the Assigned To field to the location's Assignment Group
current.assigned_to = locationGr.u_assignment_group; // Adjust field name if necessary
}
}
}

})(current, previous);

Thank you in advance

1 ACCEPTED SOLUTION

Still making progress - almost there.  If these are the correct custom field names on the location table, you need to force the sys_ids/values to a string type.  Here are two methods for doing that

assignment_group: locationGr.u_assignment_group.toString(), // Adjust field name as necessary
assigned_to: locationGr.getValue('u_location_support')

 

View solution in original post

17 REPLIES 17

Brad Bowman
Kilo Patron
Kilo Patron

Hi Rhonda,

I can see that the script is not running because your condition is never true.  Service offering is a reference field, and as such stores the sys_id of the record as it's value.  You can select the name using Filter conditions, or change your condition to

 current.service_offering.name == 'ACD' || current.service_offering.name == 'Call Center'

In the script, you can dot-walk to the caller's location's support so no GlideRecords are needed

function executeRule(current, previous /*null when async*/ ) {

    // Check if the affected user (caller_id) is set
    if (current.caller_id) {
		if (current.caller_id.location) {
			if (current.caller_id.location.u_location_support) {
				// Set the Assignment Group field to the location's Location Support
				current.assignment_group = current.caller_id.location.u_location_support;
                // Set the Assigned To field to the location's Assignment Group
                current.assigned_to = current.caller_id.location.u_assignment_group;
            }
        }
    }
})(current, previous);

The last line doesn't make sense - assigned_to is a person, and location.u_assignment_group sounds like a group

 

Rhonda9
Tera Expert

Thank you for your reply, I tried the script that you shared but it is not allowing me to save.   Oh you are right about the last line, it should ....     Also, I cant save the script you shared with me.

 

function executeRule(current, previous /*null when async*/ ) {

    // Check if the affected user (caller_id) is set
    if (current.caller_id) {
        if (current.caller_id.location) {
            if (current.caller_id.location.u_location_support) {
                // Set the Assignment Group field to the location's Location Support
                current.assignment_to = current.caller_id.location.u_location_support;
                // Set the Assigned To field to the location's Assignment Group
                current.u_assignment_group = current.caller_id.location.u_assignment_group;
            }
        }
    }
})(current, previous);

Rhonda9
Tera Expert

The script is working but only after the assignment group is already populated and I save the incident.  I want the automation to happen before saving.

 

 

A Business Rule will run before or after insert/update - that really means before the database action, so you need to submit/save the record either way.  It sounds like you want to see the Assigned to and Assignment group fields populate on the form when the Service offering field is populated or changes.  To do this you will need an onChange Client Script.  Client scripts can't dot-walk to related fields, so your onChange Client Script will need to use GlideAjax to call a Script Include.  This is an often-used tool to have in your belt.  Here is an excellent guide on how to do this.

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

  In your case If the newValue (service offering sys_id) is one of the two that belong to ACD or Call Center, you will pass in the caller_id as a parameter.  The Script Include will run the GlideRecord from your first attempt and return the two custom fields for the client script to set on the form.  Give the scripts a try, and if you're stuck post each script using the insert code icon </> and we'll get it sorted.