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 

update as this

Client Script:

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

    var users = g_form.getValue('listCollectorVariableName').toString();
    var gaAsset = new GlideAjax('TestSI');
    gaAsset.addParam('sysparm_name', 'getMachineNames');
    gaAsset.addParam('sysparm_userid', users);
    gaAsset.getXMLAnswer(parseAnswer);

    function parseAnswer(response) {
        g_form.setValue('assets', JSON.stringify(answer));
    }
}

Script Include function:

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);
},

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Ruchi Kumari1 

don't use MRVS here.

Let them select multiple users and have a multi line text variable

Simply use onChange catalog client script and populate the variable in this format

User A - Asset 1

User B - Asset 2

sample working script here; enhance for your case

Populate Email Addresses of users selected in List Collector variable to Multi Line Text Variable

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

Hi @Ankur Bawiskar , 
The issue with multi line field is that we will not be able to differentiate which asset belong to which user as the field will display all the machine names together separated by ,.

@Ruchi Kumari1 

Nope, it will look like this and it will be easy to differentiate

Abel Tuter - Dell Computer, Webcam

Beth Angelin - Asus Laptop, Printer 

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

Chaitanya ILCR
Kilo Patron

Hi @Ruchi Kumari1 

update your client script like this

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

    var gaAsset = new GlideAjax('TestSI');
    gaAsset.addParam('sysparm_name', 'getMachineNames');
    gaAsset.addParam('sysparm_users', newValue);
    gaAsset.getXMLAnswer(parseAnswer);

    function parseAnswer(response) {
        var answer = JSON.parse(response);
        if (answer) {
			alert(answer) //comment this added for testing
            g_form.setValue('machine_names', answer);
        }
    }

}

 

the issue is in the script include

you are querying the sys_user table

you are suppose to query the assets related table right?

 

replace the sys_user table with asset table 

in which table the assets are in?

is machine and user_name are proper field names in that table?

 

please share the asset table name screenshot of that table would be great to include filed names and backend names of machine and user name fields too

getMachineNames: function() {
        var userSysId = this.getParameter('sysparm_users');
        var listValues = [];
        var grDetails = new GlideRecord('sys_user');//query correct table 
        grDetails.addEncodedQuery('sys_idIN' + userSysId);//put proper filter
        grDetails.query();
        while (grDetails.next()) {
            listValues.push({
                "tagged_asset": grDetails.getValue('machine'), //get proper field names
                "selected_user": grDetails.getValue('user_name')
            });
        }
        return JSON.stringify(listValues);
}

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya