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.

On submit client script

Venky Kshatriy2
Tera Contributor

Hi team,

 

Shall I use for loop in on submit client script? I used for loop in in On submit client script for getting MRVS(Multi row variable set) values once I got the MRVS values that values this values I am sending to script include and return I am getting value one by one record value i need to compare to all values need to select the high value and to set  variable   and based on the this variable value i need to restrict the user.

I am tried But I didn't get what I need.

 

for setting values I am fallowed below article

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579

 

is I use this method na number of times form loaded no.of records(request) created.

 

suggestion me sum best way to do this kind of scenario 

 

 

1 REPLY 1

Community Alums
Not applicable

Hi @Venky Kshatriy2 ,

Yes, you can use a for loop in an onSubmit client script to get values from a Multi Row Variable Set (MRVS) and send them to a script include for further processing.

 

Updated Client Script

function onSubmit() {
    // Prevent form from submitting
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();
    var ga = new GlideAjax('YourScriptIncludeName');
    var mrvsValues = []; // Array to hold MRVS values
    // Loop MRVS rows and collect values
    var mrvs = g_form.getValue('multi_row_variable_set_name');
    var mrvsLength = mrvs.length;
    for (var i = 0; i < mrvsLength; i++) {
        var rowSysId = mrvs[i].sys_id;
        // Collect your desired MRVS values here, e.g., mrvs[i].variable_name
        mrvsValues.push(mrvs[i].variable_name);
    }
    ga.addParam('sysparm_name', 'yourFunctionName');
    ga.addParam('sysparm_mrvsValues', JSON.stringify(mrvsValues));

    ga.getXMLAnswer(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var highestValue = JSON.parse(answer);// script include returns a JSON object
        g_form.setValue('your_variable_name', highestValue);
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    });

    return false;// prevent default form submission
}

 

Script Include should be like this-

var YourScriptIncludeName = Class.create();
YourScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    yourFunctionName: function() {
        var mrvsValues = JSON.parse(this.getParameter('sysparm_mrvsValues'));
        var highestValue = this._getHighestValue(mrvsValues);
        return JSON.stringify(highestValue); // Return the highest value as a JSON string
    },
    
    _getHighestValue: function(values) {
        var highest = values[0];
        for (var i = 1; i < values.length; i++) {
            if (values[i] > highest) {
                highest = values[i];
            }
        }
        return highest;
    }
});

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!


Thanks & Regards,

Sanjay Kumar