Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

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

Thank you again Cory you are an excellent resource!


rldejesusm
Mega Guru

So what was the problem?


Hi Ramon,



Sorry, I don't remember. Maybe Kathryn still has the Update Versions for that include and can tell you.



It was   long time ago, so I wouldn't count on it.


In my case it was a problem with the function's scope.


(functionName() couldn't be called from my business rule.)



I used the full path of the script include function in my business rule (x_snc_scope).functionName() and it called it.



What's weird is that I'm doing everything in studio on the same custom app, and on the same scope.



Oh well, thank you!



#Instanbul #SNOW


We ended up rebuilding in a standard non-scoped application. We were running into too many issues with scoped apps. Next time if we decide to switch back to building scoped apps I will look into your solution for this particular issue. Thanks for letting us know!