javascript for automatically populating company name of logged in user.

Aki17
Kilo Guru

I set "javascript:gs.getUser().getCompanyID()" in the Default Value field to automatically populate company name of logged in user into a table form, however, the result shows sys_id of the company name...

I would like to get just company name (NOT sys_id), so could someone tell me how to fix the script?

find_real_file.png

 

Best Regards,

Aki

1 ACCEPTED SOLUTION

VigneshMC
Mega Sage

Try this

javascript:gs.getUser().getCompanyRecord().name

View solution in original post

4 REPLIES 4

Upender Kumar
Mega Sage

Hi,

Make this field as a reference type or instate of ID get company name.

VigneshMC
Mega Sage

Try this

javascript:gs.getUser().getCompanyRecord().name

Kajal Goti
Mega Guru

Hello,

 

First of all you have to make your variable as reference type and reference Company table .

Second one you have to create Script include and call in default value in variable.

 

Refer below link for more information and code.

 

https://community.servicenow.com/community?id=community_question&sys_id=9cad99ebdbde9b043882fb651f96...

 

 

Please mark as correct and helpful,If you get any help worthy

 

Krishna  Penaka
Tera Expert

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("company", user_detail.company);


 }


  },

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("company");


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


  g_form.setValue('requestor_company',company);


  g_form.setValue('requestor_name',user);


  }

}

 type: 'requestor_details'


});


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