How to select Multiple Values in a Select Box Variable without updating ACL or in a List Collector Variable without creating a custom Table for options.

Laukik Udpikar
Tera Expert

Hello Experts,

I need to know how to select Multiple Values in a Select Box Variable without updating ACL or in a List Collector Variable without creating a custom Table for options.

I saw multiple articles which mentioned either updating an ACL or creating a custom table both are not suitable in my case. If there's any other way to achieve the same please comment you response. 

Thank you.

1 ACCEPTED SOLUTION

To prevent duplicates from being entered, you basically have to read the current value of the MRVS and loop through the rows to see if the new value has already been used.  Getting the current value of the MRVS works differently in the Service Portal, so first we need an onLoad Catalog Client Script running on the Catalog Item, not within the MRVS.

function onLoad() {
	if (this) {//we only need to do this for Service Portal
		//We need to make the g_form object for the parent item available from the MRVS window
		this.cat_g_form = g_form;
	}
}

Now when you create an onChange Catalog Client Script within the MRVS when Vulnerability Type changes, it will be able to get the value of the already-submitted rows in both Service Portal and the native UI.

 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var mrvs = '';
    if (this) { //Service Portal method
        mrvs = this.cat_g_form.getValue('mrvs_internal_name'); //internal name of your MRVS
    } else { //native UI method
        mrvs = parent.g_form.getValue('mrvs_internal_name');
    }
	if(mrvs.length>2){//native UI empty MRVS = '[]'
		var obj = JSON.parse(mrvs);
		for (var i = 0; i < obj.length; i++) {
			if(obj[i].vulnerability_type == newValue){//replace with your variable name
				g_form.clearValue('vulnerability_type');
				alert('Vulnerability Type already exists in this table.');
			}
		}
	}
}

You can add an alert on mrvs to show the JSON formatting of the MRVS value.  It's a little easier to work with MRVS values in server script, so if you have later requirements needing to do something with the contents of the MRVS, try to do it in a workflow Run Script or Business Rule.  The idea is similar to what's shown in the onChange script, but you don't need to parse it.  So to get the value and loop through the elements would be something like this.

var mrvs = current.variables.mrvs_internal_name;
var rowCount = mrvs.getRowCount();
for(var i=0; i<rowCount; i++){
  var row = mrvs.getRow(i);
  gs.info('This is the value of the variable in row ' + i + ': ' + row.vulnerability_type); 
}

You can also add rows or update values with this same approach.  More than you need to do on this project, but good to keep in mind for future use.

View solution in original post

15 REPLIES 15

To prevent duplicates from being entered, you basically have to read the current value of the MRVS and loop through the rows to see if the new value has already been used.  Getting the current value of the MRVS works differently in the Service Portal, so first we need an onLoad Catalog Client Script running on the Catalog Item, not within the MRVS.

function onLoad() {
	if (this) {//we only need to do this for Service Portal
		//We need to make the g_form object for the parent item available from the MRVS window
		this.cat_g_form = g_form;
	}
}

Now when you create an onChange Catalog Client Script within the MRVS when Vulnerability Type changes, it will be able to get the value of the already-submitted rows in both Service Portal and the native UI.

 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var mrvs = '';
    if (this) { //Service Portal method
        mrvs = this.cat_g_form.getValue('mrvs_internal_name'); //internal name of your MRVS
    } else { //native UI method
        mrvs = parent.g_form.getValue('mrvs_internal_name');
    }
	if(mrvs.length>2){//native UI empty MRVS = '[]'
		var obj = JSON.parse(mrvs);
		for (var i = 0; i < obj.length; i++) {
			if(obj[i].vulnerability_type == newValue){//replace with your variable name
				g_form.clearValue('vulnerability_type');
				alert('Vulnerability Type already exists in this table.');
			}
		}
	}
}

You can add an alert on mrvs to show the JSON formatting of the MRVS value.  It's a little easier to work with MRVS values in server script, so if you have later requirements needing to do something with the contents of the MRVS, try to do it in a workflow Run Script or Business Rule.  The idea is similar to what's shown in the onChange script, but you don't need to parse it.  So to get the value and loop through the elements would be something like this.

var mrvs = current.variables.mrvs_internal_name;
var rowCount = mrvs.getRowCount();
for(var i=0; i<rowCount; i++){
  var row = mrvs.getRow(i);
  gs.info('This is the value of the variable in row ' + i + ': ' + row.vulnerability_type); 
}

You can also add rows or update values with this same approach.  More than you need to do on this project, but good to keep in mind for future use.

Hello Brad,

I tried creating both the catalog client scripts 

1. onLoad Catalog Client Script on the Catalog Item

I also made the UI type = All, and Isolate Script = false.

function onLoad() {
	if (this) {//we only need to do this for Service Portal
		//We need to make the g_form object for the parent item available from the MRVS window
		this.cat_g_form = g_form;
	}
}

 

2. onChange Catalog Client Script within the MRVS

I also made the UI type = All, and Isolate Script = false.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var mrvs = '';
    if (this) { //Service Portal method
        mrvs = this.cat_g_form.getValue('vulnerability_type_vs'); //replace with the internal name of your MRVS
    } else { //native UI method
        mrvs = parent.g_form.getValue('vulnerability_type_vs');
    }
	var obj = JSON.parse(mrvs);
	for (var i = 0; i < obj.length; i++) {
		if(obj[i].vulnerability_type_select_box == newValue){//replace with your variable name
			g_form.clearValue('vulnerability_type_select_box');
			alert('Vulnerability Type already exists in this table.');
		}
	}
}

 

I can still select same options on the MRVS and submit the request.

find_real_file.png

 

Am I missing something? Please let me know if you need to see any other parts.

Thank you for your help.

Sorry, I forgot that Service Portal doesn't like parsing empty values.  I added an if condition prior to the var obj in my script above.  If this wasn't what was preventing the clearValue and alert from happening, try adding an alert after the 'for', before the 'if' line to show what it thinks the existing value is.

alert(obj[i].vulnerability_type_select_box)

If this doesn't trigger, go back to before the new if and add an alert on mrvs to make sure that is getting populated.

Hello Brad,

Thank you so much, It is working as expected now. I'm going to run some more tests just out of curiosity and will get back to you if I've any doubts. I'll mark this as correct. I'm yet to check the workflow run-script solution you suggested; to check if in future if i need to create task based on each type how can I achieve it. 

Really appreciate your support on this one. 🙂 

 

PS: sorry for the late reply, I was busy with other things and could not debug it properly.

You are welcome.