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

phsdm
Giga Expert

Search for GlideAjax to learn more about AbstractAjaxProcessor.


Chuck Tomasi
Tera Patron

Hi,



You'll want to give your functions names because your GlideAjax call needs to send a sysparm_name to call it. See episodes 5, 6 and 33 about that in the TechNow series



TechNow Episode List



also helpful;



Docs: Client Scripts


Docs: GlideForm


Docs: GlideAjax


Client Script Best Practices - ServiceNow Wiki      


phsdm
Giga Expert

You can use a function as a Script Include.



Script includes



Note that you need to use GlideAjax and AbstractAjaxProcessor to invoke a script from the client side.


Chuck Tomasi
Tera Patron

Sorry, I missed the very last part about invoking the classless script include from the AbstractAjaxProcessor. yes, this is entirely possible. Put your server side logic in the functionless/classless script include and you can call it form the AbstractAjaxProcessor one.



sabell2012 did an Ask the Expert session on including a script include in a script include like this, but I cannot seem to find it at the moment.