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.

Auto populate a variable in multi row variable set based on another variable on record producer

D V Sandeep
Tera Contributor

Hi,

I have a variable called “Receipt Number” in a Single Row Variable Set, and another variable called “Transfer Receipt Number” in a Multi Row Variable Set.

I need to copy the value from the Receipt Number variable into the Transfer Receipt Number variable for each row in the Multi Row Variable Set.

Could someone please help me with how to do this?
Thank you!
@Brad Tilton 

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@D V Sandeep 

you can use onLoad catalog client script which Applies to MRVS and use this code

function onLoad() {
	// give correct variable names
    g_form.setValue('insideVariableName', g_service_catalog.parent.getValue("outSideVariableName"));
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Vishal_Jaiswal
Mega Guru

Hi @D V Sandeep ,

Follow the below approach.

1) Create an OnChange Client script outside of both variable sets below that is under record producer.

2) Applies to: A Catalog Item

3) Variable name: Select your main "Receipt Number" variable (e.g., receipt_number).

4) Use script below

function onChange(control, oldValue, newValue, isLoading) {

    var mrvsInternalName = 'transfer_receipt_mrvs';     // Internal name of your Multi-Row Variable Set
    var mrvsFieldName = 'transfer_receipt_number';      // Internal name of the field INSIDE the MRVS
    var mainReceiptNumber = newValue;                   // This is the new value from the field that changed

    if (isLoading) {
        return;
    }

    var mrvsValue = g_form.getValue(mrvsInternalName);
    if (!mrvsValue) {
        return;
    }

    try {
        var mrvsRows = JSON.parse(mrvsValue);
        for (var i = 0; i < mrvsRows.length; i++) {
            mrvsRows[i][mrvsFieldName] = mainReceiptNumber;
        }
        var newMrvsValue = JSON.stringify(mrvsRows);
        g_form.setValue(mrvsInternalName, newMrvsValue);

    } catch (e) {
        alert('Error copying receipt number to MRVS: ' + e.message);
    }
}

 

Regards,

Vishal

: