How to auto populate value in MRVS variable

Barath P
Tera Guru

Hi All,

I hope you're doing well.

I'm working on catalog item MRVS and need some assistance. I have a reference variable named "User Name" outside of the MRVS. Inside the MRVS, there is a string field called "User ID." I would like to populate the "User ID" field in the MRVS with the value from the "User Name" reference variable. Specifically, whenever a user enters a value in the "User Name" field and then clicks the "Add" button in the MRVS, the "User ID" field should automatically populate with the same value of User name. i am getting only sys_id in user id it is not getting display value in user id. I tried all method but not working for MRVS. Could anyone guide me on how to achieve this functionality?


onLoad client script:

function onLoad() {

var getUserName = g_service_catalog.parent.getValue('user_name');

if(getUserName){
	g_form.setValue('user_id', getUserName);
}
}

 

Screenshot 2024-09-12 180128.png

Thank you!

Best regards, 
@Barath P 

1 ACCEPTED SOLUTION

Barath P
Tera Guru

I got this requirement. if anyone want to know I have added the script in below. I had a reference variable called User Name outside the MRVS and a string field called User ID inside the MRVS. The goal was to automatically populate the User ID field with the display value of the User Name when the user selects a value and adds a row to the MRVS.

  • Script Include: To fetch the display value of the User Name from the sys_user table using the sys_id.
  • Client Script: To call the Script Include via GlideAjax and set the value of the User ID field in the MRVS.

    Here’s the Script Include:
var MRVSUtil = Class.create();
MRVSUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserDisplayName: function() {
        var NewUser = this.getParameter('sysparm_userSysId'); // Get the sys_id from the client script
        var gr = new GlideRecord('sys_user'); 
        gr.get(NewUser);
        return gr.name; // Return the display value (User Name)
    },
    
    type: 'MRVSUtil'
});


Here’s the Client Script:

function onLoad() {
    var getUserName = g_service_catalog.parent.getValue('user_name'); // Get the sys_id of the selected user

    var ga = new GlideAjax('MRVSUtil'); // Initialize GlideAjax to call Script Include
    ga.addParam('sysparm_name', 'getUserDisplayName'); // Specify the Script Include function
    ga.addParam('sysparm_userSysId', getUserName); // Pass the sys_id of the selected user

    ga.getXMLAnswer(function(response) {
        g_form.setValue('user_id', response); // Set the User ID field with the display value
    });
}


Screenshot 2024-09-13 145226.png

Thank you!

Best regards, 
@Barath Prathap 



 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Reference variables and fields show a Display column for the referenced table, but the value stored is the sys_id of the record.  To lookup the user_name field from the user record you'll need a GlideAjax call to a Script Include, or getReference.  Here is the shorter/easier method:

function onLoad() {

    var getUserName = g_service_catalog.parent.getReference('user_name', userLookup);
    function userLookup(getUserName){
	    g_form.setValue('user_id', getUserName.user_name);
    }
}

The last mention of user_name refers to the field on the user record with that name, not the variable name as is the first mention.  I haven't tried getReference with g_service_catalog, so if it doesn't work, alert on getUserName to confirm it's not returned, then revert to

var getUserName = parent.g_form.getReference('user_name', userLookup);

if you're not using Service Portal.

Barath P
Tera Guru

I got this requirement. if anyone want to know I have added the script in below. I had a reference variable called User Name outside the MRVS and a string field called User ID inside the MRVS. The goal was to automatically populate the User ID field with the display value of the User Name when the user selects a value and adds a row to the MRVS.

  • Script Include: To fetch the display value of the User Name from the sys_user table using the sys_id.
  • Client Script: To call the Script Include via GlideAjax and set the value of the User ID field in the MRVS.

    Here’s the Script Include:
var MRVSUtil = Class.create();
MRVSUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserDisplayName: function() {
        var NewUser = this.getParameter('sysparm_userSysId'); // Get the sys_id from the client script
        var gr = new GlideRecord('sys_user'); 
        gr.get(NewUser);
        return gr.name; // Return the display value (User Name)
    },
    
    type: 'MRVSUtil'
});


Here’s the Client Script:

function onLoad() {
    var getUserName = g_service_catalog.parent.getValue('user_name'); // Get the sys_id of the selected user

    var ga = new GlideAjax('MRVSUtil'); // Initialize GlideAjax to call Script Include
    ga.addParam('sysparm_name', 'getUserDisplayName'); // Specify the Script Include function
    ga.addParam('sysparm_userSysId', getUserName); // Pass the sys_id of the selected user

    ga.getXMLAnswer(function(response) {
        g_form.setValue('user_id', response); // Set the User ID field with the display value
    });
}


Screenshot 2024-09-13 145226.png

Thank you!

Best regards, 
@Barath Prathap