How can I add an option to a dropdown menu that is dependent on another field? (Using g_form.addOption).

DSTN
Kilo Contributor

I am stuck pretty heavily at the moment with adding an option to a dropdown list that is dependent on another field.

I have one field called 'Attack Vectors' with a subcategory type field called 'Type'. 

I have 'Type' set as dependent to 'Attack Vectors' and I have one value in the 'Type' field labeled 'Other' that I want to populate for multiple Attack Vectors and not have to make 6 different Other choices just to accomodate this. For example:

1. If 'Attack Vector' is 'Vector 1', in the dropdown for field 'Type' add the 'Other' option to this dropdown.

2. If 'Attack Vector' is 'Vector 2', in the dropdown for field 'Type' add the 'Other' option to this dropdown.

Remember that field 'Type' is a dropdown that has Dependent values populating based on the value in 'Attack Vector'.

 

I've tried a couple of options in Client Scripts and I have one that partially works, but it only adds the 'Other' option AFTER I have made a selection in the 'Type' dropdown. I need it to populate before I ever even open that dropdown. 

In my first client script, I have it running as a onChange on the 'Attack Vector' field, but nothing happens at all. If I change the onChange field to 'Type', it does work, but only after I make a selection. I need this option to be added when 'Attack Vector' changes AND BEFORE I open the 'Type' field. 

Here is the current code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	var attackVector = g_form.getValue('u_attack_vectors_rcas');
	var type = g_form.getValue('u_type');
	var typeValues = ['Social Engineering', 'Malware', 'Misuse'];
	
		if(typeValues.indexOf(attackVector) >= 0) {
			console.log("Found a match, attack vector: " + attackVector + " and type of: " + type);
			g_form.addOption('u_type', 'Other', 'Other');
		}
		else {
			console.log("Didn't find a match");
		}
	
}
3 REPLIES 3

Archana Reddy2
Tera Guru

Hi,

Try using the following client script.

Type: onChange 

Field name: Attack Vector

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	var id = document.getElementById('your_table_name.u_type');
	var ch = new GlideRecord('sys_choice');
	ch.addQuery('name',g_form.getTableName());
	ch.addQuery('element','u_type');
	ch.addQuery('label','Other');
	ch.addQuery('value','other');
	ch.query();
	if(ch.next())
		{
		if(isLoading || newValue == '')
			{
			ch.setValue('dependent_value','');
			ch.setValue('sequence',(id.options.length)+1);
		}
		else
			{
			ch.setValue('dependent_value',newValue);
			ch.setValue('sequence',(id.options.length)+1);
		}
		ch.update();
	}		
	}

Mark the answer as Correct/Helpful based on its impact.

Thanks,

Archana

Thanks for the reply, but aren't we unable to use GlideRecord in Client Scripts? 

Not exactly. We can use GlideRecord in Client Scripts as well. As far as I know, if there are any other ways other than 'using Glide Record in Client Script' to achieve your requirement, we better avoid using it. In this case, I believe we can use.

Hope this answered your question.

Thanks,

Archana