Auto populate MVRS variable on basis of a variable outside it

Nagashree5
Tera Contributor

Hi All,

 

I'm trying to auto-populate the "SAP Cost center" variable inside the mvrs based on the option selected in a reference field called "Company code" which is outside mvrs. In the company code variable, when code is selected, the location should autopulate in the 'sap_cost_center' field which is inside a MVRS(Multi Row).

I'm trying to set the value like below, but it isn't working.

Script Include:

sap:function()
{
var companycode=this.getParameter('sysparm_company');
var gr=new GlideRecord('core_company');
gr.addQuery('name',companycode);
gr.query();
if(gr.next())
{
var ans=gr.location;
return ans;
}
},

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('populateaddress');
ga.addParam('sysparm_name', 'sap');
var com = g_form.getValue('sap_purchase');
ga.addParam('sysparm_company', com);
ga.getXML(callBackFunction);
function callBackFunction(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('sap_cost_center_mr', answer);// this is the MVRS variable
}
}

When tried with alert, we are getting the location value but unable to set the value in to the MVRS variable. Can anyone please guide me on this.

 

Thank you in Advance.

 
6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

The MRVS is stored as a JSON array since there could be multiple rows, so you need to loop through the rows to update the variable on each row.  Something like this after your var answer line inside of the callback function should do it:

var mrvs = g_form.getValue('mrvs_internal_name');
var obj = JSON.parse(mrvs);
for (var i = 0; i < obj.length; i++) {
    obj[i].v_sap_cost_center_mr = answer;	
}
g_form.setValue('mrvs_internal_name', JSON.stringify(obj));

 

Hi @Brad Bowman  ,

 

Thanks for the response,

I have tried adding the above like below in client script.

var ga = new GlideAjax('populateaddress');
ga.addParam('sysparm_name', 'sap');
var com = g_form.getValue('sap_purchase');
ga.addParam('sysparm_company', com);
ga.getXML(callBackFunction);
function callBackFunction(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var mrvs = g_form.getValue('multirow_variable');
var obj = JSON.parse(mrvs);
for (var i = 0; i < obj.length; i++) {
obj[i].v_sap_cost_center_mr = answer;

}
g_form.setValue('multirow_variable', JSON.stringify(obj));

But, it is not setting the value in the MVRS.

Can you please have a look at this.

 

Thank you in Advance.

I found an example where I am doing this and I think what you will have to is either find and replace the empty value, or rebuild the MRVS with the contents plus your new value within the for loop.  Add an alert on mrvs after var mrvs... so that you can see what the populated MRVS string looks like in JSON format.  Empty variables will either not appear at all in the object (no name-value pair with the variable name sap_cost_center_mr, or you will see that name with "" after it indicating an empty value.  If it's there you can do a simple find and replace on the mrvs script variable for that exact string - the variable name : "" and replace it with variable name : answer.  If it's not there, you'll need the parse line and the for loop more like this to re-build each row with each variable name and value from the original MRVS contents, then adding the cost_center variable and value (answer) more like this:

var mrvs = g_form.getValue('multirow_variable');
alert('original MRVS=' + mrvs);
var obj = JSON.parse(mrvs);
var arr = [];
for (var i = 0; i < obj.length; i++) {
    arr.push({var1_name:  obj[i].var1_name, var2_name: obj[i].var2_name, sap_cost_center_mr: answer});	
}
alert('new MRVS=' + JSON.stringify(arr));
g_form.setValue('multirow_variable', JSON.stringify(arr));

   

Hi @Brad Bowman  ,

 

Thanks for the response,

I have tried adding the above code. In the first alert mvrs we are getting empty value and it is not setting the value in the MVRS.

Can you please have a look at this.

 

Thank you in Advance.