getCompanyID is not acquiring companyID

leolin94
Kilo Explorer

Hi,

I am trying to acquire login user's CompanyID by using this code:

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

However, when I try this code on Scripts Background, it returns blank.

find_real_file.png

Result:

find_real_file.png

One of the post said that there might be some ACL restriction from core_company table which prevent it to be read.

Anybody know how to solve this issue??

 

Thanks in advance!

 

Best regards,

Leo

4 REPLIES 4

Maik Skoddow
Tera Patron
Tera Patron

Hi @leolin94 

did you execute above code at script console as an admin?

If so, did you check the admin's user record that there is a company set?

If not, please provide more details and screenshots as it is not really clear what your concern is!

Kind regards
Maik

Frank Tate
Giga Guru
Giga Guru

Adding to what @Maik Skoddow suggested, when trying new things in ServiceNow, the very first place you want to start is in your PDI as the admin user. Once you have things working, you can then try it as a lesser user and start battling things like permissions, roles, user criteria, etc.

Frank

Mark Roethof
Tera Patron
Tera Patron

Hi there,

The syntax itself is correct.

How are you testing this? Can you share your way of testing?

For example, I also just reproduced. Initially, also my test gave a blanc result. Checked my user record, no company... filled the company, retest: still blanc.

Logged off, logged in again, retest (exact same code): success.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hitoshi Ozawa
Giga Sage
Giga Sage

I'm having the same problem with my instance. gs.getUser().getCompanyID() isn't returning a value.

It seems logging out and logging in resolves the problem. After logging out and then in, gs.getUser().getCompanyID() was able to return a proper value.

 

Following script, however, is able to get the value without doing a logout/login so it may be better.

var gr = new GlideRecord('sys_user');
if (gr.get(gs.getUserID())) {
  gs.info(gr.company);
}