How to auto populate logged in user details

vidhya_mouli
Giga Sage

I have a simple button to add new skills. When user clicks on this button, it takes them to the following URL.

 

https://INSTANCE.service-now.com/esc?id=form&table=sys_user_has_skill&sys_id=-1 

 

vidhya_mouli_0-1729168016817.png

 

I want to auto populate the User with the logged in user. How to do this?

 

Client script in the widget:

api.controller = function($window) {
  /* widget controller */
  var c = this;

  // Function to handle button click
  c.addSkill = function() {
		
				
    var hosturl = 'https://' + $window.location.host;
    $window.open(hosturl + "/esc?id=form&table=sys_user_has_skill&sys_id=-1", '_blank');
  };
};

 

1 ACCEPTED SOLUTION

Mayur2109
Kilo Sage
Kilo Sage

Hi @vidhya_mouli,

 

Create client script onload for the sys_user_has_skill & get logged in user there with g_user.userID. As the widget you are using is form widget it will render the client script for the form.

 

Please check and Mark Helpful and Correct if it really helps you.

Regards,
Mayur Shardul

ServiceNow Rising Star 2024

 

View solution in original post

2 REPLIES 2

Mayur2109
Kilo Sage
Kilo Sage

Hi @vidhya_mouli,

 

Create client script onload for the sys_user_has_skill & get logged in user there with g_user.userID. As the widget you are using is form widget it will render the client script for the form.

 

Please check and Mark Helpful and Correct if it really helps you.

Regards,
Mayur Shardul

ServiceNow Rising Star 2024

 

Community Alums
Not applicable

Hi @vidhya_mouli 

Can you please write this on server side:

data.skill = "";
data.user_id = gs.getUserID();
var grSkill = new GlideRecord("sys_user_has_skill");
grSkill.addQuery("user",data.user_id );
grSkill.query();
if(grSkill.next())
{

data.skill = grSkill.skill;

}

 

Client side :

api.controller = function($window) {
  /* widget controller */
  var c = this;

  // Function to handle button click
  c.addSkill = function() {
		
				
    var hosturl = 'https://' + $window.location.host;
     $window.open(hosturl + "/esc?id=form&table=sys_user_has_skill&sys_id=-1+'&skill='+c.data.skill+'&user='+c.data.user_id", '_blank')
  };
};

Please accept the solution and mark it helpful, if it helps