Client script to make a field mandatory for particular assignment groups

Kevin Parker
Tera Expert

I belatedly realised that using UI Policy to make a field mandatory for particular assignment groups doesn't really work until the form is saved as there is no On Change option. I have been trying to locate knowledge articles that will allow me to borrow a Client Script but without much success.

 

My particular challenge is that I only want the field to be mandatory for groups that all sit under the same Parent group. Is this possible? I am ok to have the field visible to all, but just need to be Mandatory if the incident state is Resolved and the assignment group is a member of the 'All DW Groups' parent group.

10 REPLIES 10

Hi @Kevin Parker ,

 

Yes i have checked above in some cases it dosent work because here we have to check both onchange of resolved and onchange of assignment group ,

 

so what you can do is create a script include and call that in client script ,

the catch here is you have to write 2 onchange client script on 2 fields(assignment group and state) with same code 

 

Client script 1:

swathisarang98_0-1709657665204.png

Client script 2:

swathisarang98_1-1709657715049.png

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        // g_form.setMandatory('description', false);
        return;
    }
    
    var groupSysId = g_form.getValue('assignment_group');
    var si = new GlideAjax('getGroupPraentDetail');
    si.addParam('sysparm_name', 'getParent');
    si.addParam('sysparm_id', groupSysId);
    si.getXML(getResponse);

}

function getResponse(response) {
    var ans = response.responseXML.documentElement.getAttribute('answer');
    alert(ans);
	var stateChnaged = g_form.getValue('state');
    if (ans == '8a5055c9c61122780043563ef53438e3' && stateChnaged == 6) //hardware (parent group name)
    {
        g_form.setMandatory('description', true);
    } else if (ans != '8a5055c9c61122780043563ef53438e3') {
        g_form.setMandatory('description', false);
    }

}

 

 

Script include : make it client callable

 

swathisarang98_2-1709657763812.png

 

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

    getParent: function() {


        var sysId = this.getParameter('sysparm_id');
        var gr = new GlideRecord('sys_user_group');
        gr.addQuery('sys_id', sysId);
        gr.query();
        if (gr.next()) {
            var parentSysid = gr.parent;
            
        }
        gs.info("line number 14 " + parentSysid);
        return parentSysid;


    },



    type: 'getGroupPraentDetail'
});

 

 

Note: remove all logs and alert

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang