i am unable to get the userdetails instead it is return null value.can any one help me

Kongaleti Navee
Tera Contributor
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var abc = g_form.getValue('u_name');
 
    var ga = new GlideAjax('AutoPopulateUserDetails');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_usr', 'abc');
    ga.getXML(ResponseFunction);
 
    function ResponseFunction(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
        var results = JSON.parse(answer);
 
        g_form.setValue('u_first_name',);
        g_form.setValue('u_last_name',);
        g_form.setValue('u_location',);
        g_form.setValue('u_manager',);
        g_form.setValue('u_email',);
 
 
 
 
 
var AutoPopulateUserDetails = Class.create();
AutoPopulateUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    type: 'AutoPopulateUserDetails',
    getUserDetails: function() {
        var xyz = this.getParameter('sysparm_usr');
 
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', xyz);
        gr.query();
if(gr.next()){
 
return gr.u_first_name +";" + gr.u_last_name + ";" + gr.u_manager + ";" + gr.u_location + ";"+ gr.u_email;
 
}
 
    },
 
 
});
6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

Your client script should look more like this - with some temporary alerts for troubleshooting:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var abc = g_form.getValue('u_name');
    alert("Name = " + abc);

    var ga = new GlideAjax('AutoPopulateUserDetails');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_usr', abc);
    ga.getXML(ResponseFunction);

    function ResponseFunction(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert('Answer = ' + answer);
        g_form.setValue('u_first_name', answer.split(';')[0]);
        g_form.setValue('u_last_name', answer.split(';')[1]);
        g_form.setValue('u_manager', answer.split(';')[2]);
        g_form.setValue('u_location', answer.split(';')[3]);
        g_form.setValue('u_email', answer.split(';')[4]);
    }
}

And a couple of changes to the Script Include to use the correct field names, prevent unexpected results returning sys_id and email type fields, and aide in the troubleshooting and a best practice of always returning a value:

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

    type: 'AutoPopulateUserDetails',
    getUserDetails: function() {
        var answer = 'not found';
        var xyz = this.getParameter('sysparm_usr');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', xyz);
        gr.query();
        if (gr.next()) {
             answer = gr.first_name +";" + gr.last_name + ";" + gr.manager.toString() + ";" + gr.location.toString() + ";"+ gr.email.toString();
        }
        return answer;
    },
 });

This is assuming your u_manager and u_location fields on the form that you are setting are reference type fields, otherwise use gr.manager.name and gr.location.name. 

 

Tai Vu
Kilo Patron
Kilo Patron

Hi @Kongaleti Navee 

Let's give the Auto-populate feature a try without a single line of code.

Sample.

Timi_0-1703516762667.png

 

Cheers,

Tai Vu