We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to get the logged In user's company ID in the form.

shrinivasprasad
Tera Expert

Hi All,

I am trying to fetch the logged in user's company ID.

for example, my credentials are:  Name: shrinivasprasad naikodi

first name: shrinivasprasad

last name: naikodi.

company ID: sp123

email id: shrinivasprasad@xyz.com

so I just now logged in to the instance and opened one catalog form which has the company ID filed, so in that field, I need 'sp123'

Thanks,

Shrinivasprasad 

 

2 REPLIES 2

Sudhanshu Talw1
Tera Guru

If you need company sys_id to set in the reference field pointing to company table try:

Try:
javascript:gs.getUser().getCompanyID();

in the Default value field

 

For comapnay ID if it is a different field then create a onLoad client script & Script include as ,

function onLoad() {

var ga = new GlideAjax('companyUtil');

ga.addParam('sysparm_name', 'getCompanyName');
ga.getXML(analyzeResponse); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)
}

function analyzeResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
//you can do anything here with the form, the answer variable holds the name of the company
}

 

var companyUtil = Class.create();

companyUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

      getCompanyName: function() {

              var compName = ''; // Return value

               var grComp   = new GlideRecord('core_company');

              if (grComp.get(gs.getUser().getCompanyID())) {

              compName = grComp.name;//Return the company ID or any field you want

              }

            return compName;

      }

});

 

 

Thanks

Sudhanshu

 

 

RudhraKAM
Tera Guru

open the company variable and in the default value paste this 

javascript:gs.getUser().getCompanyID(); and save