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

@Rhonda9 While skimming through your code, I found that your Script include is not a client callable script include. Due to this the call to LocationUtils will get failed. I recommend you to recreate the script include by making the client callable checkbox checked on the script include form.

Rhonda9
Tera Expert

Thanks, I updated to a client callable script include and getting same results.

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

@Rhonda9 Sent you a direct message, please check.

AndersBGS
Tera Patron
Tera Patron

Hi @Rhonda9 ,

 

Have you thought about utilizing assignment rules instead of a business rule? 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/