Auto populate user information on Load

tiyasa
Giga Expert

Hello everyone,

So I need to auto fill User information based on the current logged in User. I did search the community and found loads of suggestions but mostly

when a user is selected and the related other fields are populated.

I tried to set the following values to Default for the variables but most did not work

  1. Requested For : javascript:gs.getUserName(); Worked fine.
  2. Phone Number : javascript:gs.getMobileNumber();
  3. Company : javascript:gs.getUser().getCompanyID();
  4. Location : javascript:gs.getUser().getLocation(); Returns a value but per the user record of the current logged in user, the value returned is Country not Location.
  5. Department :
  6. Division :
  7. BU:
  8. CU:
  9. Cost Center :

find_real_file.png

Additionally, I did try to create a catalog client script on load and fetch the object of the user record, but it doesn't work too.

Thank you ,

Tiyasa

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi,



For Auto population of the above defined Variables you can write an Script Include and call the same Script Include in an OnLoad Catalog Client Script as mentioned below:



Script Include:



var requestor_details = Class.create();


requestor_details.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  requestor_info: function() {


  var result = this.newItem("result");


  var logged_user = gs.getUserID();


  var user_detail = new GlideRecord('sys_user');


  user_detail.addQuery('sys_id',logged_user);


  user_detail.query();


  while(user_detail.next())


  {


  result.setAttribute("user",user_detail.sys_id);


  result.setAttribute("phone", user_detail.phone);


  result.setAttribute("email",user_detail.email);


  result.setAttribute("location",user_detail.location);



  }


  },



  type: 'requestor_details'


});



find_real_file.png



2) Then you can write an On Load Catalog Client Script wither on your Catalog Item or on the Variable Set to have the variables populated as shown below:



Script:



function onLoad()


{


  var ga = new GlideAjax("requestor_details");


  ga.addParam("sysparm_name","requestor_info");


  ga.getXML(ajaxResponse);


  function ajaxResponse(serverResponse) {


  var result = serverResponse.responseXML.getElementsByTagName("result");


  var phone = result[0].getAttribute("phone");


  var user = result[0].getAttribute("user");


  var email = result[0].getAttribute("email");


  var loc = result[0].getAttribute("location");


  g_form.setValue('requestor_phone',phone);


  g_form.setValue('requestor_name',user);


  g_form.setValue('Location_user',loc);


  }


}



find_real_file.png



Replace your Variable Name appropriately in the Set Value Conditions say for example in place of the variable "Location_user" with your Location Variable.



In a Similar Way you can try for other Variables also.



Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

26 REPLIES 26

Hi Tiyasa



I would strongly recommend against referring to the Wiki for coding work as this is only relevant for Fuji and older releases. You are best served using the APIs described here: https://developer.servicenow.com/app.do#!/api_doc



The reason for this is that the platform can change where some API calls can change or be removed in later releases. The developer docs are more up to date with what you can do.



Having said that, there are other options to achieve this:


  1. Just have the Requester reference field and rely on the Reference Icon to see other values (as suggested earlier by Chuck)
  2. Dot-walk to the respective fields on the form (see Configure a form and Dot-walking)
  3. Use GlideAjax to populate the other fields
  4. To get all the details in one hit and minimise on multiple server lookups you can work with the g_scratchpad object


Regards


Shahid


Noted. Thank you for the suggestion.


Vignesh11
Tera Expert

Hi Tiyasa,



Create a Onload client script and script include.


Make Glide Ajax call to make client- server interaction and auto populate field information.


http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0



http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices#Example:_Asynchronous_GlideA...


dilini
Giga Expert

Tiyasa,



You can auto populate phone number and department by adding following code in the variable default value .



phone number



javascript: var userPhone;


var user = new GlideRecord('sys_user');


if (user.get(gs.getUserID()))


{userPhone = user.phone};


userPhone;



department



javascript: var user = new GlideRecord('sys_user');


if (user.get(gs.getUserID())) {


user.department.sys_id;


}



please check sys_user table. It has Location, Cost Center, Unit field so you can change above codes and auto populate those fields too.



Dilini


Hi Dilini,



Was really excited to try this out, but no luck Does it work for you?



Thanks,


Tiyasa