Script Include: Class Based vs Function Based

greggszul
Tera Expert

Hello Colleagues,

I'm relatively new to ServiceNow script includes and I have some questions about them. What I've noticed is that we can create script includes by extending class "AbstractAjaxProcessor" or as a stand alone javascript functions.

The first   method looks like this:

var myScriptInclude = Class.create();

myScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

        myFuncton: () {

                  var x = this.getParameter( "my_parameter" )

                  ...

                  return x * 2;

        }

        type: 'myScriptInclude '

}

On the client we can say:

var myVar = new GlideAjax( "myScriptInclude" );

myVar.addParam( "my_parameter", "PARAMETER" );

myVar.getXML( callback ) // Here we can specify the callback function, which will do something with the asnwer.

The next option is:

function myScriptInclude ( arg ) {

        ...

        return arg * 2;

}

In the workflow activity we can use it by

gs.include( "myScriptInclude " );

with the function we can build our custom reference qualifier for example

javascript:myScriptInclude("some_argument");

This one is pretty powerfull for building custom filters with some advanced logic.

However I don't know if it is possible to call such script (function based) on the client.

So my question is how those two differs (how ServiceNow perceives them) ? Where can I find something about AbstractAjaxProcessor ? And how can I invoke function based script include on the client side (for example in the catalog form) ?

Thank you.

5 REPLIES 5

Hey there,   the session that Chuck mentioned should be here:



Ask the Expert: Scoped Libraries with Steve Bell



Also, this morning I published an article that has an example of a Scoped Function Library.



Mini-Lab: Converting a UI Script Library From Global to Scoped - Part 2



Hope that helps,



Steven.