Make variable mandatory on MVRS onsubmit

Ash41
Kilo Sage

Hi Team, 

 

I was trying below code to restriction submission of request before filling in mandatory variable not working, please suggest

function onSubmit() { 
    var values = g_form.getValue('service_details'); // give here mrvs variable name
    var parser = JSON.parse(values);
    for (var i = 0; i < parser.length; i++) {
 
        var service = parser[i].service_question; // give here the variable name for proportion
		var serviceAnswer =parser[i].service_question_answer;
		alert(serviceAnswer);
		alert(service);
        if (service != "" && serviceAnswer =='') {
 
            g_form.addErrorMessage('please fill Service Answer');
 
            return false;
 
        }
}
}

 

8 REPLIES 8

Please use below code:

var mvrsvalues= JSON.parse('service_details');
g_form.addInfoMessage(JSON.stringify(mvrsvalues)); // to print the JSON

//Below code is to access the JSON elements
 for(var i=0; i<mvrsvalues.length;i++)
{
 var service = mvrsvalues[i].service_question; // give here the variable name for proportion
		var serviceAnswer =mvrsvalues[i].service_question_answer;
		alert(serviceAnswer);
		alert(service);
        if (service != "" && serviceAnswer =='') {
            g_form.alert('please fill Service Answer');
            return false;
        }
}
If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Hi Nayan, 

still retuning undefined for 

alert(serviceAnswer);

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Ash41 

why not make those 2 variables as mandatory when MRVS form loads?

you can use catalog UI policy or catalog client script which applies on variable set and select the MRVS

 

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

Hi Ankur, 

 

Question variable will not be mandatory always, sometime there won't be any question for that service. 

Applied UI policy on variable set but not working. Still request get submitted.