You could end up in a scenario when a form validation needs to occur based on values in a multi-row Variable Set (MRVS) on the same form, just like how I did.
The problem here is, MRVS variables are not global, hence values in them cannot be fetched on the form for other validations. I know what you are thinking... "Let me mark the variable set as global". Sadly, MRVS cannot be marked global, for obvious reasons that ServiceNow will best know.
So, we need to mark the MRVS 'Global' if we have plans of validating things based on it.
The key is:
GLOBAL_VAR = {g_form: g_form};
Declare this in one of your onLoad client scripts and voala! other OnChange scripts can use this global variable and fetch the entire field-value details of the g_form (in our case - MRVS) as a JSON object!
first script:
GLOBAL_VAR = {g_form: g_form};
second script:
GLOBAL_VAR.g_form.getValue("field_name");
You might have to do a JSON parse of the fetched value to get details.
ServiceNow considers GLOBAL_VAR to be DOM, and hence you will have to set Isolate scripts=false to both the scripts (the one that creates it, and all others that utilize it)
On the contrary, If the validation is to be done WITHIN the MRVS, as in showing/hiding/making variables read-only right inside each row that opens as a flyout window, we can define client scripts applicable to the variable set and not the catalog item. Doing this is sufficient for the validation to work since the variables are accessed within the scope of the variable set, doesn't matter if their variables are global or not.
g_service_catalog is a powerful API offered by ServiceNow to access data on the MRVS modal while validating items from the catalog variables too. A simple code snippet:
//helps access variables on the MRVS modal
functiononLoad(){
if (g_service_catalog.parent.getValue("field")=="value"){ g_form.setValue("mrvs_field1","mrvs_fieldvalue"); g_form.setVisible("mrvs_field2","false");
}
}
To operate on a mrvs from a workflow/server script, the following functions might come in handy:
var mrvs = current.variables.mrvs_name; //This will return a json
var rowCount = mrvs.getRowCount(); //returns list of rows on the mrvs
var row = mrvs.getRow(i); //returns a row
row.var_name; //returns the variable value
To update a mrvs variable:
row.var_name = 'val'; //sets the mrvs variable value
This should be followed by a mrvs update:
g_form.setValue('mrvs_name') = JSON.stringify(modified_mrvs_var);
or
current.variables.mrvs_name = JSON.stringify(modified_mrvs_var);
Comments