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

No its not working. to fix this I just made variable hidden on variable level post that I added below script now it's working perfectly.

function userCheck(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        //var ouput = JSON.parse(answer);
        if (answer.includes("Abel")) {
            g_form.setVisible("requester", true);
            g_form.setMandatory("requester", true);
        } else {

            g_form.setMandatory("requester", false);
            g_form.setVisible("requester", false);
        }
        if (answer.includes("Abraham")) {
            g_form.setMandatory("test_value", true);
        } else {

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

any issue with this approach?

 

 

Community Alums
Not applicable

@Nagesh5 ,

Please try this piece of code this is working for me 

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");
			g_form.setDisplay('additional_assignee_list',true);  
        }
		else{
			alert('Else');
			g_form.setMandatory("Requester", false);
			g_form.setDisplay('additional_assignee_list',false);  
		}
    }
}

 

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

Thanks and Regards 

Sarthak

it's not working..let me explain if keyword is "Abraham" then requester field is visible and it should be mandatory at the time of on change it's working perfectly.

 

1.if item form loads it's should be hidden 

2.If list collect does not have the value then also it should be hidden

Community Alums
Not applicable

@Nagesh5 ,

1.if item form loads it's should be hidden 

Hide the variable by checking the hidden checkbox so at the time of form load it is hidden

SarthakKashyap_0-1723795319000.png

 

 

2.If list collect does not have the value then also it should be hidden

 

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");
	alert("newValue.length + " + newValue.length);
    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);
		alert("Length " + answer.length);
        if (answer.includes("Abraham")) {
            // g_form.setMandatory("Requester", true);
			alert("IN IF Condition");
			g_form.setDisplay('additional_assignee_list',true);  
        }
		if(answer == null || answer.length == 0){
			alert("Inside nukll");
			g_form.setDisplay('additional_assignee_list',false);
			g_form.setMandatory("Requester", false);
		}
		else{
			alert('Else');
			g_form.setMandatory("Requester", false);
			g_form.setDisplay('additional_assignee_list',false);  
		}
    }
}

 

 

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

Thanks and Regards 

Sarthak

the problem is If I hidden from variable level post submission of the request it is hiding in RITM level if that list collector having the value