Create a record in MRVS automatically

virajsapte99
Tera Contributor

Hi team,

We have a requirement wherein a record must be created by swapping the values in MRVS.

virajsapte99_0-1736599337403.png

Whenever Swap is selected, another record must be created in MRVS with values swapped. So, after clicking on Add, there must be 2 records as follows-
Record 1 - User1: Abel Tuter, User2: Abraham Lincoln
Record 2 - User1: Abraham Lincoln, User2: Abel Tuter

Kindly help.

Regards,
Viraj

3 REPLIES 3

AshishKM
Kilo Patron
Kilo Patron

Hi @virajsapte99 , 

 

Seems like you need some back end process to replicate the inserted record for specific MRVS , if swap checkbox checked. Need to consider add & edit  both operations. The data is not saved until we submit the catalog, so need to find the code/variables where data stay before the form submission.

 

Cant think of any solution at moment.

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

MackI
Kilo Sage

HI @virajsapte99 

 

Okay, I understand the requirement. You want to create two records in an MRVS (Multi-Row Variable Set) when the "Swap" checkbox is checked, effectively swapping the values of "User1" and "User2" in the second record.

Here's how you can achieve this in ServiceNow using a Catalog Client Script and potentially a Script Include (for cleaner code):

 

 

Catalog Client Script (onChange of the "Swap" variable)---Below is the script---

 

 

 

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

    // Assuming your MRVS variable name is 'my_mrvs' and 'swap' is the name of the swap checkbox variable.
    var mrvs = g_form.getValue('my_mrvs'); 
    var swapChecked = newValue === 'true';

    if (swapChecked) {
        // Get the current rows of the MRVS
        var mrvsRows = JSON.parse(mrvs);

        // If rows exist, proceed with swapping logic
        if (mrvsRows.length > 0) {
            g_form.hideFieldMsg('swap', true); // Hide any previous messages (optional)

            // Call a Script Include to handle the swap and add logic (recommended)
            var ga = new GlideAjax('SwapMRVSRows');
            ga.addParam('sysparm_name', 'addSwappedRow');
            ga.addParam('sysparm_mrvs_rows', JSON.stringify(mrvsRows));
            ga.getXML(handleSwapResponse);

        } else {
            // Show a message if there are no rows in MRVS
            g_form.showFieldMsg('swap', 'Please add at least one row to the MRVS before swapping.', 'error');
            g_form.setValue('swap', 'false'); // Uncheck the swap checkbox
        }

    }
}
//call back function to process the response from the script include
function handleSwapResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer) {
        var newRows = JSON.parse(answer);
        g_form.setValue('my_mrvs', JSON.stringify(newRows)); // Set the updated rows in the MRVS
        //if you are using on submit client script then call this function from there also, with the script include function to create records.
    }
}

 

 

 

2. Script Include:The addSwappedRow function receives the MRVS rows.[ You need to brainstorm and develope the script incldue , see the logic below ---]
It iterates through the rows, adding each original row to newRows.
If a row has both user1 and user2 values, it creates a new swappedRow object with the values swapped.
The swappedRow is also added to newRows.
Finally, it returns the updated newRows array as a JSON string.
The createRecords function receives the MRVS rows.
It iterates through the rows, and insert the values into the table.

3. Develope another onSubmit Client Script(Optional)

If you like this opinion and your problem is resolved after reviewing and applying it. Please kindly mark this your best answer‌🌠‌ OR  mark it  Helpful â€Œâ›‘‌ if you think that you get some insight from this content relevant to your problem and help me to contribute more to this community

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia

Ankur Bawiskar
Tera Patron
Tera Patron

@virajsapte99 

what if user does the swap multiple times? each time you will have to check MRVS doesn't have duplicate rows

Is this a business requirement?

This can be achieved but it's somewhat complex to handle.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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