Auto populate user details through script include and glide ajax

hemathsnow002
Tera Contributor

Auto populate user details through script include and glide ajax

5 REPLIES 5

Namrata Ghorpad
Mega Sage
Mega Sage

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Here you go:

- 2020-01-10 - Article - Client Side Scripting: Go for GlideAjax (with getXMLAnswer)!

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Raghu3
Tera Contributor

Refer below code and change it as per your need, Make sure you mark client callable true on script include

 

Client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
 
var ga = new GlideAjax("userDetailsUtil");// Script include name
    ga.addParam('sysparm_name', 'getUserDetails');//Script include function name
    ga.addParam('sysparm_usersysid', newValue);// newValue object in onchange client script always return changed value of the field
 
    ga.getXML(getDetails);//Calling call back function to convert response and set values
 
    function getDetails(response) {//call back function
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var objval = JSON.parse(answer);
g_form.setValue("u_department",objval.department);
 
        alert(objval.department);
 
}
 
   
}
 
 
Script include:
var userDetailsUtil = Class.create();
userDetailsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails:function(){//Function declaration
var obj = {};//json to collect the information
  var usersysid = this.getParameter('sysparm_usersysid');
var gr=new GlideRecord('sys_user');//glide user table
gr.get(usersysid);
obj.email=gr.getValue('email');//map values
obj.department=gr.getValue('department');
 
 
var json = new JSON();
      return json.encode(obj);
 
},
 
    type: 'userDetailsUtil'
});

maroon_byte
Mega Sage

Is it for catalog item variables? or is it for form in Core UI?

If it's for catalog item variables, then why use scripts? Instead, use 'Service catalog data lookup'

https://docs.servicenow.com/en-US/bundle/vancouver-servicenow-platform/page/product/service-catalog-...