gs.getUser().getDomainID() not working in scoped application scripts

aditya_snow1
Tera Contributor

Hi Guys,

I am trying to retrieve the current session's Domain using a server side scoped application   script.   Using the   'gs.getUser().getDomainID() '   function gives the following error-

'Cannot find function getDomainID in object com.glide.script.fencing.ScopedUser'

Is there any other way to retrieve the current domain using a scoped application script without explicitly calling a globally scoped script?

Any help would be highly appreciated.

Thanks,

Aditya

1 ACCEPTED SOLUTION

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Aditya,



The session/user getDomainID() methods are not exposed to scope currently. As a workaround, I'd suggest creating a Script Include with a method you can call that will create a domain-separated record and just read the domain off of it. Like this:



var DomainInfo = Class.create();


DomainInfo.prototype = {


      initialize: function() {


      },



      getCurrentDomain : function() {


              var throwAwayRecord = new GlideRecord("sys_user");


              throwAwayRecord.newRecord();



              return throwAwayRecord.getValue("sys_domain");



      },




      type: 'DomainInfo'


};









Then when you want to get the domain, you call your Script Include:


var currentDomain = new DomainInfo().getCurrentDomain();



This assumes that the sys_user table is domain-separated. I'd suggest using a domain-separated table which doesn't have Number Maintenance or other auto-incrementing stuff, since we are deliberately initializing a full record, but never saving it.



Thanks,


Cory Seering


View solution in original post

13 REPLIES 13

Hi Rajesh,



Do you know name of the Glide API that needs to be used in the 'target name' field for allowing access to these domain support functions?



Thanks,


Aditya


Rajesh Mushke
Mega Sage
Mega Sage

Hello Aditya,




Target NameThe name of the table, script include, or script object being requested


It may be your global   table or script include,   script object


Example : Table : incident, problem


                                Script include: your script include.




Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Aditya,



The session/user getDomainID() methods are not exposed to scope currently. As a workaround, I'd suggest creating a Script Include with a method you can call that will create a domain-separated record and just read the domain off of it. Like this:



var DomainInfo = Class.create();


DomainInfo.prototype = {


      initialize: function() {


      },



      getCurrentDomain : function() {


              var throwAwayRecord = new GlideRecord("sys_user");


              throwAwayRecord.newRecord();



              return throwAwayRecord.getValue("sys_domain");



      },




      type: 'DomainInfo'


};









Then when you want to get the domain, you call your Script Include:


var currentDomain = new DomainInfo().getCurrentDomain();



This assumes that the sys_user table is domain-separated. I'd suggest using a domain-separated table which doesn't have Number Maintenance or other auto-incrementing stuff, since we are deliberately initializing a full record, but never saving it.



Thanks,


Cory Seering


Hi   coryseering.



Initializing the record without saving it always returns the domain as global irrespective of the current session's domain.



As a work around, I created a custom domain separated table within my application scope, and used a scoped script include to create a record in the table, return the domain, and then subsequently delete the record.



Thanks a lot for your help, really appreciate it.



Regards,


Aditya