The Zurich release has arrived! Interested in new features and functionalities? Click here for more

'Undefined' value on user ID field when i try to select the user name

nisargac21
Tera Contributor

nisargac21_0-1756224135038.png

I have written script include and catalog client script to populate the user id on selection of user name. But not getting the correct value on user id field.

Anyone have idea on this.

 

Script include:

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

        details: function() {
        var userObj = {};
        var gr = new GlideRecord('sys_user');
        if (gr.get(this.getParameter('sysparm_userSelected'))) {
             userObj.gruserid = gr.user_name.getDisplayValue();
        }
        return JSON.stringify(userObj);
    },

    type: 'populateUserID'
});
 
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        //g_form.clearValue('UserIDornumber');
        return;
    }
    var name = g_form.getValue('userName');
    var userDetails = new GlideAjax('populateUserID');
    userDetails.addParam('sysparm_name', 'details');
    userDetails.addParam('sysparm_userSelected', name);
    userDetails.getXMLAnswer(function(response) {
        var userObj = JSON.parse(response);

            g_form.setValue('UserIDornumber', userObj.gruserid);
    });
}
 
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@nisargac21 

if this is for catalog item then you can use Auto populate feature with no scripting

Auto-populate a variable based on a reference type variable (Utah) 

If you still want to use script then update as this

Script include: no need to return json as you are returning only 1 value

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

        details: function() {
        var gr = new GlideRecord('sys_user');
        if (gr.get(this.getParameter('sysparm_userSelected'))) {
             return gr.user_name.toString();
        }
    },

    type: 'populateUserID'
});
 Catalog Client Script: no need to parse now
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    if (newValue == '')
        g_form.clearValue('UserIDornumber');

    var name = g_form.getValue('userName');
    var userDetails = new GlideAjax('populateUserID');
    userDetails.addParam('sysparm_name', 'details');
    userDetails.addParam('sysparm_userSelected', name);
    userDetails.getXMLAnswer(function(response) {
        g_form.setValue('UserIDornumber', response);
    });
}
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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@nisargac21 

if this is for catalog item then you can use Auto populate feature with no scripting

Auto-populate a variable based on a reference type variable (Utah) 

If you still want to use script then update as this

Script include: no need to return json as you are returning only 1 value

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

        details: function() {
        var gr = new GlideRecord('sys_user');
        if (gr.get(this.getParameter('sysparm_userSelected'))) {
             return gr.user_name.toString();
        }
    },

    type: 'populateUserID'
});
 Catalog Client Script: no need to parse now
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    if (newValue == '')
        g_form.clearValue('UserIDornumber');

    var name = g_form.getValue('userName');
    var userDetails = new GlideAjax('populateUserID');
    userDetails.addParam('sysparm_name', 'details');
    userDetails.addParam('sysparm_userSelected', name);
    userDetails.getXMLAnswer(function(response) {
        g_form.setValue('UserIDornumber', response);
    });
}
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

Thank you for your response, I tried the 1st solution, Auto-populate a variable based on a reference type. It worked.