Get domain of current user

Rj27
Mega Guru

Hi There,

I want to fetch the domain of current user in my script include.

i am using ...
var domain = gs.getUser().getDomainID();
gs.print('Domain ID is =='+domain )

This is giving me the domain of the user from domain picker. but i want to get the value which is there in the user table.
can  i not get the value directly without gliderecording the user table?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Rj27 

Just to inform gs.getUser().getRecord().getValue('fieldName') won't work in scoped application

It would work in global scope.

For Global Scope:

gs.info('Domain ID is ==' + gs.getUser().getRecord().getValue('sys_domain'));

For Custom scope

better you query sys_user table for that user and get the domain value

var user = new GlideRecord('sys_user');

user.get(gs.getUserID());

gs.info('Domain ID is ==' + user.sys_domain);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Willem
Giga Sage
Giga Sage

So the user record has a field domain, that contains a different value for domain?

You can use:

gs.getUser().getRecord().getValue('domain_field');

Replace domain_field with the field.

Namrata Khabale
Giga Guru

Hey Rj27,

Try this:

var domaine = gs.getUser().getDomainID();
var gruser = new GlideRecord('sys_user');
gruser.addQuery('sys_domain',domaine);
gruser.query();
while(gruser.next())
{
ans = gruser.getDisplayName();
return ans;
}

Mark it Correct or Helpful, if it works based on impact....!!!!

 

Best Regards,

Namrata.

Hi @Namrata Khabale ,

as mentioned in my question,

var domaine = gs.getUser().getDomainID(); 

is giving me the value from domain picker but i want it from the user table. 
I was looking for a solution where i do not have to gliderecord the user table

Bhagyashri Sort
Kilo Guru

Hi,

 If you want to show the domainId on form only then you can use the g_scratchpad, for that create an display BR and onload client script to display the domainId. 

And if you want to use that value in script include for any operation then you have to use the GlideRecord.

 

Mark it correct and helpful.

Thanks

Bhagyashri Sorte.