Problem calling a Script Include from a Business Rule

Katie A
Mega Guru

I am making a scoped application in Fuji. I having a problem when I try to call a Script Include from a Business rule. I have confirmed that all of the artifacts such as the script include and business rule are all residing in the same application scope.

I should be able to call the script include as such:


var x = new MyScript();

x.myFunction();

The business rule:


function onBefore(current, previous){

var lu = new LoanerResUtils();

lu.isAvailable();

}

This is not working and I see the undefined error in the log.

The script include name is LoanerResUtils.

org.mozilla.javascript.EcmaError: The undefined value has no properties.

Caused by error in Business Rule: 'Loaner Res Item Availability' at line 20

==> 20: var lu = new LoanerResUtils();

21: lu.isAvailable();

I tried using the fully-qualified scope name but that did not work either.

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

Caused by error in Business Rule: 'Loaner Res Item Availability' at line 20

==> 20: var lu = new x_myscp_loaner_res_LoanerResUtils();

21: lu.isAvailable();

This exact syntax has worked for me in Eureka so I am not sure what could be the issue here.

Let me know if anyone has any possible solutions!

Here is the script include.

Screen Shot 2015-08-17 at 4.11.29 PM.png

1 ACCEPTED SOLUTION

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Kathryn,



Do you have an initialize method in your Script Include? You need one for the Prototype class-extension.



var LoanerResUtils = Class.create();


LoanerResUtils.prototype = {


      //THIS IS REQUIRED


      initialize: function() {


      },


     


      //ALL YOUR METHODS DEFINED LIKE THIS


      isAvailable : function() {


              //whatever logic


      },


     


      //THIS IS ALSO REQUIRED


      type: 'LoanerResUtils'


};



If you go the Class extension route (the default for Script Includes) you need to have an initialize method (even if it's empty) and a type declaration.


View solution in original post

14 REPLIES 14

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Kathryn,



You've marked the script Client callable, which means the script include will extend AbstractAjaxProcessor. It shouldn't make the script inaccessible, but it might make the script syntax kind of weird server-side, since your methods aren't called with parameters, instead trying to take them off the transaction.



What does your LoanerResUtils script include look like? It should start with either:


        var LoanerResUtils = Class.create();


        LoanerResUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {



Or


        var LoanerResUtils = Class.create();


        LoanerResUtils.prototype = {


                initialize: function() {



If you need to access it with the fully-qualified scope name, that would be


x_myscp_loaner_res.LoanerResUtils()


not


x_myscp_loaner_res_LoanerResUtils()



But that would only be needed if the Business Rule is in a different scope from the Script Include.


Hi Cory, The script include is structured as such:



var LoanerResUtils = Class.create();


LoanerResUtils.prototype = {


isAvailable : function(ci, start, end) {


// do stuff


  },


  type: 'LoanerResUtils'


}


I have also tried unenabling the Client Callable box and the same error occurs.


coryseering
ServiceNow Employee
ServiceNow Employee

OK, let's try something simple.



Elevate your permissions to security admin:


https://wiki.servicenow.com/index.php?title=High_Security_Settings#Elevated_Privilege



The navigate to Scripts - Background:


https://community.servicenow.com/people/ctomasi/blog/2012/09/29/2196



In the Script box, run this:



var lu = new LoanerResUtils();


gs.info("Type of LRU: " + typeof lu);


gs.info(lu.isAvailable);



Before clicking Submit, change the "in scope" drop-down to x_myscp_loaner_res:


Screen Shot 2015-08-17 at 2.29.35 PM.png


What does it output? In an instance where LoanerResUtils doesn't exist, you would see:


Evaluator: org.mozilla.javascript.EcmaError: "LoanerResUtils" is not defined.


  Caused by error in script at line 1



==> 1: var lu = new LoanerResUtils();


  2:


  3: gs.info("Type of LRU: " + typeof lu);


  4:



However, hopefully in your case you see the function definition for isAvailable.