MRVS How to iterate and check each row if a variable value is not empty

Ljone
Giga Contributor

Hi,

I have an Onsubmit client script that should check if for each rows, 1 variable: 'member_replacement' is not empty and all rows are filled in. How to achieve this? 

We are auto-populating the addtion of row  and users need to update each row variable:member_replacement

below is my current script but not working .  Thank you.

 

function onSubmit() {
//Type appropriate comment here, and begin script below

var multiRowVariableSet = JSON.parse(g_form.getValue('group_creation_members')); //internal name mrvs
var numberOfRows = multiRowVariableSet.length;

for (var i = 0; i < multiRowVariableSet.length; i++) {
alert(numberOfRows);

if(multiRowVariableSet[i].member_replacement == ""){                //variable name
alert("With Value "+ multiRowVariableSet.member_replacement + " " + multiRowVariableSet.group_member_name);
}
else{
alert("Without Value "+ multiRowVariableSet.member_replacement + " " + multiRowVariableSet.group_member_name);

}
}

 }

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Ljone 

So you want to check if both the variables are non-empty

if any row has both the variables as empty then stop form submission

update as below

function onSubmit() {
//Type appropriate comment here, and begin script below


var mrv = JSON.parse(g_form.getValue('group_creation_members')); // mrvs internal name
if (mrv.length >= 1){
alert(mrv);

var memberArrCount = 0;
var replacingArrCount = 0;

for (var i=0; i<mrv.length; i++){

if(mrv[i].member_replacement != '')
memberArrCount++;

if(mrv[i].group_member_who_are_you_replacing != '')
replacingArrCount++;

}

if(mrv.length != memberArrCount && mrv.length != replacingArrCount){
alert('empty column');
return false;
}
}

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Brian Lancaster
Tera Sage

Just wondering my you would not just make the variables in the mrvs mandatory instead?  That way you don't need an onsubmit client script.

Hi Brian, the value of the MRVS is dependent to another field and is auto-populated based on that, users are required to update 2 variables from that row. We have ui policy that enforces the mandatory when user clicks the EDIT form, but we have no way of checking if user did not click the EDIT icon. can you help please?

Narsing1
Mega Sage

Hi,

Try like this.

var mrv = JSON.parse(g_form.getValue('group_creation_members')); // mrvs internal name

for(var row=0; row < mrv.length; row++){

if(mrv[row]["<sysid of member_replacement>"] == "" && mrv[row]["<sysid ofgroup_member_who_are_you_replacing>"] == ""){

alert('empty column');

}

}

 

Please mark it as correct answer if it helps.

Ljone
Giga Contributor

Hi Narsing, the sys_id doesn't seem to work. i updated my script to below but no luck, the if/else statement is not working properly. can you help please?

 

var multiRowVariableSet = JSON.parse(g_form.getValue('group_creation_members'));
var numberOfRows = multiRowVariableSet.length;

for (var i = 0; i < multiRowVariableSet.length; i++) {
alert(numberOfRows);

if(multiRowVariableSet[i].member_replacement == ""){
alert("With Value "+ multiRowVariableSet.member_replacement + " " + multiRowVariableSet.group_member_name);
}
else{
alert("Without Value "+ multiRowVariableSet.member_replacement + " " + multiRowVariableSet.group_member_name);

}
}