How can I set value to a variable that exists outside of MRVS from the MRVS

eyalabuhame
Giga Contributor

I have MRVS and one of the variables is Amount
I want onchange client script that once amount change it well set a variable outside of the MRVS that called total amount

eyalabuhame_0-1778399137762.png

tried g_form.setvalue // didnt work
tried something with 

 g_service_catalog.parent but found that only getvalue works so also didnt work
2 REPLIES 2

vaishali231
Kilo Sage

Hey @eyalabuhame 

In MRVS, normal g_form.setValue() does not work directly from inside the MRVS because the script runs in the MRVS iframe context, not the parent catalog item form.

Use window.parent with g_form from parent form.

Example:

function onChange(control, oldValue, newValue, isLoading) {

   if (isLoading || newValue == ''") {

       return;

   }
   // Get parent catalog item form
   var parentForm = window.parent.g_form;

   // Set value outside MRVS

   parentForm.setValue('total_amount', newValue);

}

But this only sets the latest row amount.

If you want TOTAL of all MRVS rows:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading) {
       return;
   }
   var parentForm = window.parent.g_form;

   // MRVS variable name

   var mrvsName = 'accommodation';

   // Get MRVS rows

   var mrvsData = parentForm.getValue(mrvsName);

   if (!mrvsData) {

       return;

   }

   var rows = JSON.parse(mrvsData);
   var total = 0;
   for (var i = 0; i < rows.length; i++) {

       var amt = parseFloat(rows[i].amount || 0);
       total += amt;

   }

   // Outside variable name

   parentForm.setValue('total_amount', total);

}

*********************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb



for some reason window.parent is not workong for me
sp_min.jsx?v=02-02-2026_1554:2994 (g_env) [SCRIPT:EXEC] Error while running Client Script "Try": TypeError: Cannot read properties of null (reading 'parent')

this is the error