client script to validate glide list field value

Lakshmi Velu
Tera Contributor

i have scenario to make the field mandatory on change of the assignment group on INC table based on the below condition

there is a glide list field on group table referring to sys_user_group_list table which may have more than one comma separated values.

if the glide list field matches the specific vale on change of the assignment group field value on INC form , then make the field required field mandatory

i have tried creating onChange client script and Script include to achieve this but still finding an issue.

4 REPLIES 4

Brian Lancaster
Tera Sage

Can you share the script your tried?

Client Script below 

 

var group = new GlideAjax('getTypeData');
    group.addParam('sysparam_name', 'getType');
    group.addParam('sysparam_group', g_form.getValue('assignment_group'));
    group.getXML(updateField);



    function updateField(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        answer = JSON.parse(answer);

        if (groupID == 'SRM-PDXC') {
            g_form.setMandatory('u_vendor_resolver_group', true);
        }

    }
 
Script Include below
 
var getTypeData = Class.create();
getTypeData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    initialize: function() {},
    getType: function() {
        var obj = this.getParameter('sysparam_group');
        gs.log("group type is :" + obj);
        var grp = new GlideRecord('sys_user_group');
        grp.addQuery('sys_id', obj);
        grp.query();

        if (grp.next()) {
                var result = {
            "type" : grp.type.toString()
                };

        }

        return JSON.stringify(result);
    },
    type: 'getTypeData'



});

So you are just looking for one specific group being in the list of groups? If so you can just do a onChange script and use indexOf.

Rajesh Chopade1
Mega Sage

hi @Lakshmi Velu 

you can try bellow sample script to resolve your issue:

Script include:

// Script Include: CheckGroupValues
var CheckGroupValues = Class.create();
CheckGroupValues.prototype = {
    initialize: function() {},
    
    isGroupMatched: function(assignmentGroupSysId) {
        var matched = false;

        // GlideRecord to check the specific Glide List field on the group table
        var groupGr = new GlideRecord('sys_user_group');
        if (groupGr.get(assignmentGroupSysId)) {
            // Assuming 'u_glide_list_field' is your Glide List field
            var glideListValues = groupGr.u_glide_list_field.split(',');

            // Check for the specific value you're looking for
            for (var i = 0; i < glideListValues.length; i++) {
                if (glideListValues[i].trim() === 'specific_value') { // Replace 'specific_value' with your value
                    matched = true;
                    break;
                }
            }
        }
        return matched;
    },

    type: 'CheckGroupValues'
};

 

onChange Client script:

// onChange Client Script for the Assignment Group field
function onChange(control, oldValue, newValue) {
    if (newValue) {
        // Create a GlideAjax object
        var ga = new GlideAjax('CheckGroupValues');
        ga.addParam('sysparm_name', 'isGroupMatched');
        ga.addParam('sysparm_assignment_group', newValue);
        
        // Call the Script Include
        ga.getXMLAnswer(function(response) {
            if (response === 'true') {
                // If matched, make the field mandatory
                g_form.setMandatory('field_name', true); // Replace 'field_name' with the actual field name
                g_form.showFieldMsg('field_name', 'This field is mandatory because the assignment group is set.', 'error');
            } else {
                // If not matched, remove the mandatory requirement
                g_form.setMandatory('field_name', false);
                g_form.hideFieldMsg('field_name');
            }
        });
    }
}

 

i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

rajesh