Make Multi-row variable set mandatory and limit number of entries

Eash1
Tera Expert

Hello Experts,

I am using Multi row variable set in my catalog item and it works perfect but i am stuck on below 2 requirements.

Does anyone have any insights to it ?

 

  • How to make sure at-least one entry is selected as part of Multi row variable set. I tried to do a on-submit and later read through docs that it is not supported
  • How to limit number of entries ? like just 10

Thank you,

Easwar

1 ACCEPTED SOLUTION

LaurentChicoine
Tera Guru

Hi Easwar,

I was looking to make a sure at least one entry exist in the multi row variable set. I don't know if you found a way to it.

I came with the solution of a on submit client script (I think what the doc refers to is for on submit that would run after each entry being added). Here is a code snippet that I was able to use to make sure there is at least one row:

function onSubmit() {
	if(g_form.getValue('internal_name_of_variable_set') == '[]'){
		g_form.addErrorMessage('You must enter at least 1');
		return false;
	}
}

g_form.getValue returns a JSON of the variable set, so simply by checking if it is an empty array, I was able to block submission in that case.

 

I think you could solve your second requirement with this (parsing the JSON and doing a length validation: array.length < 11). However this is not the best User experience as you are checking this on submit while the user already entered more than 10 entries.

View solution in original post

25 REPLIES 25

Hi @Laurent Chicoine ,

 

Thanks for the solution. I am in Orlando and trying to execute the same onSubmit catalog client script but it is not working. Can you please help here.

Hi, I did a few tests in Orlando and I'm pretty sure one of the scripts in this thread should work. In which interface is it not working (Platform/Service Portal/Other)?

Depending on the UI and the version, when empty g_form.getValue('internal_name_of_variable_set') should return an empty array, an empty array as a JSON (string) or an empty string.

To troubleshoot, you can add this line of code in the onSubmit client script to see the value of the variable set on submit. To see the log you have to open your browser developer tools (usually opened with F12)

console.log(g_form.getValue('internal_name_of_variable_set'))

Hi Laurent,

I was trying something like below. 

I need to make sure if at least one row is added to multi row variable set and then display another variable on the catalog form. But unsure how to achieve this since multi row variable sets are not available on variable name in catalog client scripts or UI policies. 

Please suggest if any pointers. 

Thanks,

Gopi

When a row is added you can run an onSubmit client script on the multi row variable set, that is an opportunity to make your field show. There is also the g_form.onUserChangeValue function, but from my test it does not seem to run in Service Catalog for the Platform UI, however it does work in the Service Portal.

Both of these cases only allow you to run script on addition of a row, I didn't find any supported method to run on removal of a row.

So if you only need the Service Portal, I suggest to go that route, simply integrate the function callback in a onLoad client script from the catalog item and listen for changes on the fieldname matching your MRVS internal name.

 

If you also need support for the platform, you can create a callback function on an onLoad script on the catalog item, for example:

function onLoad() {
	var currentWindow = window || _self; // Support for service portal and platform
    if (currentWindow) {
		currentWindow.onMRVSChange = function(){
			g_form.setDisplay('variable', true);
		};
    }
}

Then in the onSubmit script of the variable set:

function onSubmit() {
    var mainWindow = parent || _self; // Support for service portal and platform
    if (mainWindow && typeof mainWindow.onMRVSChange === 'function') {
        mainWindow.onMRVSChange();
    }
}

Both script should have the "Isolate script" attribute to false.

As you can see that method is less ideal, since the platform version requires to play with IFrames and get retrieve the parent object. The portal version requires to use _self because window is blocked in the Service Portal.

Thank you Laurent 🙂

It seems to be working as required after updating your scripts. 

Regards,

Gopi