Watchlist an user/approver from MRVS in the catalog item

Zaman Sm
Tera Expert

Hello all,

I am trying to add an user/approver from an MRVS to the Catalog item Watchlist user  field. The approver field gets auto populated from the user field. Should I create a Client script in the MRVS or Catalog Item?

 

I tried both, but did not work. Could please suggest me something?

ZamanSm_0-1729121135066.png

 

I used the following script to auto populate a field's user to the watchlist, but unable to achieve the same for MRVS field.

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

    // Get the current watch list and split it into an array
    var currentWatchList = g_form.getValue("watch_list").split(',').map(function(item) {
        return item.trim();  // Trim whitespace from each item
    });

    // Get the user ID from the new approver field
    var userId = g_form.getValue('current_approver_2'); // This field is from the catalog item, not MRVS

    // Perform the GlideAjax call to retrieve user details
    var ga = new GlideAjax("MYUserUtil"); //predefined class
    ga.addParam("sysparm_name", "getUserDetails");
    ga.addParam("sysparm_user_id", userId);
    ga.getXMLAnswer(callBack.bind(null, currentWatchList)); // Pass current watch list to callback
}

/*_________________________________________________________________
   * Description: Call back function to set the Contractor Details
   * Parameters: ajax function call result
   * Returns: none
   ________________________________________________________________*/
function callBack(currentWatchList, answer) {
    var answerObj = JSON.parse(answer);
    var newApprover = answerObj.name; // Get the new approver's name

    // Check if the new approver is not already in the watch list
    if (newApprover && currentWatchList.indexOf(newApprover) === -1) {
        currentWatchList.push(newApprover); // Add the new approver if not already present
    }

    // Update the watch list field with the new list
    g_form.setValue('watch_list', currentWatchList.join(','));
}
1 ACCEPTED SOLUTION

Hello Bowman,

Appreciate your response on that. Could you specify if I have to create the OnSubmit script inside the MRVS or as a catalog client script? I tried to modify my script using the advised "parent.g_form.getValue/.setValue to get/set". However, I am unable to achieve the goal. Could you please provide your feedback on the following script?

Thanks in advance.

function onSubmit() {
    // Retrieve the MRVS data
    var mrvsData = g_form.getValue("users_name_mrvs");
    if (!mrvsData) {
        return;
    }

    var currentWatchList = parent.g_form.getValue("watch_list").split(',').map(function(item) {
        return item.trim();  
    });

    // Parse MRVS data
    var mrvsArray;
    try {
        mrvsArray = JSON.parse(mrvsData);
    } catch (e) {
        console.error('Error parsing MRVS data:', e);
        return; // Exit if parsing fails
    }

    // Iterate through the MRVS entries
    for (var i = 0; i < mrvsArray.length; i++) {
        var approver = mrvsArray[i].current_approver;
        if (approver && currentWatchList.indexOf(approver) === -1) {
            currentWatchList.push(approver); // Add to watchlist if not already present
        }
    }

    // Update the watchlist field
    parent.g_form.setValue("watch_list", currentWatchList.join(','));

    // Return true to allow the form submission
    return true;
}
Update: ______________________________________________________________________
I am able to achieve this with the following code, using as a OnSubmit catalog client script,
function onSubmit() {
    // Get the MRVS (multi-row variable set) data from 'users_name_mrvs'
    var vs1 = g_form.getValue('users_name_mrvs') || '[]';  // Default to '[]' if no data is present
    var multiRowVariableSet;
   
    // Parse the MRVS data to an array
    try {
        multiRowVariableSet = JSON.parse(vs1);
    } catch (e) {
       // console.error('Error parsing MRVS data:', e);
        return true;  
    }

    // Get the current watchlist and split it into an array
    var currentWatchList = g_form.getValue('watch_list').split(',').map(function(item) {
        return item.trim();
    });

    // Loop through each row in the MRVS to add all approvers
    for (var i = 0; i < multiRowVariableSet.length; i++) {
        // Get the approver name from each row
        var approvername = multiRowVariableSet[i].current_approver;


        // If the approver is not already in the watchlist, add it
        if (approvername && currentWatchList.indexOf(approvername) === -1) {
            currentWatchList.push(approvername);  // Add to watchlist
        }
    }

        // Update the watchlist field with the new list of approvers
        g_form.setValue('watch_list', currentWatchList.join(','));

       
        return true;
}

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

It would probably be easier to do this on submit of the MRVS. You can use g_form.getValue for the MRVS variable name and parent.g_form.getValue/.setValue to get/set the watchlist field

Hello Bowman,

Appreciate your response on that. Could you specify if I have to create the OnSubmit script inside the MRVS or as a catalog client script? I tried to modify my script using the advised "parent.g_form.getValue/.setValue to get/set". However, I am unable to achieve the goal. Could you please provide your feedback on the following script?

Thanks in advance.

function onSubmit() {
    // Retrieve the MRVS data
    var mrvsData = g_form.getValue("users_name_mrvs");
    if (!mrvsData) {
        return;
    }

    var currentWatchList = parent.g_form.getValue("watch_list").split(',').map(function(item) {
        return item.trim();  
    });

    // Parse MRVS data
    var mrvsArray;
    try {
        mrvsArray = JSON.parse(mrvsData);
    } catch (e) {
        console.error('Error parsing MRVS data:', e);
        return; // Exit if parsing fails
    }

    // Iterate through the MRVS entries
    for (var i = 0; i < mrvsArray.length; i++) {
        var approver = mrvsArray[i].current_approver;
        if (approver && currentWatchList.indexOf(approver) === -1) {
            currentWatchList.push(approver); // Add to watchlist if not already present
        }
    }

    // Update the watchlist field
    parent.g_form.setValue("watch_list", currentWatchList.join(','));

    // Return true to allow the form submission
    return true;
}
Update: ______________________________________________________________________
I am able to achieve this with the following code, using as a OnSubmit catalog client script,
function onSubmit() {
    // Get the MRVS (multi-row variable set) data from 'users_name_mrvs'
    var vs1 = g_form.getValue('users_name_mrvs') || '[]';  // Default to '[]' if no data is present
    var multiRowVariableSet;
   
    // Parse the MRVS data to an array
    try {
        multiRowVariableSet = JSON.parse(vs1);
    } catch (e) {
       // console.error('Error parsing MRVS data:', e);
        return true;  
    }

    // Get the current watchlist and split it into an array
    var currentWatchList = g_form.getValue('watch_list').split(',').map(function(item) {
        return item.trim();
    });

    // Loop through each row in the MRVS to add all approvers
    for (var i = 0; i < multiRowVariableSet.length; i++) {
        // Get the approver name from each row
        var approvername = multiRowVariableSet[i].current_approver;


        // If the approver is not already in the watchlist, add it
        if (approvername && currentWatchList.indexOf(approvername) === -1) {
            currentWatchList.push(approvername);  // Add to watchlist
        }
    }

        // Update the watchlist field with the new list of approvers
        g_form.setValue('watch_list', currentWatchList.join(','));

       
        return true;
}