Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

When assignment group type is Test integration user then category and subcategoryis mandatory

adityaashok
Tera Contributor

 

Hello Community,

We have the following requirement:

 

When creating an Incident, if the Assignment Group type is “Test Integration User”, then Category and Subcategory must be mandatory.

 

If the Assignment Group is changed later and the new group does not belong to our scope (i.e., it is not of type Test Integration User), then Category and Subcategory should no longer be mandatory.

 

We attempted to implement this using UI Policies, Catalog Client Scripts, and UI Policy script actions, but none of these approaches worked as expected.

Can anyone help me on this?

Thanks!!


 

1 ACCEPTED SOLUTION

Yogesh11bhatt
Tera Expert

Hi @adityaashok ,

This is a great requirement, but it is a classic ServiceNow "gotcha." The reason standard UI Policies, Catalog Client Scripts, and the getReference method fail here is due to the architecture of the Type field on the Group (sys_user_group) table.

The type field is a List field (glide_list), meaning a single group can have multiple types (e.g., ITIL, Agile, Test Integration User). Because it is a list of references, trying to read it on the client side usually returns a comma-separated string of 32-character Sys IDs, not the display names. Therefore, simple string comparisons like group.type == 'Test Integration User' will always evaluate to false.

The strictest, most reliable best practice to solve this is to use an Asynchronous GlideAjax call. This asks the server to evaluate the list field safely and return a simple true/false to the form.

Here is the step-by-step solution:

1. Create the Script Include (Server-Side)

This script securely checks the group's type without exposing raw Sys IDs to the client.

  • Name: GroupTypeAjax

  • Client callable: Check the box (Very Important!)

  • Script:

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

    checkGroupType: function() {
        var groupId = this.getParameter('sysparm_group_id');
        var targetType = 'Test Integration User'; // Ensure this matches your exact Type name
        
        if (!groupId) {
            return 'false';
        }

        var groupGr = new GlideRecord('sys_user_group');
        if (groupGr.get(groupId)) {
            // getDisplayValue gets the comma-separated string of names, avoiding Sys IDs
            var typeDisplay = groupGr.getDisplayValue('type'); 
            
            if (typeDisplay && typeDisplay.indexOf(targetType) > -1) {
                return 'true';
            }
        }
        return 'false';
    },

    type: 'GroupTypeAjax'
});

2. Create the Client Script (Client-Side)

This script runs when the Assignment Group changes, calls the Script Include, and updates the form based on the answer.

  • Table: Incident [incident]

  • Type: onChange

  • Field name: Assignment group

  • Script:

JavaScript
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    // If the group is cleared out, remove the mandatory requirement
    if (newValue === '') {
        g_form.setMandatory('category', false);
        g_form.setMandatory('subcategory', false);
        return;
    }

    // Call the server to check the group type
    var ga = new GlideAjax('GroupTypeAjax'); 
    ga.addParam('sysparm_name', 'checkGroupType');
    ga.addParam('sysparm_group_id', newValue);
    
    // Process the true/false response asynchronously
    ga.getXMLAnswer(function(response) {
        if (response === 'true') {
            g_form.setMandatory('category', true);
            g_form.setMandatory('subcategory', true);
        } else {
            g_form.setMandatory('category', false);
            g_form.setMandatory('subcategory', false);
        }
    });
}

 

View solution in original post

8 REPLIES 8

namanajain
Tera Expert

Hi @adityaashok 

Just to clarify — are you working with a Record Producer or the  normal Incident form? 
You mentioned a Catalog Client Script, which typically applies only to Record Producers.
Try this code:-
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }

 

    // Get Assignment Group reference
    g_form.getReference('assignment_group', function(group) {

 

        if (!group) {
            return;
        }

 

        // Check group type
        if (group.type == 'Test Integration User') {
            g_form.setMandatory('category', true);
            g_form.setMandatory('subcategory', true);
        } else {
            g_form.setMandatory('category', false);
            g_form.setMandatory('subcategory', false);
        }
    });
}

I think the core issue with the script lies in this specific line: if (group.type == 'Test Integration User') {

This will always evaluate to false in ServiceNow for two distinct reasons:

  1. getReference returns raw database values (Sys IDs): When you use getReference on the client side, the object it returns contains the raw database values, not the string display values. group.type will return a 32-character Sys ID, not the string "Test Integration User".

  2. The Type field is a List (glide_list), not a single Reference: A group can have multiple types (e.g., "ITIL", "Agile", "Test Integration User"). Because it is a list field, group.type will actually return a comma-separated string of Sys IDs (e.g., sys_id_1,sys_id_2,sys_id_3).

Comparing a comma-separated list of Sys IDs to the string 'Test Integration User' will always fail, meaning the category and subcategory will never become mandatory.

This will not work.

adityaashok
Tera Contributor

Hello @namanajain ,
Thanks for your quick response.
Working on normal incident form.

We already try the above script by adding log also but it's' not working every time it comes under the else part.

Hi @adityaashok 
The main problem is that assignment group provide name with spacing so it evaluates false every time 
Use instead :- g_form.getDisplayValue('assignment_group').trim()

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

 

    // Fetch Assignment Group reference
    var groupName = g_form.getDisplayValue('assignment_group').trim();
    console.log(groupName );
    console.log(groupName == "CSM Support");
    if (groupName == "CSM Support") {
            g_form.setMandatory('category'true);
            g_form.setMandatory('subcategory'true);
    } else {
            g_form.setMandatory('category'false);
            g_form.setMandatory('subcategory'false);
        }
}
 
Please make this as helpful and also accept it as an solution Please.
regards