Missing function declaration for onBefore error on Business Rule

mitzaka
Mega Guru

Hi SNC,

I am trying to write a Business Rule but whatever I do I get this message: Missing function declaration for onBefore.

I am on Fuji, and have never seen this so far.


Would appreciate any tips on what I am doing wrong.

1 ACCEPTED SOLUTION

Harish KM
Kilo Patron
Kilo Patron

Hi..


  In fuji, write the code inside that function



function onBefore(current, previous) {


    //This function will be automatically called when this rule is processed.



// your code here


}




or



function onBefore(current, previous) {


    //This function will be automatically called when this rule is processed


}


// your code here



Thanks'


Harish


Regards
Harish

View solution in original post

19 REPLIES 19

Phew



Thanks for the answer


I have a similar issue.   But getting the error:   Missing function declaration for onAfter.



Would I add the following to the script?



function onAfter(current, previous) {


    //This function will be automatically called when this rule is processed


}


// your code here


Yup, you just need to have the corresponding function depending on the "When" value of the business rule.



Not every function has access to both "current" and "previous," though (e.g., "function onAsync(current)"). You could figure this out using the Wiki (or you could just start a brand new business rule and see what the system populates for you).



On a related note, I just realized that there's now an "Execute Function" field on business rules deciding whether or not these functions will be automatically run. (If you also get the crazy idea to try manually converting a pre-Fuji business rule to the new approach, make sure you set this field to true!)


Hey please give us code for examples


Well, this is interesting. We just upgraded one of our instances from Fuji Patch 12 Hotfix 1 to Helsinki Patch 1, and the implementation is slightly different.



In Fuji P12H1, the default Script you're provided with depends on the When selection you make. For example, "before" business rules start with:


function onBefore(current, previous) {
    //This function will be automatically called when this rule is processed.
   
}


In Helsinki P1, the default Script you're provided with is always the same:


(function executeRule(current, previous /*null when async*/) {



  // Add your code here



})(current, previous);



In any version, be mindful of what global variables are actually available to you.


Fuji and earlier: Scripting in Business Rules - ServiceNow Wiki


Helsinki (and I assume Geneva, as well): Using predefined global variables



P.S., continuing my "related note" from my last post, it looks like my personal revelation isn't really relevant anymore, hah. By moving the default script to a self-executing function, business rule scripts are able to run without dependencies on any other fields while still being safely enclosed in functions. Well done, ServiceNow developers!