Trying to fetch user details via GlideAjax

Roshini
Giga Guru

Hi Team,

Am trying with below client script and server side script to get the alert message on form for the selected caller.

Client script 

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

   //Type appropriate comment here, and begin script below

   var alertDetails = new GlideAjax('UserLearning');
   alertDetails.addParam('sysparam_name','userDetails');
   alertDetails.addParam('sysparam_userid', newValue);
   alertDetails.getXMLAnswer(details);
   function details (detail)
   {
    if (detail)
    {
    var a = JSON.parse(detail);
    alert("email is " + a.email);
   }
   }
   
}
Script include
var UserLearning = Class.create();
UserLearning.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
userDetails: function(){
    var id = this.getParameter('sysparam_userid');
var gr = new GlideRecord('sys_user');
gr.get(id);
 var userObj ={
    "email": gr.getValue("email"),
 };
 
 return JSON.stringify(userObj);
 
},
    type: 'UserLearning'
});
But am not getting null, can someone help me out to find the error
1 ACCEPTED SOLUTION

SN_Learn
Kilo Patron
Kilo Patron

Hi @Roshini ,

 

All code is correct, just a mistype is there which is causing the issue.

 

In Catalog Client Script, Instead of:

alertDetails.addParam('sysparam_name','userDetails');
alertDetails.addParam('sysparam_userid', newValue);

 It will be:

alertDetails.addParam('sysparm_name','userDetails');
alertDetails.addParam('sysparm_userid', newValue);

 

In the Script include, Instead of:

var id = this.getParameter('sysparam_userid');

It will be:

var id = this.getParameter('sysparm_userid');

 

The rectified final code will be:

 

Script Include:

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

    userDetails: function() {
        var id = this.getParameter('sysparm_userid');
        var gr = new GlideRecord('sys_user');
        if (gr.get(id)) {
            var userObj = {
                "email": gr.getValue("email"),
            };
            return JSON.stringify(userObj);
        }
    },
    type: 'UserLearning'
});

 

Catalog Client script:

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

    var alertDetails = new GlideAjax('UserLearning');
    alertDetails.addParam('sysparm_name', 'userDetails');
    alertDetails.addParam('sysparm_userid', newValue);
    alertDetails.getXMLAnswer(details);

    function details(detail) {
        if (detail) {
            var a = JSON.parse(detail);
             alert("email is " + a.email);
        }
    }

}

 

The output is:

 

SN_Learn_0-1720380939410.png

 

 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

View solution in original post

5 REPLIES 5

Satishkumar B
Giga Sage
Giga Sage

Hi @Roshini 

 

Tyr this:

Client Script:


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

var alertDetails = new GlideAjax('UserLearning'); // Corrected script include name
alertDetails.addParam('sysparam_name', 'userDetails');
alertDetails.addParam('sysparam_userid', newValue);

alertDetails.getXMLAnswer(function(response) {
var detail = response.responseText;
if (detail) {
var a = JSON.parse(detail);
alert("Email is " + a.email);
} else {
alert("No email found for the selected user.");
}
});
}

Script Include 


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

userDetails: function() {
var id = this.getParameter('sysparam_userid');
var gr = new GlideRecord('sys_user');
if (gr.get(id)) {
var userObj = {
"email": gr.getValue("email"),
};
return JSON.stringify(userObj);
} else {
return "{}"; // Return empty object if user is not found
}
},

type: 'UserLearning'
});

———————————————-
Please consider marking my reply as Helpful👍 and/or Accept Solution☑️, if applicable. Thanks!

Its_Azar
Tera Guru

Hi there @Roshini 

 

Hi Roshini,

Your script looks correct, check these things to check and adjust:

Make sure the Script Include UserLearning is marked as Client Callable.

Add some debugging information to see where it might be failing.

Client script

 

 

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

   var alertDetails = new GlideAjax('UserLearning');
   alertDetails.addParam('sysparam_name', 'userDetails');
   alertDetails.addParam('sysparam_userid', newValue);
   alertDetails.getXMLAnswer(details);

   function details(response) {
      if (response) {
         var a = JSON.parse(response);
         alert("email is " + a.email);
      } else {
         alert("No response from server");
      }
   }
}

 

 

 

SI

 

 

var UserLearning = Class.create();
UserLearning.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
   userDetails: function() {
      var id = this.getParameter('sysparam_userid');
      var gr = new GlideRecord('sys_user');
      if (gr.get(id)) {
         var userObj = {
            "email": gr.getValue("email")
         };
         return JSON.stringify(userObj);
      } else {
         return JSON.stringify({"error": "User not found"});
      }
   },

   type: 'UserLearning'
});

 

 

 

 

 make sure the Client Callable checkbox is checked.

Ensure newValue in your client script is the correct sys_id of the user.

 

If this helps kindly accept the answer thanks mych,

 

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India

Hi Thanks or your reply, 
am getting no response from server while modifying the script.
is there any alteration can be done to get the value.
The user is havin email id in sys_user profile.

SN_Learn
Kilo Patron
Kilo Patron

Hi @Roshini ,

 

All code is correct, just a mistype is there which is causing the issue.

 

In Catalog Client Script, Instead of:

alertDetails.addParam('sysparam_name','userDetails');
alertDetails.addParam('sysparam_userid', newValue);

 It will be:

alertDetails.addParam('sysparm_name','userDetails');
alertDetails.addParam('sysparm_userid', newValue);

 

In the Script include, Instead of:

var id = this.getParameter('sysparam_userid');

It will be:

var id = this.getParameter('sysparm_userid');

 

The rectified final code will be:

 

Script Include:

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

    userDetails: function() {
        var id = this.getParameter('sysparm_userid');
        var gr = new GlideRecord('sys_user');
        if (gr.get(id)) {
            var userObj = {
                "email": gr.getValue("email"),
            };
            return JSON.stringify(userObj);
        }
    },
    type: 'UserLearning'
});

 

Catalog Client script:

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

    var alertDetails = new GlideAjax('UserLearning');
    alertDetails.addParam('sysparm_name', 'userDetails');
    alertDetails.addParam('sysparm_userid', newValue);
    alertDetails.getXMLAnswer(details);

    function details(detail) {
        if (detail) {
            var a = JSON.parse(detail);
             alert("email is " + a.email);
        }
    }

}

 

The output is:

 

SN_Learn_0-1720380939410.png

 

 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.