catalog client script to get logged in username, email address, phone

BanuMahalakshmi
Tera Contributor

Hi,

 

Pls assist me to create catalog client script to get logged in username, email address, phone.

 

Thanks.

7 REPLIES 7

Bert_c1
Kilo Patron

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.

Community Alums
Not applicable

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();
	}

}

 

SarthakKashyap_1-1720292478937.png

 

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'
});

 

SarthakKashyap_0-1720292462910.png

 

Result 

SarthakKashyap_1-1720292914553.png

 

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

 

Its_Azar
Tera Guru

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);
}
☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India