catalog client script to get logged in username, email address, phone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2024 11:08 AM
Hi,
Pls assist me to create catalog client script to get logged in username, email address, phone.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2024 12:04 PM
Hi @BanuMahalakshmi,
You can use an onLoad Catalog client script and script include to do what you want. Client script logic follows:
function onLoad() {
//Type appropriate comment here, and begin script below
// call script include to get user data
var usr = g_user.getUserID();
alert('usr='+usr);
var ga = new GlideAjax('GetUsersData');
ga.addParam('sysparm_name','getData'); // function in the script include we're calling
ga.addParam('sysparm_user_id', usr);
/* Call function with user set and use the callback function getResponse() to return the result when ready */
ga.getXMLAnswer(getResponse);
}
// callback function for returning the result from the script include
function getResponse(response) {
var usr = response.split(',');
var uName = usr[0];
var uPhone = usr[1];
var uEmail = usr[2];
alert('name: ' + uName + ', phone: '+ uPhone + ', email: '+ uEmail);
// set variables as desired.
}
The script include is named 'getUsersData' and has Client callable checked with script:
var GetUsersData = Class.create();
GetUsersData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getData: function() {
var userID = this.getParameter("sysparm_user_id");
var result = "";
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', userID);
user.setLimit(1);
user.query();
// gs.info("GetUsersData: Found " + user.getRowCount() + " user records.");
if (user.next()) {
result = user.name+','+user.phone+','+user.email;
}
else {
result = "Unknown,Unknown,Unknown";
}
// gs.info("GetUsersData: Returning: " + result);
// return JSON.stringify(result);
return result;
},
type: 'GetUsersData'
});
Should you choose this approach.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2024 12:05 PM - edited 07-06-2024 12:08 PM
Hi @BanuMahalakshmi ,
I tried your problem in my PDI and it works for me please refer below steps
Create onLoad client script and add below code
function onLoad() {
//Type appropriate comment here, and begin script below
alert("ID = " + g_user.userID);
var ga = new GlideAjax('getUserDetails');
ga.addParam('sysparm_name', 'userFunc');
ga.addParam('sysparm_userid', g_user.userID);
ga.getXML(updateUserHandler);
function updateUserHandler(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("answer = " + JSON.parse(answer));
var a = JSON.parse(answer);
g_form.setValue("email", a.email);
g_form.setValue("phone", a.phone);
g_form.setValue("username", a.userName);
g_form.update();
}
}
Create script include which is client callable and add below code
var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
userFunc: function(){
var userId = this.getParameter('sysparm_userid');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', userId);
gr.query();
if(gr.next()){
var obj = {};
obj.userName = gr.user_name.toString();
obj.email = gr.email.toString();
obj.phone = gr.phone.toString();
gs.log("OBJOBJ = " + JSON.stringify(obj));
gs.log("OBJOBJ = " + JSON.stringify(obj.email));
return JSON.stringify(obj);
}
},
type: 'getUserDetails'
});
Result
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2024 12:24 PM
Hi there @BanuMahalakshmi
you can try this
function onLoad() {
var user = g_user.userName;
var email = g_user.email;
var phone = g_user.phone;
// Use these variables as needed
console.log("Username: " + user);
console.log("Email: " + email);
console.log("Phone: " + phone);
}
Kind Regards,
Mohamed Azarudeen Z
Developer @ KPMG
Microsoft MVP (AI Services), India