Access multi row variables outside variable set

Ruchi Kumari1
Tera Expert

Hi,
I have a ask where i need to display names of asset tagged to user, whenever any user is selected.

1. User field is of type list collector. So multiple users can be selected in one request.
2. On select of users I need to display the tagged asset. (Field type of this asset can be decided based on feasibility).

**Work around that i was trying is to use a MVRS (multi row variable set) and populate asset names whenever user is selected in list. In this variable set i have created 2 variables (User, Asset) of single line text field type. I am expected whenever user is selected in list collector, That user and associated asset is populated in this MRVS.

But the challenge is list type field is outside MRVS.
Is this feasible ? Or any other workaround to achieve this (I just want the user selected in list and associated asset is displayed on form)?

Thanks,


15 REPLIES 15

@Ruchi Kumari1 

the logic is already shared.

I believe you can debug it further and that will help you learn as well.

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

@Ruchi Kumari1 

Hope you are doing good.

Did my reply answer your question?

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

This approach could be preferable over a multi-line depending on which appearance is desired, and if you need to do anything with this data later in the workflow.  I would make the two MRVS variables reference types so that you're populating it with sys_ids that will appear nicely in the displayed MRVS, then if you have direct access to the actual user and asset records if needed later for workflow or reporting.

 

To maximize efficiency, you only need and want to make one server call, passing in all of the selected users.  The Script Include will build an array of objects with the sys_id of each user and asset, then return a stringified array of objects.  The beginning of the onChange script would look more like this:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
     var mrvsObj = {};
    var mrvsArr = []; 
    var gaAsset = new GlideAjax('TestUtil');
    gaAsset.addParam('sysparm_name', 'getEmail');
    gaAsset.addParam('sysparm_users', newValue);
    gaAsset.getXMLAnswer(parseAnswer);

    function parseAnswer(answer) {
        alert(answer); //expected value
        if (answer) {
            <<split and loop through the array parsing each member then pushing to the mrvs obj and array>>
        }
    }
    g_form.setValue('assets', JSON.stringify(mrvsArr));
}

I don't have time right now to work out the crucial split/loop/parse block, and this will depend on how the Script Include builds it, but you should be able to find an example of this to get you started in addition to the GlideAjax example showing an object return. 

Hi @Ankur Bawiskar @Brad Bowman  , This is the code that i am trying. Not sure why the rows are not added in the MRVS.

Client script :

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
   var usersArr = newValue.split(',');
   var arr = [];
   for (var i = 0; i < usersArr.length; i++) {
    var mrvs = {};
        var gaAsset = new GlideAjax('TestSI');
        gaAsset.addParam('sysparm_name', 'getMachineNames');
        gaAsset.addParam('sysparm_users', usersArr[i]);
        gaAsset.getXMLAnswer(parseAnswer);

        function parseAnswer(response) {
            var answer = JSON.parse(response);
            if (answer) {

                for (var a = 0; a < answer.length; a++) {
                    alert(answer[a].Machine) // getting expected sys id
                    mrvs.push({
                        "tagged_asset": answer[a].Machine,
                        "selected_user": answer[a].userId
                    });
                    arr.push(mrvs);
                }
            }
        }
    }
    g_form.setValue('machine_names', JSON.stringify(arr));
}

 

Script include:

getMachineNames: function() {
        var userSysId = this.getParameter('sysparm_users');
        var listValues = [];
        var grDetails = new GlideRecord('sys_user');
        grDetails.addEncodedQuery('sys_idIN' + userSysId);
        grDetails.query();
        while (grDetails.next()) {
            listValues.push({
                "Machine": grDetails.getValue('machine'),
                "userId": grDetails.getValue('user_name')
            });
        }
        return JSON.stringify(listValues);

@Ruchi Kumari1 

did you add logs and see what came in script include?

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