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

RaghavSh
Kilo Patron
userObj.gruserid = gr.getValue('user_name');  // try this. since user_name is string fiekd DisplayValue is not required, rest you code seems fine.

// also avoid using gr as variable.
// make user AA has valid value in user_name

 

is the field/variable "userName" a reference field referring to user tabkle.


Raghav
MVP 2023
LinkedIn

Thank you for your response.

Bert_c1
Kilo Patron

I use the following in a client script to parse an object returned by my script include:

 

function EmployeeDetailsLookup(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var result = JSON.parse(answer);
	//  set from fields based on the response...
}

what you have works for me when a single value is returned by the script include.  Also, I use:

 

	ga.getXML(EmployeeDetailsLookup); // Always try to use asynchronous call

in the client script to call the function to process the result.

 

But you're returning one value, just return the value, not an object.

Rafael Batistot
Kilo Patron

Hi @nisargac21 

The issue is in your scrip include in this part

 if (gr.get(this.getParameter('sysparm_userSelected'))) {
             userObj.gruserid = gr.user_name.getDisplayValue();
        }

 

  • gr.get() expects a sys_id of the user record, but in your Client Script you’re passing g_form.getValue('userName') which is most likely not the sys_id but instead the Name or Display Value of the user.

  • So your gr.get() call often fails and you don’t get the correct user_name.


See the correct code:

Client Script

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

    var userSysId = g_form.getValue('userName'); // sys_id from reference field
    var ga = new GlideAjax('populateUserID');
    ga.addParam('sysparm_name', 'details');
    ga.addParam('sysparm_userSelected', userSysId);
    ga.getXML(updateCampus);

    function updateCampus(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var userObj = JSON.parse(answer);

        // Use whichever options you need
        g_form.setValue('UserIDornumber', userObj.option_1); 
        g_form.setValue('userEmail', userObj.option_2);
        g_form.setValue('userFullName', userObj.option_3);
        g_form.setValue('userDepartment', userObj.option_4);
    }
}


Include Script

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

    details: function() {
        var userObj = {};
        var userSysId = this.getParameter('sysparm_userSelected');

        var gr = new GlideRecord('sys_user');
        if (gr.get(userSysId)) {
            userObj.option_1 = gr.user_name.toString();   // login ID
            userObj.option_2 = gr.email.toString();       // email
            userObj.option_3 = gr.name.toString();        // full display name
            userObj.option_4 = gr.department.getDisplayValue(); // department name
        }
        return JSON.stringify(userObj); // return as JSON
    },

    type: 'populateUserID'
});


See the reference to how create a glideAjax

GlideAjax Example Cheat Sheet - ServiceNow Community