Show certain fields and have them mandatory based on the selection in a List collector variable.

cstangoe
Mega Guru

I have requirement to make certain fields visible and be mandatory based the selection in the list collector variable.

List collector variable is populating from the choice table and there is 6 values which can be selected. These are

Default Cost Centre
Email Address
Job Level
Location Code
Manager
Other

So if someone selects Default Cost Centre in the right bucket, then a variable field called New Default Cost Centre will become visible and be mandatory, if Email Address is selected then variable field called New Email Address will become visible and be mandatory and so on.

Any 6 of these values in the list collector can be selected by the requester and the corresponding variable fields I would like to be visible and be mandatory.

I have tired using catalog UI policy and using the Catalog Conditions but it is not working correct as there is not a selected option in the choose operator so these variable fields are mandatory regardless if they have being selected or not

Any ideas on this, thanks

1 ACCEPTED SOLUTION

Okay got it. To clear things below are my answers:

 

When I move the choice value I require into the selected section the variable becomes a mandatory requirement but all of the variables are visible regardless of be selected or not.

Answer: You can hide all your variables when the form loads by adding the setDisplay as False in the on load section itself as shown below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
g_form.setDisplay('Variable Name',false);
        return;
    }
    if (newValue) {
        var ga = new GlideAjax('getChoice');
        ga.addParam('sysparm_name', 'validateChoice');
        ga.addParam('sysparm_choice', newValue);
        ga.getXML(getChoiceDisplay);
    }

    function getChoiceDisplay(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		if(answer == 'Default Cost Centre'){
			g_form.setDisplay('Variable Name', true);
            g_form.setMandatory('Variable Name', true);
		}else if(answer == 'Email Address'){
			g_form.setDisplay('Variable Name', true);
            g_form.setMandatory('Variable Name', true);
		}
	}

    }

find_real_file.png

To answer your second question, the Variable which are containing the below choices:

Default Cost Centre
Email Address
Job Level
Location Code
Manager
Other

These choices are coming from some table as mentioned in your post above, so field name is the column name in that table which is containing these Values in it. I assumed it may be "name" so you just need to replace that accordingly.

If you are unsure, share the screenshot of your Table List where I can see these records and I can help you which field you need to select there.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please use catalog client script as you can use single script for all the choice value and showing/hiding variables.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

shloke04
Kilo Patron

Hi,

Please find the steps below to achieve your requirement:

1) For a List collector variable it must definitely be referring to a Table from where you are populating your choices in the variable.

So you can write a Catalog Client script to both display and make your variables as mandatory for the choice option selected in the List Collector.

Sample script shared below which can be used, you just need to replace the sys_id correctly here:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue) {
        alert(newValue);
        if (newValue == 'Sys_id of Default Cost Center here') {
            g_form.setDisplay('Variable Name', true);
            g_form.setMandatory('Variable Name', true);
            }
            else if (newValue == 'sys_id of Email Address choice from choice table you can get') {
                g_form.setDisplay('Variable Name', true);
                g_form.setMandatory('Variable Name', true);
                }
                else if (newValue == 'sys_id of Job Level choice from choice table you can get') {
                    g_form.setDisplay('Variable Name', true);
                    g_form.setMandatory('Variable Name', true);
                    }
                }

                //Type appropriate comment here, and begin script below

            }

This works for me in my PDI instance. Also as a best practice would suggest instead of hard coding the Sys ID for your choice options you can do a Glide Ajax call and then check and have the validation you want.

For example create a Client callable Script Include as below:

1) Client Callable Script Include:

var getChoice = Class.create();
getChoice.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	validateChoice : function(){
		var getChoiceSelected = this.getParameter('sysparm_choice');
		var gr = new GlideRecord('Your Choice Table Name');
		gr.get(getChoiceSelected);
		return gr.name.toString(); // Replace name with the Field Name which contains the Display Name of your choices like Default Cost Centre OR Email Address
	},

    type: 'getChoice'
});

find_real_file.png

Now you need to update your catalog client script as below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue) {
        var ga = new GlideAjax('getChoice');
        ga.addParam('sysparm_name', 'validateChoice');
        ga.addParam('sysparm_choice', newValue);
        ga.getXML(getChoiceDisplay);
    }

    function getChoiceDisplay(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		if(answer == 'Default Cost Centre'){
			g_form.setDisplay('Variable Name', true);
            g_form.setMandatory('Variable Name', true);
		}else if(answer == 'Email Address'){
			g_form.setDisplay('Variable Name', true);
            g_form.setMandatory('Variable Name', true);
		}
	}

    }

 

So based on your time allotment you can go with either way but would suggest follow the best practice only.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hi Shloke

 

Thanks for reply to me, I have tried the 1st Catalog Client script and it is not working 100%

When I move the choice value I require into the selected section the variable becomes a mandatory requirement but all of the variables are visible regardless of be selected or not.

In the Client Callable Script Include can you confirm what you mean by the field name with contains the display name of the choice? Would this be the Choice table (sys_choice) there is the choices are listed or the variable which displays the list on the record producer?

 

Thanks

Okay got it. To clear things below are my answers:

 

When I move the choice value I require into the selected section the variable becomes a mandatory requirement but all of the variables are visible regardless of be selected or not.

Answer: You can hide all your variables when the form loads by adding the setDisplay as False in the on load section itself as shown below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
g_form.setDisplay('Variable Name',false);
        return;
    }
    if (newValue) {
        var ga = new GlideAjax('getChoice');
        ga.addParam('sysparm_name', 'validateChoice');
        ga.addParam('sysparm_choice', newValue);
        ga.getXML(getChoiceDisplay);
    }

    function getChoiceDisplay(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		if(answer == 'Default Cost Centre'){
			g_form.setDisplay('Variable Name', true);
            g_form.setMandatory('Variable Name', true);
		}else if(answer == 'Email Address'){
			g_form.setDisplay('Variable Name', true);
            g_form.setMandatory('Variable Name', true);
		}
	}

    }

find_real_file.png

To answer your second question, the Variable which are containing the below choices:

Default Cost Centre
Email Address
Job Level
Location Code
Manager
Other

These choices are coming from some table as mentioned in your post above, so field name is the column name in that table which is containing these Values in it. I assumed it may be "name" so you just need to replace that accordingly.

If you are unsure, share the screenshot of your Table List where I can see these records and I can help you which field you need to select there.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke