Call user domain in a catalog client script

Lon Landry2
Tera Contributor

I am trying to write a catalog item client script displaying a message with the users domain.

I have tried many different script ideas over 10 hours but nothing I have tried works with Catalog client scripts.

How can I Call user domain in a catalog client script?

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

i would suggest here, use display business rule and then you can call that variable in your onLoad() client script to get the value.

 

or if you want in onChange() client script , use glide ajax.

 

Sample Script:

 

Display Business rule:

 

var myUser = gs.getUser();
g_scratchpad.domain_id = myUser.getDomainID();
g_scratchpad.domain_display_value = myUser.getDomainDisplayValue();

 OnLoad Client SCript:

alert(g_scratchpad.domain_id);

 

Another Approach: Using Glide Ajax.

 

Client Script: onChange()

 

var ga = new GlideAjax('HelloWorld');
ga.addParam('sysparm_name', 'helloWorld');
ga.getXML(HelloWorldParse);
 
function HelloWorldParse(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer); 
}

 

Script Include( make sure you mark "Client Callable" true.

 

var HelloWorld = Class.create();
HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   helloWorld:function() { 

var myUser = gs.getUser();

var userDom = myUser.getDomainID();  // if you want id then you can pass "userDom" variable in return.

var userDomValue= myUser.getDomainDisplayValue();

return userDomValue ; 
},
 });

 

 

View solution in original post

8 REPLIES 8

Mike Patel
Tera Sage

you can do something like below. It's not recommended to do gliderecord query in client script.

Ref-https://www.servicenowguru.com/scripting/user-object-cheat-sheet/

//This script gets the user's title
var gr = new GlideRecord('sys_user');
gr.get(g_user.userID);
var domain = gr.domain;
alert(domain);

If it is not recommended, then I cannot use it.

It is helpful to know it is not recommended; as I have already tried a few scripts using GlideRecord.

Harsh Vardhan
Giga Patron

i would suggest here, use display business rule and then you can call that variable in your onLoad() client script to get the value.

 

or if you want in onChange() client script , use glide ajax.

 

Sample Script:

 

Display Business rule:

 

var myUser = gs.getUser();
g_scratchpad.domain_id = myUser.getDomainID();
g_scratchpad.domain_display_value = myUser.getDomainDisplayValue();

 OnLoad Client SCript:

alert(g_scratchpad.domain_id);

 

Another Approach: Using Glide Ajax.

 

Client Script: onChange()

 

var ga = new GlideAjax('HelloWorld');
ga.addParam('sysparm_name', 'helloWorld');
ga.getXML(HelloWorldParse);
 
function HelloWorldParse(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer); 
}

 

Script Include( make sure you mark "Client Callable" true.

 

var HelloWorld = Class.create();
HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   helloWorld:function() { 

var myUser = gs.getUser();

var userDom = myUser.getDomainID();  // if you want id then you can pass "userDom" variable in return.

var userDomValue= myUser.getDomainDisplayValue();

return userDomValue ; 
},
 });

 

 

I started down this path with a Business Rule but ran into issues in my personal instance with scoped app.

I am trying this now in a dev client with global app now.

Thanks for your help and time,

 

I look forward to marking your answer as correct...