Autopopulate email filed based on caller using GlideAjax

navya4
Tera Contributor

HI Team,

iam trying to autopopulate email field using caller, but iam failing to achieve the results.

could you please help me checking the below code.

 

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var getEmailAddr = new GlideAjax('ATQinfo'); // script include name
    getEmailAddr.addParam('sysparm_name','populateUser'); //function/method name
    //Pass the Requested for sys_id
    getEmailAddr.addParam('sysparm_user_name', g_form.getValue('caller_id'));
    getEmailAddr.getXML(userInfo);

    function userInfo(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.clearValue('email');
        g_form.setValue('email', answer);

    }
}
 
script Include:
var ATQinfo = Class.create();
ATQinfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    populateUser: function() {
        var uid = this.getParameter('sysparm_userID');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', uid);
        gr.query();
        if (gr.next()) {
            return gr.email;
        }

    },
    type: 'ATQinfo'
});
2 ACCEPTED SOLUTIONS

Service_RNow
Mega Sage

Hi @navya4 

Here is the solution to this -

Create a new client callable script include with following function 

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

getUserInfo: function() {
var details = {};
var userId = this.getParameter('sysparm_user_id');
var userObj = new GlideRecord('sys_user');
userObj.addQuery('sys_id', userId);
userObj.query();
if (userObj.next()) {
details.email= userObj.email_id.toString();
}
return JSON.stringify(details);
},

type: 'UserDetails'

});

 

Create a new catalog client script on the record producer/request form

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

var ajax = new GlideAjax('UserDetails');
ajax.addParam('sysparm_name', 'getUserInfo');
ajax.addParam('sysparm_user_id', g_form.getValue('employee_name')); // change variable name here
ajax.getXML(doSomething);

function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var answers = JSON.parse(answer);
g_form.setValue('var_email_id', answers.email.toString()); // change variable name here
}
}

 

Please mark reply as Helpful/Correct, if applicable. Thanks!

 

View solution in original post

Harish KM
Kilo Patron
Kilo Patron

Hello @navya4 I have updated your code based on correction needed, refer bold lines corrected

var ATQinfo = Class.create();
ATQinfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
populateUser: function() {
var uid = this.getParameter('sysparm_user_name');// need to pass sysparm_user_name which you have in client script
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', uid);
gr.query();
if (gr.next()) {
return gr.getValue('email'); //always use getValue method
}

},
type: 'ATQinfo'
});

----
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var getEmailAddr = new GlideAjax('ATQinfo'); // script include name
getEmailAddr.addParam('sysparm_name','populateUser'); //function/method name
//Pass the Requested for sys_id
getEmailAddr.addParam('sysparm_user_name', g_form.getValue('caller_id'));
getEmailAddr.getXMLAnswer(userInfo); // use getXMLAnswer

function userInfo(answer) {
alert(answer);
g_form.setValue('email', answer);

}
}

Regards
Harish

View solution in original post

4 REPLIES 4

Service_RNow
Mega Sage

Hi @navya4 

Here is the solution to this -

Create a new client callable script include with following function 

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

getUserInfo: function() {
var details = {};
var userId = this.getParameter('sysparm_user_id');
var userObj = new GlideRecord('sys_user');
userObj.addQuery('sys_id', userId);
userObj.query();
if (userObj.next()) {
details.email= userObj.email_id.toString();
}
return JSON.stringify(details);
},

type: 'UserDetails'

});

 

Create a new catalog client script on the record producer/request form

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

var ajax = new GlideAjax('UserDetails');
ajax.addParam('sysparm_name', 'getUserInfo');
ajax.addParam('sysparm_user_id', g_form.getValue('employee_name')); // change variable name here
ajax.getXML(doSomething);

function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var answers = JSON.parse(answer);
g_form.setValue('var_email_id', answers.email.toString()); // change variable name here
}
}

 

Please mark reply as Helpful/Correct, if applicable. Thanks!

 

Harish KM
Kilo Patron
Kilo Patron

Hello @navya4 I have updated your code based on correction needed, refer bold lines corrected

var ATQinfo = Class.create();
ATQinfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
populateUser: function() {
var uid = this.getParameter('sysparm_user_name');// need to pass sysparm_user_name which you have in client script
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', uid);
gr.query();
if (gr.next()) {
return gr.getValue('email'); //always use getValue method
}

},
type: 'ATQinfo'
});

----
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var getEmailAddr = new GlideAjax('ATQinfo'); // script include name
getEmailAddr.addParam('sysparm_name','populateUser'); //function/method name
//Pass the Requested for sys_id
getEmailAddr.addParam('sysparm_user_name', g_form.getValue('caller_id'));
getEmailAddr.getXMLAnswer(userInfo); // use getXMLAnswer

function userInfo(answer) {
alert(answer);
g_form.setValue('email', answer);

}
}

Regards
Harish

navya4
Tera Contributor

Thanku @Harish KM corrections u made in my code helped me to get the expected results.

 

navya4_0-1709320978837.png

 

Jyoti Jadhav9
Tera Guru

Hi @navya4 

 

In the script Include, you are passing wrong parameter. correct it. It will work fine.

 

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var getEmailAddr = new GlideAjax('ATQinfo'); // script include name
    getEmailAddr.addParam('sysparm_name','populateUser'); //function/method name
    //Pass the Requested for sys_id
    getEmailAddr.addParam('sysparm_user_name', g_form.getValue('caller_id'));
    getEmailAddr.getXML(userInfo);

    function userInfo(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.clearValue('email');
        g_form.setValue('email', answer);

    }
}

 

ScriptInclude:

var ATQinfo = Class.create();
ATQinfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
populateUser: function() {
        var uid = this.getParameter('sysparm_user_name');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', uid);
        gr.query();
        if (gr.next()) {
            return gr.email;
        }

    },
    type: 'ATQinfo'
});
 

Please hit the like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

 

Thanks & Regards

Jyoti Jadhav