MRVs to Another MRVS

Ryan Keeler1
Tera Contributor

Hello, I've been tasked with mapping values from one MRSV to another within the same form. MRVS 2 holds some of the same values as MRVS 1. 

 

The below code has been suggested and modified but it is not mapping the values from MRVS 1 to the same value in MRVS 2.

 

below is the code: 

 

function postValuesToAnotherMRVS() {
        // Get the source MRVS
        var sourceMRVS = g_form.getControl('mrvs_1');
        var sourceRows = sourceMRVS.getValues();

        // Get the target MRVS
        var targetMRVS = g_form.getControl('mrvs_2');

        // Clear any existing rows in the target MRVS
        targetMRVS.clearRows();

        // Iterate through the source rows and add them to the target MRVS
        for (var i = 0; i < sourceRows.length; i++) {
            var row = sourceRows[i];
            var newRow = targetMRVS.addRow();

            // Copy values from source row to target row
            for (var new_fieldin row) {
                newRow[new_field] = row[new_field];
            }
        }
    }
}
 
Has anyone encountered this before? 
4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Hi Ryan,

Do you need / want to see the second MRVS populate on the form, or can this be done after the request/RITM/Catalog Task is submitted or updated?  If it needs to be on the form before a save/reload, what is the trigger - on load, or when something changes?  Does the second MRVS contain variables with the same names and / or types as the first MRVS? 

This would need to be done as soon as the first MRVS is populated. The trigger would be an onChange. And yes the Second MRVS contains some of the same values as the first

The Catalog Item has access to a variable containing the contents of the MRVS that is set/updated when a row is added or edited, but it's tricky to trigger when this changes.  There are some posts on the topic you can explore, but in this case you could just trigger this with an onSubmit Catalog Client Script that applies to the (first) MRVS.  This script would add data from the row that is about to be added in the first MRVS to the second MRVS.

 

In this example I have a MRVS with the internal name employees_mrvs that I am adding rows to on a request form.  It has a reference variable to the sys_user table named v_employee_mrvs and a Select Box named v_choice_mrvs.  When a row is added to this MRVS, I will add a row to a MRVS named managers_mrvs, populating a variable in this MRVS named v_manager_mrvs that is also a reference to the sys_user table.  This MRVS does not have a variable for the select box, and does have a single line text variable that will remain unpopulated, or you can populate it with something in the script.

function onSubmit() {
	var mrvsArr = [];
	var mrvs2 = g_service_catalog.parent.getValue('managers_mrvs');
	if (mrvs2.length>0) {
		mrvsArr = JSON.parse(mrvs2);	
	}
	mrvsArr.push({
		"v_manager_mrvs" : g_form.getValue('v_employee_mrvs') //add a comma then a row for each variable to populate
	});
		
	if (this) { //Service Portal method
		this.cat_g_form.setValue('managers_mrvs', JSON.stringify(mrvsArr));
	} else { //native UI method
		parent.g_form.setValue('managers_mrvs', JSON.stringify(mrvsArr));
	}
}

If you are using Service Portal you will also need this onLoad Catalog Client Script that applies to the Catalog Item, not the MRVS

function onLoad() {
	if (this) {//we only need to do this for Service Portal
		//We need to make the g_form object for the parent item available from the MRVS window
		this.cat_g_form = g_form;
	}
}

Once you get this working consider what will happen when a row in the first MRVS is being edited - as it is right now, a new row will be added with the new information, but if there's a variable in the first MRVS that can't or won't change once added, or depending if you are using the native UI vs Service Portal we may be able to detect an edit vs add...

 

x