Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

MRVS onsubmit client script

levino
Giga Guru

Hi Team

I have a mrvs , with the below client script which works fine.

e.g it sets the below to the mrvs_description variable.

Item #1:
Dealer_Code : df
Dealer_Name : dfdf
Effective_Date : 09/08/2024
Dealer_Address : ddf
Dealer_Type : Consumer
Hub_Dealer_Code : false
SRS_Dealer_Code : true
Store_Manager : dff

 

I have another requirement where if    Dealer Type == Consumer then setvalue on another variable.

if(dealer_type == 'Consumer'){g_form.setvalue('request_template','test')

}

not sure if the above will work, please help with a onsubmit client script

 

note dealer type is a select box variable type

 

 

function onSubmit() {
	var output = "";
	var variables_names = ['Dealer_Code','Dealer_Name','Effective_Date','Dealer_Address','Dealer_Type','Hub_Dealer_Code','SRS_Dealer_Code','Store_Manager'];
	//get values from the Multirow variables
	var vset_value = g_form.getValue("IO:2db810c7db6d90d0304c147a3a9619c1");
	var json_obj = JSON.parse(vset_value);
	for(var i = 0;  i < json_obj.length; i++){
		output+= "\n"+ 'Item #' + (i+1) + ":\n";
		var row = json_obj[i];
		for(var j = 0; j < variables_names.length; j++){
			if(row[variables_names[j]] != ""){
				output+= variables_names[j] + ' : ' + row[variables_names[j]] + "\n";
			}
		}
	}
	g_form.setValue('mrvs_description', output);
}

 

 

1 ACCEPTED SOLUTION

Hi @levino 

print the log from if condition and check condition matching or not also use actual field name instead of 'request_template' 

View solution in original post

10 REPLIES 10

Anubhav24
Mega Sage

Hi @levino ,

Can you try the below script , I have parsed the JSON and set the value accordingly:

var mrvs_value = g_form.getValue('<name of the MRVS>');
var jsonobj = JSON.parse(mrvs_value);
var length = jsonobj.length;
for (var i = 0; i < length; i++) {

if (jsonobj[i].<MRVS Variable name> == '<Value>') {
g_form.setValue(jsonobj[i].<MRVS Variable name>,'<Value to be set>');
}
}

Please mark helpful/correct if my response helped you.

Hi Anubhav

mrvs variable name  in my case would be  'dealer_type'  and value would be  'consumer'

f (jsonobj[i].<MRVS Variable name> == '<Value>') {
g_form.setValue(jsonobj[i].<MRVS Variable name>,'<Value to be set>');
}
}

 

g_form.setValue  mrvs variable name  here your referring to   'request_template',  value can  be anything 'test'

 

Thanks

Levino

 

Rajesh Chopade1
Mega Sage

hi @levino 

To accomplish what you're aiming to do—setting a value based on a select box (dealer_type) and handling multi-row variables in an onsubmit client script—you need to ensure that your script correctly handles both scenarios. Below are improvements and corrections for your script.

 

function onSubmit() {
    // Get the value of the dealer_type field
    var dealerType = g_form.getValue('dealer_type'); // Use the actual field name here

    // Set the request_template field based on the dealer_type value
    if (dealerType == 'Consumer') {
        g_form.setValue('request_template', 'test'); // Use the actual field name here
    }

    // Initialize output variable for multi-row variables
    var output = "";
    var variables_names = ['Dealer_Code','Dealer_Name','Effective_Date','Dealer_Address','Dealer_Type','Hub_Dealer_Code','SRS_Dealer_Code','Store_Manager'];

    // Get values from the Multi-row variable
    var vset_value = g_form.getValue("IO:2db810c7db6d90d0304c147a3a9619c1"); // Use the actual variable name here
    if (vset_value) {
        var json_obj = JSON.parse(vset_value);
        for (var i = 0; i < json_obj.length; i++) {
            output += "\n" + 'Item #' + (i + 1) + ":\n";
            var row = json_obj[i];
            for (var j = 0; j < variables_names.length; j++) {
                if (row[variables_names[j]]) {
                    output += variables_names[j] + ' : ' + row[variables_names[j]] + "\n";
                }
            }
        }
    }
    
    // Set the output to the 'mrvs_description' field
    g_form.setValue('mrvs_description', output);

    // Return true to allow form submission
    return true;
}

 

 

I hope my answer helps you to resolve your issue, if yes please mark my answer as correct & helpful.

THANK YOU

rajesh chopade

Did you try this AI generated answer yourself? 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark