How to add rows dynamically in Multi Row Variable set (MRVS) based on input value of Catalog item
Hello,
In this article, i will show you how you can add the rows dynamically in MRVS based on the input value of a catalog item.
For this, i have created 1 Record producer, in which there is one field "No. of Companies worked". And i have 1 MultiRow variable set "Employment Details", which user has to fill the details of employer.
So once user enter the value in the "No. of Companies worked" field, we auto populate the Multi row variable set (MRVS) with that many no. of rows.
For this, we will require 1 catalog client script OnChange on the field (No. of Companies worked).
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(isNaN(newValue)) {
g_form.showFieldMsg('no_of_companies','Please enter only numeric values','error',true);
}
//populate the multi row variable set based on the experience.
var mrvs_data = {};
var aDetails = [];
mrvs_data = fillMRVS(newValue);
var oDetails = mrvs_data;
for(var sKey in oDetails){
if (sKey == "employment_details") {
g_form.setValue(sKey, JSON.stringify(oDetails[sKey])); //This will set the multirow once the key is matched.
}
}
}
function fillMRVS(num) {
var oDet = {};
var aDet = [];
var total_rows=0;
for(var i=1;i<=num;i++){
oDet = {"company": "",
"position": "",
"start_date": "",
"end_date": ""
};
aDet.push(oDet);
}
return {"employment_details": aDet};
}
If you want to populate the variable set with the data that is already filled earlier, check my other article.
Mark the article as helpful and bookmark if you found it useful.
https://www.servicenow.com/community/developer-articles/how-to-add-rows-dynamically-in-multi-row-variable-set-mrvs-based/ta-p/2296844