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. 

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)