Set Value on MVRS from the catalog form variable

Kaustubh k
Tera Contributor

Hello All,

 

Thanks for looking into the ask,

 

We have a catalog variable : Applications(list collector)

 

We have a mvrs: which has Applications(reference) field.

 

We need to create and set, each row for the applications selected on the Catalog form variable Application(list collector) on to the MVRS.

 

 

Any feedback on the same will be highly appreciable.

Thanks

 

12 REPLIES 12

Hello @Kaustubh k ,

 

This solution will be a bit elaborate, please modify it as per your requirements. 

 

I have a list collector and a MRVS with 2 values(a reference field and a free text field).

list collector and reference field pointing to sys_user

 

1. Script Include (Client callable)

 

 

 

var MRVSUtils = Class.create();
MRVSUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserInfo: function() {
        var sys_id = this.getParameter("sysparm_id");  //sys_id of the value selected in list collector
        var result = [];                                              //initialize an empty array to store the return values

        var gr = new GlideRecord("sys_user");       //replace with your table name
        gr.addQuery("sys_id", sys_id);
        gr.query();
        if (gr.next()) {
            result.push({
                "user_name": gr.getValue("user_name"),      //used as third parameter in g_form.setvalue()
                "email": gr.getValue("email")                          //Retrieve whatever additional info needed
            });
            //retrieve the values you need in the above format in my case I am retrieving user_name (which is very important because this will be used as a third parameter in g_form.setValue()) and email
        }

        return JSON.stringify(result);

    },
    type: 'MRVSUtils'
});

 

 

 

2. Catalog Client Script

Write this on the Variable set(MRVS)

 

Applies to: Variable set

Active: true

UI type: All

Isolate script: Unchecked

Type: onLoad

 

Script:

 

 

function onLoad() {
    //Type appropriate comment here, and begin script below

    var mrvsValues = g_service_catalog.parent.getValue("mrvs"); //gets the mrvs values (stringified object)
    var arrMrvsValues = [];
    var mrvsSysIds = [];
    var value = g_service_catalog.parent.getValue("friends");
    var arrValue = value.split(",");
    var valueToBeAdded = arrValue[0];


    if (mrvsValues) {
        arrMrvsValues = JSON.parse(mrvsValues); //needs to be done here only
        // gets the mrvs sys ids
        for (var i = 0; i < arrMrvsValues.length; i++) {
            mrvsSysIds.push(arrMrvsValues[i]["selected_friends"]); //gets the mrvs sys ids
        }
        //checks to see if value to be added is not already added
        for (var j = 0; j < arrValue.length; j++) {
            if (mrvsSysIds.indexOf(arrValue[j]) == -1) {
                valueToBeAdded = arrValue[j];
                break;
            }
        }
        var ga1 = new GlideAjax("MRVSUtils");
        ga1.addParam("sysparm_name", "getUserInfo");
        ga1.addParam("sysparm_id", valueToBeAdded);
        ga1.getXMLAnswer(callback);
    } else {
        var ga2 = new GlideAjax("MRVSUtils");
        ga2.addParam("sysparm_name", "getUserInfo");
        ga2.addParam("sysparm_id", valueToBeAdded);
        ga2.getXMLAnswer(callback);
    }


    function callback(response) {
        var res = JSON.parse(response);
        g_form.setValue("selected_friends", valueToBeAdded, res[0]["user_name"]);
        g_form.setValue("email", res[0]["email"]);
    }
}

 

 

 

also consider setting this sys_property to false - glide.script.block.client.globals (create if not already available)

 

Regards,
Bhavani Shankar
Linked In

Hello @Bhavani Shankar , thanks for the reply, we are able to map the list collector single value to one row, but multiple applications selected on MVRS is not adding extra row in MVRS.

 

The first row is only getting overridden.

 

if you could please check and help on the same.

 

Thanks

 

Were you able to check the Demo video I attached, that is the expected behavior with the script I shared. 

Regards,
Bhavani Shankar
Linked In

Hello @Bhavani Shankar ,thanks for asking,the logic tried multiple approaches but did not add any extra row in mrvs.

 

The iterations does not work from list collector to mrvs

 

Like list collector has: abc and xyz

 

In mrvs we are able to just get 

Abc

Xyz is not getting added to new row the first row is getting overridden.

 

Thanks

Hey @Kaustubh k ,

 

Let say you have selected 3 values in list collector in that case you will have to click on the add button(for MRVS) 3 times to add three rows (there is a validation step in code that checks if the values is already added in MRVS). Don't try to automate adding 3 rows at once in code.

 

If its not working still, please share code / exact steps to reproduce your scenario / any video samples of what is happening.

 

If possible lets connect one on one to sort his one.

Regards,
Bhavani Shankar
Linked In