g_user is not working. WHY????

Anand Shukla
Mega Guru

Hi All,

my Onload script is not working. Please help

 

function onLoad() {

var a = g_user.userID.email;

alert("Result: " +a);

}

 

I am getting result as Undefined.

2 REPLIES 2

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Anand Shukla ,

There are limitations of using and accessing the user object. You can go with the following script if you want email of current loggedin user at client side :  

 

Client Script : OnLoad   

 

function onLoad() {
//Type appropriate comment here, and begin script below
var a = g_user.userID;
var ga = new GlideAjax('checkCIandCC');
ga.addParam('sysparm_name', 'getData');
ga.addParam('sysparm_user', a);
ga.getXML(getData);

function getData(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}

}   

 

 

Script Include: Client Callable true  

 

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

getData: function() {
var sysID = this.getParameter('sysparm_user');
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", sysID);
gr.query();
if (gr.next()) {
return gr.email;
}

 

},

type: 'checkCIandCC'
});  

 

find_real_file.png

Please mark my answer as helpful/correct if it resolves your query.

 

Regards,

Gunjan Kiratkar

Consultant - ServiceNow, Cloudaction

Rising Star 2022

 

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Jaspal Singh
Mega Patron
Mega Patron

Why not use

var email = gs.getUser().getEmail();

Refer link for cheats.