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

Siva Jyothi M
Mega Sage

Hi @hemathsnow002,

We can take a simple scenario here- based on the user selected on the form level, we need to auto populate the user's email ID.

User - form variable name is - form_user

Email - form variable name is - form_email

Script Include - 

 

var populateemail = Class.create();
populateemail.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getemail: function() {
        var sysid = this.getParameter('sysparm_username');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', sysid);
        gr.query();
        if (gr.next()) {
            var ans = gr.email;
            return ans;
        }
    },
    type: 'populateemail'
});

 

Client Script-

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   var ga = new GlideAjax('populateemail');
    ga.addParam('sysparm_name', 'getemail');
    var usersysid = g_form.getValue('form_user');
    ga.addParam('sysparm_username', usersysid);
    ga.getXML(callBackFunction);

    function callBackFunction(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('form_email', answer);
    }  
}

 

There is another way where we don't use script include, you can directly write on-change client script on user variable.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 var cmp = g_form.getReference('form_user', mycallback);
  function mycallback(cmp) {
        g_form.setValue('form_email', cmp.email);
}
}

 

Mark this answer as correct and helpful if it solves your issue.

 

Regards,

Siva Jyothi M.