Dynamically setting assignment group based on location

kelpel
Tera Contributor

Hello, 

 

Newbie here. In flow designer with a record producer, how do you dynamically set the assignment group when the location field is updated? 


Thanks. 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@kelpel There are multiple ways to addresses this. One of the ways is to create an onChange client script on location field and make a server side script include method call via GlideAjax. The server side script include will return the assignment group sys_id on the basis of location provided in the client script. 

 

Here is a sample for you.

 

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

    // Create a GlideAjax object and pass the name of the Script Include
    var ga = new GlideAjax('GetAssignmentGroupByLocation');
    
    // Add the location as a parameter (you can pass the sys_id or another value)
    ga.addParam('sysparm_name', 'getAssignmentGroup');
    ga.addParam('sysparm_location', newValue); // newValue is the sys_id of the selected location
    
    // Make the asynchronous call to the server
    ga.getXMLAnswer(function(response) {
        var assignmentGroupSysId = response;
        
        if (assignmentGroupSysId) {
            // Set the Assignment Group field with the returned sys_id
            g_form.setValue('assignment_group', assignmentGroupSysId);
        } else {
            // Clear the Assignment Group field if no group is returned
            g_form.clearValue('assignment_group');
        }
    }

 

Script Include.

 

var GetAssignmentGroupByLocation = Class.create();
GetAssignmentGroupByLocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getAssignmentGroup: function() {
        // Get the location parameter from the client-side call
        var locationSysId = this.getParameter('sysparm_location');
        
        // Define logic to retrieve assignment group sys_id based on location
        var assignmentGroupSysId = '';
        
        // Example: Query the assignment group based on location (adjust as needed)
        var gr = new GlideRecord('your_assignment_group_table'); // Replace with your table
        gr.addQuery('location', locationSysId); // Adjust the query as per your logic
        gr.query();

        if (gr.next()) {
            assignmentGroupSysId = gr.getValue('sys_id'); // Get the assignment group sys_id
        }

        // Return the assignment group sys_id back to the client-side script
        return assignmentGroupSysId;
    }

});

Hope this helps.

 

View solution in original post

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@kelpel There are multiple ways to addresses this. One of the ways is to create an onChange client script on location field and make a server side script include method call via GlideAjax. The server side script include will return the assignment group sys_id on the basis of location provided in the client script. 

 

Here is a sample for you.

 

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

    // Create a GlideAjax object and pass the name of the Script Include
    var ga = new GlideAjax('GetAssignmentGroupByLocation');
    
    // Add the location as a parameter (you can pass the sys_id or another value)
    ga.addParam('sysparm_name', 'getAssignmentGroup');
    ga.addParam('sysparm_location', newValue); // newValue is the sys_id of the selected location
    
    // Make the asynchronous call to the server
    ga.getXMLAnswer(function(response) {
        var assignmentGroupSysId = response;
        
        if (assignmentGroupSysId) {
            // Set the Assignment Group field with the returned sys_id
            g_form.setValue('assignment_group', assignmentGroupSysId);
        } else {
            // Clear the Assignment Group field if no group is returned
            g_form.clearValue('assignment_group');
        }
    }

 

Script Include.

 

var GetAssignmentGroupByLocation = Class.create();
GetAssignmentGroupByLocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getAssignmentGroup: function() {
        // Get the location parameter from the client-side call
        var locationSysId = this.getParameter('sysparm_location');
        
        // Define logic to retrieve assignment group sys_id based on location
        var assignmentGroupSysId = '';
        
        // Example: Query the assignment group based on location (adjust as needed)
        var gr = new GlideRecord('your_assignment_group_table'); // Replace with your table
        gr.addQuery('location', locationSysId); // Adjust the query as per your logic
        gr.query();

        if (gr.next()) {
            assignmentGroupSysId = gr.getValue('sys_id'); // Get the assignment group sys_id
        }

        // Return the assignment group sys_id back to the client-side script
        return assignmentGroupSysId;
    }

});

Hope this helps.

 

Mani A
Tera Guru

Write onChange ( location) client script ,make a glideAjax call and check which groups available for selected location and return it client-side and setValue for assignment variable

Amit Verma
Kilo Patron
Kilo Patron

Hi @kelpel 

 

If you want to set the assignment group based on location change in a record producer, please follow the approach suggested by @Sandeep Rajput .

 

Although, if you want to dynamically get the Assignment Group in a flow based on a location which is derived or calculated dynamically within the flow, I will suggest you to go with a Decision Table.

 

You can also achieve this with a flow by creating  assignment rules. Refer https://www.servicenow.com/community/developer-forum/set-assignment-groups-based-on-location/m-p/136... 

https://www.servicenow.com/community/developer-forum/set-assignment-groups-based-on-location/m-p/136...

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

kelpel
Tera Contributor

Much appreciated! Thank you!