How to add rows to a multi row variable set dynamically

mballinger
Mega Guru

Hello,

I have a requirement to add rows to a multirow variable set based off of a selected value from another variable. The dropdown will be numbered 1 through 5. If 4 is selected, 4 single line text rows get inserted. The same applied to all the other selections.

Thanks

 

1 ACCEPTED SOLUTION

Let's take it down a level; this this: 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var mvrs = [];

    for (var i = 0; i < newValue; i++) {
        var tempObj = {};
        tempObj.test_report_number = '';
        mvrs.push(tempObj);
    }
    g_form.setValue('multi_row_variable_set_test', JSON.stringify(mvrs));

}

find_real_file.png

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

11 REPLIES 11

@Michael Jones - CloudPires - I tried to follow your example but it doesn't seem to be working for me.

I have 2 fields on my Record Producer:

  • 1 variable: Select Box with 1 - 5
  • MRVS: "Multi Row Variable Set Test" with 1 variable "Test Report Number"

When a dropdown selection is made, rows should be added based on the selected number from the dropdown.

Sorry for the confusion

What is the Internal Name of your MVRS and the name of your variable (not the labels) for example, are they multi_row_variable_set_test and test_report_number?

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

MRVS Internal name: multi_row_variable_set_test

Variable Name from MRVS: test_report_number

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    alert(g_form.getValue('multi_row_variable_set_test'));
    // output is [{"singlelinetext":"1"},{"singlelinetext":"2"},{"singlelinetext":"3"},{"singlelinetext":"4"}]
    var mvrs = JSON.parse(g_form.getValue('multi_row_variable_set_test'));

    for (var i = 0; i <= 4; i++) {
        var tempObj = {};
        tempObj.test_report_number = i + 4;
        mvrs.push(tempObj);
    }
    g_form.setValue('multi_row_variable_set_test', JSON.stringify(mvrs));

    //new value is [{"singlelinetext":"1"},{"singlelinetext":"2"},{"singlelinetext":"3"},{"singlelinetext":"4"},{"singlelinetext":4},{"singlelinetext":5},{"singlelinetext":6},{"singlelinetext":7},{"singlelinetext":8}]

}

Michael Jones -
Giga Sage

Try this, for example. Based on what you've shared, this onchange should add X blank rows to your MVRS based on what is selected in your select box, adding more each time you change the box and preserving whatever data might already be present. 

If you want it to clear everything out and only add the empty lines based on the select box, just remove the if statement and set mvrs = [];

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var mvrs_val = g_form.getValue('multi_row_variable_set_test');
    if (mvrs_val == '[]') {
        var mvrs = [];
    } else {
        var mvrs = JSON.parse(mvrs_val);

    }

    for (var i = 0; i < newValue; i++) {
        var tempObj = {};
        tempObj.test_report_number = '';
        mvrs.push(tempObj);
    }
    g_form.setValue('multi_row_variable_set_test', JSON.stringify(mvrs));


}

 

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

I tried to run what you provided me, but am getting a JavasScript error

find_real_file.png