Chalan B L
Giga Guru

How to Access parent/other variables from MVRS ?

As we all know that on click of an add button in MVRS we can access variables only which is in MVRS and there are many scenarios we would need some value from the parent/main form.

So here is a trick to get the parent variables from MVRS

Ex Scenario: In the main form, I had a reference field [Application profile] and onload of the MVRS, needed to access the application profile.

so I have written an onload client script by accessing the parent variable.
var vals = parent.g_form.getValue('application_profile');

Screenshot 1 - Application Profile

find_real_file.png

 

Screenshot 2 - Get this value onload of MVRS and sets the Pool Member Profile

find_real_file.png

 

screenshot 3 - OnLoad Client script on MVRS

 

find_real_file.png

 

 

Helpful Links:

https://community.servicenow.com/community?id=community_question&sys_id=5d28d390db272300fb115583ca961920
http://rubenferrero.com/servicenow/multi-row-variable-set-form-communication/

Comments
GeoffreyOptumOp
Tera Expert

Hello @Chalan B L 

What version of ServiceNow is required for this to work, please?

This does NOT seem to be working for me in PARIS.

parent is defined, but parent.g_form is UNDEFINED from the onLoad() event of my MRVS; therefore no getValue() function may be called on it. 

Thanks. 

 

ps:  When I attempt to reference your "Helpful Links", I get a message which says:  "The content you requested cannot be displayed right now. It may be temporarily unavailable, the link you clicked on may have expired, or you may not have permission to view this page."

isabellafredian
ServiceNow Employee
ServiceNow Employee

Hi, 

We introduced a feature in Quebec which gives a direct way to access the parent variables inside MRVS. We no longer need the 'workaround' suggested in this community post.

https://docs.servicenow.com/bundle/quebec-application-development/page/app-store/dev_portal/API_refe...

Jeffrey Siegel
Mega Sage

how do you access the parent from a MVRS? i keep trying and am getting no where.

parent.g_form.setValue('variable_name',true); 

g_service_catalog.parent.g_form.setValue('variable_name'),true);

 

neither works when my MRVS executes an onchange action.

mfhaciahmetoglu
Mega Sage

Hi, I tried as well but the parent.g_form does not seem to work.

Arturo P
Tera Contributor

Found the solution to this issue.

 

Replace parent.g_form.getValue() with g_service_catalog.parent.getValue()

 

g_service_catalog.parent.getValue

Returns the value of the specified field on the catalog item form when used in a client script on multi-row variable sets (MRVS).

 

Use this method when an MRVS modal is open for editing or creating and you want to modify data within the MRVS based on the value of a field on the parent catalog item form. For example, when you need to modify the contents of the cells within an MRVS based on a check box on the parent form. You can also use this method to access the data of other MRVS elements within the same parent form.

 

https://docs.servicenow.com/csh?topicname=g_service_catalogClientAPI.html&version=latest

Anish Singh
Tera Expert

@Jeffrey Siegel Did you ever got a solution for below?

 

parent.g_form.setValue('variable_name',true); 

g_service_catalog.parent.g_form.setValue('variable_name'),true);

none of them works.

How do I set a variable on main catalog form from inside the multirow variable?

Arturo P
Tera Contributor

@Anish Singh Have you tried g_service_catalog.parent.setValue('VARIABLE_NAME', 'VALUE');

 

Note the latest solution does not use g_form.

 

Documentation only references getValue, but I would hope it is the same for setValue.

https://docs.servicenow.com/csh?topicname=g_service_catalogClientAPI.html&version=latest

Anish Singh
Tera Expert

@Arturo P Yes, I tried the same but it gives the error in console.

 

AnishSingh_0-1717399451425.png

 

Jeffrey Siegel
Mega Sage

@Anish Singh So yes, I was able to get this done, I think I attacked an ant hill with a bazooka, but it's working so I'm not going to complain.

 

Hopefully you can use my logic below and come up with the right code to get the info you need from your MRVS.  If not, let me know i may be able to help further...

 

I created a widget, (sp_widget) that in the client controller, i am getting the info from my MRVS and then updating the form...  for my case, my MRVS is getting costs associated with an amortized item...  this is what i did:

I have a MRVS: 
provide_the_total_cost_breakout_by_year_as_shown_below (internal name)
I have a variable(Single Line Text): grand_total_of_items (name)

the widget, watches the MRVS for changes, adds up all the values on change, and then populates the grand total variable, and formats it as a currency (commas every 3 characters to give a visual of cost).

The MRVS has two values, a cost and a year.

 

 

 

 

 

api.controller = function($scope) {
    /* widget controller */
    var c = this;
    $scope.$watch(function() {
        //Internal Name of Variable Set 'item_details'
        return $scope.page.g_form.getValue('provide_the_total_cost_breakout_by_year_as_shown_below');
    }, function(value) {
        if (value) {
            var mrvsData = JSON.parse(value.replace(/'/g, ''));
            var totalCost = 0;

            for (var i = 0; i < mrvsData.length; i++) {
                var mrvs = mrvsData[i];
				var amount = mrvs.cost.substring(1);
				if(amount.indexOf(",")>=0){
					var amountArr = amount.split(",");
					var number = "";
					for (var j=0; j< amountArr.length; j++) {
						number += amountArr[j];;
					}
					amount = number;
				}
                totalCost += parseFloat(amount);
				
              
            }
						
            //Set Value in the Total Cost
			totalCost = parseFloat(totalCost).toFixed(2);
			totalCost = totalCost.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); // will add comma after 3 digits
			c.data.cost = totalCost;
           $scope.page.g_form.setValue('grand_total_of_items',totalCost);
        }
    });
};

 

 

 

 

 


Then I added the widget to the Record Producer, as a Variable with type custom, and pointed to it via Type Specifications tab, under the widget field.

Anish Singh
Tera Expert

@Jeffrey Siegel It worked like a charm!! Thank you very much!!

tushar_ghadage
Tera Contributor

Hi @Chalan B L ,

 

i  am doing the same thing 

here is the client script iniside of MRVS to access a field which is outside of mrvs.

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   
   var choice_type = parent.g_form.getValue('this_inquiry');
   alert('choice_type'+choice_type);
   
   if(choice_type == 'new_server')
   {  
     g_form.setDisplay('origin',true);
   
   }    
 
here i am make field called origin (choice field ) visible when you select new_server choice type
in the field called 'this_inquiry'.
 
but its not working 
can you please help !!
Version history
Last update:
‎10-09-2020 12:14 PM
Updated by: