Mandate the field based on the particular keyword in the List type fiedl

Nagesh5
Tera Contributor

Hello Community,

 

we have list collector variable on the catalog item and where user can select multiple values and We are looking for the way if particular keyword identified then another variable need to make mandatory. We have below code and it's not working can anyone help where we are missing the logic?

 

Client Script :--------------------

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

    var ga = new GlideAjax("checkUser");
    ga.addParam("sysparm_name""getUsers");
    ga.addParam("sysparm_value", g_form.getValue("user_check"));
    ga.getXML(userCheck);

    function userCheck(response) {

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

        //var ouput = JSON.parse(answer);

        if(answer.includes("albert")){

            g_form.setMandatory("Requester",true);
        }

    }
}
 
Script Include ------------------
 
var checkUser = Class.create();
checkUser.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUsers: function() {

        var checkValues = this.getParameter("sysparm_value");
        //var arr = checkValues(",");
        //for (var i = 0; i < arr.length; i++) {
            var croles = new GlideRecord('sys_user');
            croles.addQuery('sys_id', checkValues);
            croles.query();
            var arrRoles = [];
            if (croles.next()) {

                gs.info("*******Users" + croles.getDisplayValue());
                //Fill array
                //var arr =croles.split(",");
                arrRoles.push(croles.getDisplayValue().toString());

            //}
        }
        return arrRoles.toString();

    },

    type: 'checkUser'
});
 
Thanks,
Nagesh
14 REPLIES 14

Community Alums
Not applicable

@Nagesh5 ,

Please edit this client Script 

 

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

    //Type appropriate comment here, and begin script below
	// var user = g_form.getValue("additional_assignee_list");
    var ga = new GlideAjax("checkUser");
    ga.addParam("sysparm_name", "getUsers");
    ga.addParam("sysparm_value", newValue);
	alert("HERE IN CS = " + newValue);
    ga.getXML(userCheck);

    function userCheck(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        //var ouput = JSON.parse(answer);
        if (answer.includes("Abraham")) {
             g_form.setMandatory("Requester", true);
			alert("IN IF Condition");
        }
		else{
			alert('Else');
			g_form.setMandatory("Requester", false);
		}
    }
}

 

Now if it enter in else condition that field is no more mandatory. 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

It's working perfectly and how to hide this field if list collector not having this keyword ? 

Community Alums
Not applicable

@Nagesh5 ,

In Else part of client script add this line

g_form.setDisplay('user_check',false);  // backend value of field user_check

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

Apologies for the confusion it's should be hidden until that value available in list collector at the time form load as well because if list collector is empty then also it's appearing

Community Alums
Not applicable

@Nagesh5 ,

You can add that line on the top of client script

g_form.setDisplay('user_check',false);

or

you can also make that field hidden you can add in if condition 

g_form.setDisplay('user_check',true);

 

You can do in any of way

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak