Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

on change client script for list collector

sai190
Tera Contributor

hi

My requirment is i want to display a field and it should be mandatory, i want to display a field when defects is selected in a list collector variable, but iam getting the field also when i select the other variable in the list collector? i have written on change client script but it is displaying the new field for two values where as i want to display value only when defects is selected?

kindly look into screenshots. 

the new field should not be visible while selecting incident but what happening is that if i select defects and remove defects and select incident the new field is not hiding

1 ACCEPTED SOLUTION

Hey Sai,
Did the solution worked for you?

Cheers..!
Tushar

Mark it as helpful or correct, If Applicable


Cheers..!

Happy Learning:)

Tushar

View solution in original post

12 REPLIES 12

eumak
Tera Guru

Hello sai,
As it is list collector we have to use the for loop for checking the sys_id of the list collector. Let me take you through this.

Steps we are going to follow:

1.Create a system property and paste the sysid into it and use that in the script include.
2. Create a onchange client script with the code below.
3. Create a client callable script include paste the code below.

I have done things on the group list you can replace with yours.

1. create the system property & give the sys in the value section for which you want to make mandatory field. 
2. Client Script - on change on the field "group List"

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    alert(newValue); //for checking whether we are getting the correct sys_id from the list collector
    var ga = new GlideAjax('getListValue');
    ga.addParam('sysparm_name', "checkchoice");
    ga.addParam('sysparm_list', newValue); //List collector field sending to server side
    ga.getXML(Populate);

    function Populate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 1 || answer == '1') {
            alert("the answer is " + answer); //for checking the return value
            g_form.setDisplay('company', true);
            g_form.setMandatory('company', true);
        }
    } //Type appropriate comment here, and begin script below   

}



find_real_file.png

3. Script Include

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

	checkchoice:function(){
	var list_value = this.getParameter('sysparm_list');
	var flag = 0;
	var myArray = list_value.split(',');
	for (var i = 0; i < myArray.length; i++) {
		if (myArray[i] == gs.getProperty('list_choice_default')) {//Calling the property
		//if (myArray[i] == '75d20d9f87ea8110b23b4087cebb3579') { //if you want to check with the sys id of the list collector variable
		flag = 1;
			break;
}
	}
		return flag;
	
	},
	
	
    type: 'getListValue'
});


find_real_file.png

 

OUTPUT
I have selected group 2 in list & company field got mandatory

find_real_file.png

 

Mark it helpful & correct, If applicable

Cheers..!
Happy Learning
Tushar

Mark it as helpful or correct, If Applicable


Cheers..!

Happy Learning:)

Tushar

You can replace your for..in loop in your script include to use Array.prototype.some. It's shorter and I find it a little easier to read. You'd also need to update the client script to check for true/false instead of 1/0. 

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

	checkchoice:function(){
	var list_value = this.getParameter('sysparm_list');
	var myArray = list_value.split(',');
	return myArray.some(function(choice) { return choice == gs.getProperty('list_choice_default');});
	
	},	
	
    type: 'getListValue'
});

Hey @Chris Helming ,

Thanks for the suggestion.

Cheers..!
Tushar

Mark it as helpful or correct, If Applicable


Cheers..!

Happy Learning:)

Tushar

sai190
Tera Contributor

sys id do i need to paste of the list collector or the the defects sys id which i need to paste on system property?

you have to take the sys_id of defects-> which ever you are entering to the list collector.

Cheers..!
Tushar

Mark it as helpful or correct, If Applicable


Cheers..!

Happy Learning:)

Tushar