Need to auto-populate fields on an incident form before saving.

Rhonda9
Tera Expert

 

I want to know how to auto-populate the assigned to field with the incident location location support and auto populate the assignment group field with the incident location's assignment group if the user selects ACD or Call Center as the service offering before saving the form.     I created a before business rule and it is only populating after the form is saved. 

 

See screenshots below

 

Rhonda9_1-1727830325038.png

 

Rhonda9_0-1727830288171.png

 

1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @Rhonda9 

 

In ServiceNow, an "onChange" client script is used to execute JavaScript code when a specific field's value changes. This can be useful for dynamically updating other fields, validating input, or displaying messages based on the user's selections.

 

Since you are trying to auto populate fields on form, onChange Client script could be the possible solution.

 

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thank You

Juhi Poddar

 

 

 

 

View solution in original post

8 REPLIES 8

Juhi Poddar
Kilo Patron

Hello @Rhonda9 

 

In ServiceNow, an "onChange" client script is used to execute JavaScript code when a specific field's value changes. This can be useful for dynamically updating other fields, validating input, or displaying messages based on the user's selections.

 

Since you are trying to auto populate fields on form, onChange Client script could be the possible solution.

 

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thank You

Juhi Poddar

 

 

 

 

Kamal17
Kilo Sage

@Rhonda9 

You need to 

1. Write an onchange client script on 'Service Offering' field.

2. When selected value is call center/acd and if the location field is not empty, then call a script include from this onchange client script and pass the sys_id of the selected location as input.

3. In the script include, create a function to query the location table with the input sys_id.

4. From the queried record, get the value of location assignment group and assigned to information which you need and store it in a object type variable. E.g. var result = {"assignment_group": locationrecord groupSysid, "assigned_to": locationrecord userSysid}

5. Return the result from the script include. In the client script, parse the result to get the assigned to and assignment group values and set the necessary fields in the form.

 

Since the above change is triggered by an onChange client script, you'll see the changes happening in real time.

 

Refer to the below link to understand how to call script include from client script.

https://developer.servicenow.com/dev.do#!/reference/api/xanadu/client/c_GlideAjaxAPI

 

PS: Hit "Correct", "Helpful" or "Like" depending on the impact of the response.

Regards,

Kamal S

Thank you , I will definitely give this a try.

This is what I have and it does not seem to work: 

Client Script

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
var LocationUtils = Class.create();
LocationUtils.prototype = {
    initialize: function() {
    },

    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'
};