2

I understand that glideAjax can be used to call a business rule from client script. But wiki says below

Do not create a business rule, but instead navigate to System Definition > Script Include and create a new script

Can some one help me understand how does one call a business rule which is 'client callable' from a client script using glide ajax

Prasanna
  • 440
  • 4
  • 14

1 Answers1

2

I don't think you want a business rule. A business rule is something that is intended to run an arbitrary server-side script any time a record in a particular table is updated which meets a particular condition.

There exists the concept of a global business rule which allows you to basically define a function that will exist in the global scope, making it callable most anywhere server-side, but you should avoid global business rules. Just like in general programming best practices, you don't want to be defining things in the global scope/context. This is especially problematic with javascript in ServiceNow. Just avoid bleeding into the global scope and save yourself a world of hurt.

What you actually want is a client-callable Script Include, which is just a place for you to define a script-defined object, which you can call using GlideAjax.

The wiki on GlideAjax includes a pretty straightforward example on how to define a client-callable Script Include (which is server-side) that you can invoke from a client script (which is client-side).

Joey
  • 2,901
  • 21
  • 22
  • Hi joey i totally accept with what you say. But what difference a script include has from a business rule since script includes are also accessible globally? Forgive my ignorance :) – Prasanna Apr 22 '15 at 17:14
  • 2
    That's a fair question. The main best-practices benefit of a Script Include is that it's wrapped as an Object, so all your code and logic is contained, compared to a business rule which really just dumps your function in the global scope without any regard for what it might step on or get stepped on by. If you create function foo() {} and someone else creates function foo() {}, there's no guarantee which gets called, but if yours is MyScript.foo(), you're safe. – Joey Apr 22 '15 at 17:28