Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

I would like to display the current domain when a user opens a Catalog Item.

Kento Sanekata
Tera Contributor

Is there a way to retrieve and display the domain currently selected in the Domain Picker on a Catalog Item form? What I want to display is not the user's assigned domain, but the domain currently selected in the Domain Picker.

2 REPLIES 2

Hemanth M
Tera Sage

Hi @Kento Sanekata ;

 

Have you tried this ?- you can try this as default variable value or call from a script include through onload catalog client script

gs.getUser().getDomainID();

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025,2026

mohithreddy
Mega Contributor

The Domain Picker selection is stored on the user session, not on the user record, so gs.getUser().getDomainID() will not give you what you want. Read it from the session instead: gs.getSession().getCurrentDomainID() returns the sys_id of the domain currently selected in the picker.

 

Implementation on a Catalog Item: 1) Add a variable of type Single Line Text (or a Label/HTML variable) named current_domain, mark it Read only. 2) Create a client-callable Script Include, e.g. DomainUtilAjax extends AbstractAjaxProcessor, with: getCurrentDomain: function() { var id = gs.getSession().getCurrentDomainID(); var gr = new GlideRecord('domain'); if (id && gr.get(id)) return gr.getValue('name'); return 'global'; } and set Client callable = true. 3) Add an onLoad catalog client script on the item: function onLoad() { var ga = new GlideAjax('DomainUtilAjax'); ga.addParam('sysparm_name','getCurrentDomain'); ga.getXMLAnswer(function(answer){ g_form.setValue('current_domain', answer); }); }

 

Notes: query the domain table with a GlideRecord that is not domain-limited (the domain table itself is fine), and handle the global case where getCurrentDomainID() returns 'global' rather than a sys_id. If you need it on the record after submit, set the same value in a before-insert business rule on sc_req_item using gs.getSession().getCurrentDomainID(), because at submit time the session domain is still the picker selection.