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

Rhonda9
Tera Expert

Thank you for the resources...

This is what I have and it's still not working for me:

Client Script with service offering being the onChange field Name

function onChange(control, oldValue, newValue, isLoading) {
    // Check if the form is loading or if no new value is selected
    if (isLoading || !newValue) {
        return;
    }
alert('This is an alert message.');
    // Check if the selected value is "ACD" or "Call Center"
    if (newValue === 'ACD' || newValue === 'Call Center') {
        var locationSysId = g_form.getValue('location'); // Get the location field value

        // Ensure the location field is not empty
        if (locationSysId) {
            // Call the Script Include
            var ga = new GlideAjax('LocationUtils');
            ga.addParam('sysparm_name', 'getLocationDetails');
            ga.addParam('sysparm_location_id', locationSysId);
            ga.getXMLAnswer(function(response) {
                // Parse the response
                var result = JSON.parse(response);
                if (result) {
                    // Set the Assignment Group and Assigned To fields
                    g_form.setValue('assignment_group', result.u_assignment_group);
                    g_form.setValue('assigned_to', result.u_assigned_to);
                } else {
                    g_form.addErrorMessage('Could not retrieve location details.');
                }
            });
        } else {
            g_form.addErrorMessage('Please select a location.');
        }
    }
}
 
Script Include
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }

    // Call the Script Include if the service offering is ACD or Call Center
    if (newValue === 'ACD' || newValue === 'Call Center') {
        var locationSysId = g_form.getValue('location'); // Get the location field value

        if (locationSysId) {
            // Create a GlideAjax instance
            var ga = new GlideAjax('LocationUtils');
            ga.addParam('sysparm_name', 'getLocationDetails');
            ga.addParam('sysparm_location_id', locationSysId);
            ga.getXMLAnswer(function(response) {
                var result = JSON.parse(response);
                if (result) {
                    // Set the Assignment Group and Assigned To fields
                    g_form.setValue('assignment_group', result.u_assignment_group);
                    g_form.setValue('assigned_to', result.u_assigned_to);
                } else {
                    g_form.addErrorMessage('Could not retrieve location details.');
                }
            });
        } else {
            g_form.addErrorMessage('Please select a location.');
        }
    }
}
 

You need to use the sys_id of the service offering, not the name since this is a reference field the value is a sys_id.  I've changed your alert to show you the value of the service offering selected.  In your first script you were getting the location of the caller, this script is getting the incident location.  If this change was intended the rest of the client script should be fine.  Added an alert on response to confirm that is the expected value.

function onChange(control, oldValue, newValue, isLoading) {
    // Check if the form is loading or if no new value is selected
    if (isLoading || !newValue) {
        return;
    }
    alert('Service offering = ' + newValue);
    // Check if the selected value is "ACD" or "Call Center"
    if (newValue === 'sys_id' || newValue === 'sys_id') {
        var locationSysId = g_form.getValue('location'); // Get the location field value

        // Ensure the location field is not empty
        if (locationSysId) {
            // Call the Script Include
            var ga = new GlideAjax('LocationUtils');
            ga.addParam('sysparm_name', 'getLocationDetails');
            ga.addParam('sysparm_location_id', locationSysId);
            ga.getXMLAnswer(function(response) {
                alert(response);
                // Parse the response
                var result = JSON.parse(response);
                if (result) {
                    // Set the Assignment Group and Assigned To fields
                    g_form.setValue('assignment_group', result.u_assignment_group);
                    g_form.setValue('assigned_to', result.u_assigned_to);
                } else {
                    g_form.addErrorMessage('Could not retrieve location details.');
                }
            });
        } else {
            g_form.addErrorMessage('Please select a location.');
        }
    }
}

If it's still not working post the Script Include (client script was posted twice above)

 

Thank you,  I copied your script and added the sys id's and  I am now  getting the message "Could not retrieve location details".

Here is the Script Include

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }

    // Call the Script Include if the service offering is ACD or Call Center
    if (newValue === 'ACD' || newValue === 'Call Center') {
        var locationSysId = g_form.getValue('location'); // Get the location field value

        if (locationSysId) {
            // Create a GlideAjax instance
            var ga = new GlideAjax('LocationUtils');
            ga.addParam('sysparm_name', 'getLocationDetails');
            ga.addParam('sysparm_location_id', locationSysId);
            ga.getXMLAnswer(function(response) {
                var result = JSON.parse(response);
                if (result) {
                    // Set the Assignment Group and Assigned To fields
                    g_form.setValue('assignment_group', result.u_assignment_group);
                    g_form.setValue('assigned_to', result.u_assigned_to);
                } else {
                    g_form.addErrorMessage('Could not retrieve location details.');
                }
            });
        } else {
            g_form.addErrorMessage('Please select a location.');
        }
    }
}

The (old) Client Script still re-posted.

Okay, I think I accidently copied the client script into the Script Include.

Here it is....

var LocationUtils = Class.create();
LocationUtils.prototype = {
    initialize: function() {
    },

    // This function is callable from the client script
    getLocationDetails: function() {
        var locationId = this.getParameter('sysparm_location_id'); // Get the location sys_id
        var locationGr = new GlideRecord('cmn_location');

        if (locationGr.get(locationId)) {
            // Create an object to hold the results
            var result = {
                assignment_group: locationGr.u_assignment_group, // Adjust field name as necessary
                assigned_to: locationGr.u_location_support // Adjust field name as necessary
            };
            return JSON.stringify(result); // Return the result as a JSON string
        }

        return JSON.stringify(null); // Return null if not found
    },

    type: 'LocationUtils' // Ensures the class is identified as 'LocationUtils'
};

// Allow the Script Include to be callable from the client
// Ensure that the "Client Callable" checkbox is checked in the Script Include record