How to populate values in MRVS automatically from List type variable on catalog item

rkreddy
Giga Expert

Hi All,

I Have a List type variable on Catalog form, referring to user table. And I have MRVS on the same form, with the variables like name, email, department, telephone. If I select any user in List Type Variable, I need to populate the user related information in MRVS, like his name, email, location, telephone. And If I remove user from List Variable, related row needs to be deleted from MRVS. The same should work on MRVS too, like if i remove row from MRVS, the related user should be removed from List variable.

find_real_file.png

Thanks in Advance

1 ACCEPTED SOLUTION

Hi,

I assume your onChange is written on the Attendee List field

Updated below

Script Include:

var populateEmailfromList = Class.create();
populateEmailfromList.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    listcollector:function() {    

        var listValuename = [];
        var userInfo = this.getParameter('sysparm_user_info'); //Passing the selected list collector asset value.
        var query = 'sys_idIN' + userInfo;
        if(userInfo)
        {            
            var gr = new GlideRecord('sys_user');
            gr.addEncodedQuery(query);
            gr.query();
            while (gr.next()){
                //Push selected list collector values into multi row variable set variables.
                // use the correct mrvs variable name here for attendee_name,email_address,department
                listValuename.push({
                    "attendee_name": gr.getValue('sys_id'),  
                    "email_address": gr.getValue('email'),
                    "department":gr.getDisplayValue('department')
                });
            }
        }
        return JSON.stringify(listValuename);
    },
    type: 'populateEmailfromList'

});

Client Script:

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

    var listValuename = g_form.getValue('attendees'); //Getting the list collector values.

    var ga = new GlideAjax('populateEmailfromList'); //Calling Script Include.
    ga.addParam('sysparm_name', 'listcollector'); // Function name of the script include.
    ga.addParam('sysparm_user_info', listValuename); // Passing selected list collector value as parameter.
    ga.getXML(listcolleValues);

    function listcolleValues(response){

        var val = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('variables.attendees_information', val); //Set multi row variable set with the selected list collector values.
    }
}

Regards
Ankur

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

View solution in original post

23 REPLIES 23

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you would require before update BR on sc_req_item table

Condition: current.cat_item.name == 'Your Catalog Item Name Here' && current.u_attendees.changes()

Script:

(function executeRule(current, previous /*null when async*/) {

    // Add your code here

var users = current.u_attendees.toString();

var arr = [];

var rec = new GlideRecord('sys_user');
rec.addQuery('sys_id', 'IN', users);
rec.query();
while(rec.next()){

var obj = {};

obj["attendee_name"] = rec.getValue('name'); // give the respective variable name inside MRVS here

obj["email_address"] = rec.getValue('email'); // give the respective variable name inside MRVS here

obj["department"] = rec.getValue('department'); // give the respective variable name inside MRVS here

arr.push(obj);

}

current.variables.mrvsVariableName = JSON.stringify(arr); // give the MRVS variable name here

})(current, previous);

Regards
Ankur

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

Hi Ankur,

Can We do it Onchange? I also have some other fields which depend on row count in MRVS, so I need to make it onChange.

Hi,

Yes you can try onChange but remember you will have to use GlideAjax to get the user details as json string

Regards
Ankur

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

Thanks Ankur. Could you please update the script? I can able to achieve it on change of List variable, like adding or removing the value in List, reflects same in the MRVS. But if I remove any value from MRVS, it is not removing from List. I need to make it work on both variables change.