I need to generate the current user's name and email address in the URL and every time the hyperlink takes you to that URL the name and email address should dynamically reflect as that of the current user. Please help with the correct URL code

Utkarsha Saxena
Giga Contributor

I need to generate the current user's name and email address in the URL and every time the hyperlink takes you to that URL the name and email address should dynamically reflect as that of the current user. Please help with the correct URL code. This dynamic generation needs to happen on the click of a menu item on Service Portal.

URL is -> 

https://alstom-webchat.managed.computacenter.com/Webchat/webchat-flow.htm?name=Paul Meachen &emailaddress=Paul.Meachen@customer.com&locale=en-GB&reason=GNRL&division=x&telephone=6228006119&log...

where current user's name and email is show. This needs to change every time for the current logged in user. 

This link will open up when a menu item on the portal under a page is clicked. for which I have done the below config that when the Webchat link is clicked upon the URL should hit dynamically. 

URL I wrote in menu item HREF is - https://alstom-webchat.managed.computacenter.com/Webchat/webchat-flow.htm?un=${gs.getUserName()}&${gs.getEmail()}" target="_blank

find_real_file.png

find_real_file.png

The above is not working. Please help me with the correct code. 

 

Thanks in advance Community,

Utkarsha S

1 ACCEPTED SOLUTION

Hi Utkarsha,

you need to get the details by querying sys_user table with logged in user;

sample script below

var gr = new GlideRecord('sys_user');

gr.get(gs.getUserID());

var url1 = "https://alstomwebchat.managed.computacenter.com/Webchat/webchat-flow.htm?name=" + gr.name +  "&email=" + gr.email + "&user_name=" + gr.user_name + "&location=" + gr.location.name + "&department=" + gr.department.name + "&mobile_phone=" + gr.mobile_phone + "&first_name=" + gr.first_name;

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

15 REPLIES 15

Hi Harsh,

This gs.getUserID() when ran in the background script returns user ID not username. Could you please tell me the back end variable to fetch the user name and email address. 

that's sample method , which i had provided.

 

use same method which you have passed. just use javascript:

 

example:

 

 

https://alstom-webchat.managed.computacenter.com/Webchat/webchat-flow.htm?un=javascript:gs.getUserName()

Hi Harshvardhan,

I need to populate username, userID, his department, location, telephone no etc as per the posted URL "https://alstom-webchat.managed.computacenter.com/Webchat/webchat-flow.htm?name=Paul Meachen &emailaddress=Paul.Meachen@customer.com&locale=enGB&reason=GNRL&division=x&telephone=6228006119&logi...

from the user table, for which I made a widget and added this server side script which only shows the user ID  when the button is clicked -

 

Server side script:

 

var url1 = "https://alstomwebchat.managed.computacenter.com/Webchat/webchat-flow.htm?name=&email=&user_name=&location=&department&mobile_phone=&first_name=";

var user = gs.getUserName(); //userID
var email = gs.getEmail(); //user email
var loc = gs.getLocation(); //user location
//var user2 = gs.getUser(); //username
var fname = gs.getUser().getDomainID(); //user firstname or preferred name
var mob = gs.getUserID.getMobilePhoneNumber(); //user mobile no
//var dept = getUser().getDepartmentID();


//data.buttonhref = url1+user+email+loc+user2+fname+mob+dept;
data.buttonhref = url1+user;

data.buttonMsg = gs.getMessage(options.button_text || "Webchat");

 

Output:

https://alstomwebchat.managed.computacenter.com/Webchat/webchat-flow.htm?name=&email=&user_name=&loc...

 

Please help me correct the code to populate all values 

 

Thanks,

Utkarsha 

Hi Utkarsha,

you need to get the details by querying sys_user table with logged in user;

sample script below

var gr = new GlideRecord('sys_user');

gr.get(gs.getUserID());

var url1 = "https://alstomwebchat.managed.computacenter.com/Webchat/webchat-flow.htm?name=" + gr.name +  "&email=" + gr.email + "&user_name=" + gr.user_name + "&location=" + gr.location.name + "&department=" + gr.department.name + "&mobile_phone=" + gr.mobile_phone + "&first_name=" + gr.first_name;

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader