The CreatorCon Call for Content is officially open! Get started here.

How to Limit a Reference Variable in an MRV for the same value selected in the first Row

Peter Williams
Kilo Sage

good morning everyone,

so i have a trick request to configure,

i have an MRV with a reference variables that is looking up on the cmn_department table

 

i want to have the ability to auto fill the other row users will select with the same value they selected in the first row of the MRV.

 

Example:

 

the Department Column u selected Office Expenses

 

PeterWilliams_0-1712240365919.png

 

i want every other row to only show Office Expenses and not have the users be able to select anything else but Office Expenses base on their first row

so all other rows will only have the Office Expense and nothing else

 

is this possible with an onChange script?

 

1 ACCEPTED SOLUTION

maroon_byte
Mega Sage

Consider having Department variable outside of your mrvs in the form as well.. After the user selects this form Department variable and is adding first mrvs row, use onLoad script within mrvs to set the read-only mrvs Department field using below script:

 

var dept = g_service_catalog.parent.getValue('department');
    g_form.setValue('mrvs_dept', dept);

View solution in original post

21 REPLIES 21

What did not work about it?  Did it not save the value?  Did it not set the value?

I found that part of @maroon_byte suggestion will work probably better for what I suggested.

You can use g_service_catalog.parent.getValue("MRV_NAME") to get the value of the MRV and it will be in JSON format.

 

So you can just use JSON.parse to turn it into an object, check to see if there is at least one row in it and if it is then use the value for the department to set the var on the popup when it opens using the same basic idea that I posted for the onLoad.

 

//In an onLoad script for the MRV
var mrv = JSON.parse(g_service_catalog.parent.getValue("MRV_NAME"));
if(mrv && mrv[0] && mrv[0].NAME_OF_DEPARTMENT_VAR_IN_MRV) {
   g_form.setValue("department", mrv[0].NAME_OF_DEPARTMENT_VAR_IN_MRV);
   g_form.setReadOnly("department", true);
}

 

Here is some better code and this one I did test with one of our MRV's and it worked for me.

 

	var field = "NAME_OF_DEPARTMENT_FIELD_IN_MRV";
	var mrvVal = g_service_catalog.parent.getValue("international_travel_request");
	if(mrvVal) {
		var mrv = JSON.parse(g_service_catalog.parent.getValue("NAME_OF_MRV"));
		if(mrv && mrv[0] && mrv[0][field]) {
			g_form.setValue(field, mrv[0][field]);
			g_form.setReadOnly(field, true);
		}
	}

He needs to set Department in all rows the same... so no need to set the Department in first row only.

 

If don't want to have Department in the form, another option is to use browser's Local/Session storage in client scripts to set and retrieve values. I feel, its bit flaky and tedious to develop/maintain and it wont work in Incognito mode.

 

maroon_byte_0-1712591482614.png

 

I was simply showing how to set the value in all rows to what the first one has.  But you do make a good point.  Why even have the department as a question in the MRV if every row is going to have the same value.  Move it outside the MRV as a question on the cat item and then the user just sets it one time.  No need to do anything else and that simplifies it.