Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to call a global script include from now-sdk?

anthonydian
Tera Contributor

 Hi now-sdk community,

 

I struggle to complete the following task: calling a global script include from my now-sdk scopped application.

 

When the following snippet runs well in a bg script (executed from my application scope): 

var test = new global.inboundAPILog().insertEventLog('x_frt_orangetmf697.inbound.api.event', null, "","");

The exact same script deployed through now-sdk will produce this error: "undefined is not a function".

Surprisingly, if I open the deployed sys_module in my browser, the global script include is well found, and I can navigate to it with control+click : 

anthonydian_0-1753796827515.png

I haven't found any resources on this specific topic, does this speak to you?

 

Thanks a lot for your feedback,

Regards,

Anthony



11 REPLIES 11

Hey @ChristianGraab,

To use a script include from a scope other than global, you can implement the following approach to ensure it functions correctly.

import { ScriptIncludeName } from '@servicenow/glide/<scope_name>'

export function someAPI() {
   var test = new ScriptIncludeName();
   var result = test.exposedFunction();
   //do something with the result
}

If it's a TypeScript file, the compiler may indicate that it can't find the module or its type definitions. To resolve this, you can add the necessary type definitions as outlined here: https://www.servicenow.com/docs/bundle/zurich-application-development/page/build/servicenow-sdk/con….

A limitation of modules is that they are only available within the application scope where they're added and can't be used across scopes. However, this may change in the future, allowing cross-scope module access. For now, you can work around this by exposing your modules through a script include that is accessible across scopes. Refer to this example-app for an example, particularly this section of code.



Thank you @Harish Kumar Sr , this works as intended! 
I will keep using script includes for now, and see where this whole thing is going.