How to populate Requestor Details in variables of MRVS?

rkreddy
Giga Expert

Hi All,

Hope everyone is doing good.

I am working on a catalog form with Variables,

Name (reference to User table), Department, Work Location, Telephone, Email. When value changes in Name variable, all the other variables get populated automatically if the user in Name variable has that values exist. If not, we are allowing users to manually enter values in Department, work location, telephone, email variables.

We have a MRVS with the same variables as mentioned above.

Now, We have a checkbox variable on the same form with name "Self". When user checks it to true, all the values in the Name, Department, work location, telephone, email should be copied to variables in MRVS. If he unchecks, the values should get cleared (only the row should get deleted which is populated when the variable is checked). This should work on change of both Name and Self variables.

Thank you.

1 ACCEPTED SOLUTION

@rkreddy 

Done and working fine in native

please check below output

find_real_file.png

Please mark my response as correct and close the question.

Regards
Ankur

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

View solution in original post

58 REPLIES 58

Dash2
ServiceNow Employee
ServiceNow Employee

Hi @Ankur Bawiskar 

 

I tried replicating your scripts to populate the asset variables to no avail, I'd appreciate a review of the scripts below;

Client Script

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

    if (newValue == '') {
        g_form.clearValue('assetlist'); // give name of MRVS variable set here
    }

    if (oldValue != newValue) {
        var ga = new GlideAjax('populateAssetfromList');
        ga.addParam('sysparm_name', 'listcollector');
        ga.addParam('sysparm_user_info', g_form.getValue('requestor')); // give here the requestor variable name
        ga.getXML(listcolleValues);

        function listcolleValues(response) {
            var val = response.responseXML.documentElement.getAttribute("answer");
            g_form.setValue('assetlist', val); // give name of MRVS variable set here
        }
    }
}

 

Script include

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

    listcollector: function() {

        var listValuename = [];
        var userInfo = this.getParameter('sysparm_user_info');
        var query = 'sys_idIN' + userInfo;
        if (userInfo) {
            var gr = new GlideRecord('alm_asset');
            gr.addEncodedQuery(gs.getMessage('assigned_to={0}', userInfo));
            gr.query();
            if (gr.next()) {

                listValuename.push({
                    "asset_tag": gr.getValue('asset_assettag'),
                    "model": gr.getValue('asset_model'),
                    "model_category": gr.getValue('asset_category'),
                    "serial_number": gr.getValue('asset_serial'),
                    "display_name": gr.getValue('asset_name')
                });
            }
        }
        gs.info('ARB JSON' + JSON.stringify(listValuename));
        return JSON.stringify(listValuename);
    },
    type: 'populateAssetfromList'

});

Variables

find_real_file.png

rkreddy
Giga Expert

Hi @Ankur Bawiskar ,

Sure I will do like you said. Could you please tell me where should be fine to add alerts?

Thank you.

Hi,

in for loop

try updating as this

if(newParser.attendee_name.toString() != toRemove.toString()){
			finalArr.push(obj1);

Regards
Ankur

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

Hi @Ankur Bawiskar ,

I have written two Onchange scripts on two variables, on Self (checkbox) and on Name (reference field). And changed as you said above, but deleting all rows.

onChange script on Name field:

function onChange(control, oldValue, newValue, isLoading) {
    var self = g_form.getValue('self');
    var selfDetails = [];
    var details = '';

    var obj = {};

    obj["attendee_name"] = g_form.getDisplayBox('u_requested_for').value;
    obj["email_address"] = g_form.getValue('u_email');
    obj["department"] = g_form.getValue('u_department');
    obj["u_work_location"] = g_form.getValue('work_location');
    obj["telephone_number"] = g_form.getValue('u_telephone');
    selfDetails.push(obj);
    details = JSON.stringify(selfDetails);
    if (newValue != '' && self == 'true') {
        g_form.setValue('attendees_information', details);
    } else if (newValue == '' || self == 'false') {
        obj["attendee_name"] = g_form.getDisplayBox('u_requested_for').value;
        obj["email_address"] = g_form.getValue('u_email');
        obj["department"] = g_form.getValue('u_department');
        obj["u_work_location"] = g_form.getValue('work_location');
        obj["telephone_number"] = g_form.getValue('u_telephone');
        selfDetails.push(obj);
        details = JSON.stringify(selfDetails);
        var toRemove = g_form.getDisplayBox('u_requested_for').value;
        var finalArr = [];
        var parser = JSON.parse(details);
        for (var i = 0; i < parser.length; i++) {
            var obj1 = parser[i];
			alert(obj1);
            var newParser = JSON.parse(JSON.stringify(obj1));
            if (newParser.attendee_name.toString() != toRemove.toString()) {
                finalArr.push(obj1);
            }
        }
        g_form.setValue('attendees_information', JSON.stringify(finalArr));
    }
}

 

onChange script on Self variable:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    var user = g_form.getDisplayBox('u_requested_for').value;
    var selfDetails = [];
    var details = '';

    var obj = {};

    obj["attendee_name"] = g_form.getDisplayBox('u_requested_for').value;
    obj["email_address"] = g_form.getValue('u_email');
    obj["department"] = g_form.getValue('u_department');
    obj["u_work_location"] = g_form.getValue('work_location');
    obj["telephone_number"] = g_form.getValue('u_telephone');
    selfDetails.push(obj);
    details = JSON.stringify(selfDetails);
    if (newValue == 'true' & user != '') {
        g_form.setValue('attendees_information', details);
    } else if (newValue == 'false' || user == '') {
        obj["attendee_name"] = g_form.getDisplayBox('u_requested_for').value;
        obj["email_address"] = g_form.getValue('u_email');
        obj["department"] = g_form.getValue('u_department');
        obj["u_work_location"] = g_form.getValue('work_location');
        obj["telephone_number"] = g_form.getValue('u_telephone');
        selfDetails.push(obj);
        details = JSON.stringify(selfDetails);
        var toRemove = g_form.getDisplayBox('u_requested_for').value;
        var finalArr = [];
        var parser = JSON.parse(details);
        for (var i = 0; i < parser.length; i++) {
            var obj1 = parser[i];
			alert(obj1);
            var newParser = JSON.parse(JSON.stringify(obj1));
            if (newParser.attendee_name.toString() != toRemove.toString()) {
                finalArr.push(obj1);
            }
        }
        g_form.setValue('attendees_information', JSON.stringify(finalArr));
    }

    //Type appropriate comment here, and begin script below

}

Hi,

try adding alerts and debug

Regards
Ankur

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