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

Thanks Shloke.

 

Please find the below screenshot

 

find_real_file.png

Okay in that case update your Script Include as below:

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

    type: 'getChoice'
});

Try it now.

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

 

Many thanks for your help this has worked perfect.

Hi,

Can you please mark my answer as correct and close this thread for others.

Regards,

Shloke

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

Regards,
Shloke

Sagar S
Tera Contributor

HI @shloke04 ,

 

We have requirement as below,

 

The List collector 'policy' field may contain one or more records, if one of the record has the "u_mnpi_info_required" column(from the reference table)  is checked (true) then i should show the "u_was_the_information_mnpi" on the form . If none of the records in the list collector has not checked(true) the column "u_mnpi_info_required" , then i should hide the "u_was_the_information_mnpi" on the form.

 

any help on how to achieve this, currently I'm writing onchange client script and script include but it is not working.

 

Thanks