The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Access current object in script include

Kim Kronborg
Kilo Expert

Hi,

Can somebody tell me what I'm doing wrong when I try to access the current object in my script include below.

I have a reference qual on a field that looks like this :

        u_my_shopLIKEjavascript:GetShopsFromGroups(getMyGroups())[0];^EQ

and my script include looks like this :

function GetShopsFromGroups(groups) {

gs.log('Shop number = ' + current.u_shop_number);

}

but I get the following error :

org.mozilla.javascript.EcmaError: "current" is not defined.

Caused by error in Script Include: 'GetShopsFromGroups' at line 3

1: function GetShopsFromGroups(groups) {

2:

==> 3: gs.log('Shop number = ' + current.u_shop_number);

4:

5: }

Should I not be able to access the current object from my script include when I call the script include from a ref qual ?

According to this community article it should be possible : https://community.servicenow.com/thread/207784

/Kim

7 REPLIES 7

Chuck Tomasi
ServiceNow Employee
ServiceNow Employee

Hi Kim,



Accessing 'current' is a bad idea in script includes. The idea of a script include function/method is that it can be called from anywhere in server side code (business rule, UI action, background script, etc.) Not all of these offer the 'current' object. What's more, many of those are now encapsulated in functions so current's scope is private.



A better idea is to pass current in and reference it as a GlideRecord object inside of your script.



e.g.: u_my_shopLIKEjavascript:GetShopsFromGroups(current, getMyGroups())[0];^EQ



and your script include's API changes slightly:



function GetShopsFromGroups(gRec, groups) {



gs.log('Shop number = ' + gRec.u_shop_number);



}


Hi Chuck,



I tried your suggestion but still get the error that current is not defined.



org.mozilla.javascript.EcmaError: "current" is not defined.


Caused by error in <refname> at line 1



==> 1: GetShopsFromGroups(getMyGroups(), current)[0];



How can I get access to the current object in a ref qual ?


Try this instead for your ref qual:



javascript:'u_my_shopLIKE' + GetShopsFromGroups(current, getMyGroups())


Hi Chuck,



Still the same - no access to current object.



Another approach - can I somehow access current session data from the script include so I can check on that instead ? Then I could set these session data in a before query business rule ?



/Kim