- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
- It's probably not recommended.
- It's probably frowned upon.
- Heck, you might even be denied support if you do this.
The moral of the story above is simply, Be Careful!
It's critical that you pay close attention not to override existing GlideRecord functions or properties using the following approach, or you can break business rules running everywhere. I would recommend that you use a unique naming convention for your custom functions and properties, perhaps prefixing them with something like f_myFunctionName() or p_myPropertyName.
Step 1:
Temporarily disable the Client Script called "Don't allow code outside functions" which applies against the Business Rule [sys_script] table.
Step 2:
Create a new Global Business Rule called something like "GlideRecord Prototype Extensions". Click the Advanced checkbox and add your custom prototype extensions in the Script editor. This would include any custom properties or functions you would like to add to the GlideRecord object. For example:
GlideRecord.prototype.hello = 'Hello Community!';
GlideRecord.prototype.isMultipleOf = function (field, multiple) {
return parseInt(this[field]) % multiple == 0 ? true : false;
}
NOTE: When you save this custom global business rule you will receive an info message warning that the code exists outside of a function, which means it will run against every server side transaction.
Step 3:
Consume your newly created properties and functions in other server side code. For example, I created a new Advanced before Update Business Rule on the Incident table called "Test Custom GR Extensions" with the following script code:
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
// GET THE INPUT VALUE OF THE URGENCY FIELD
var multiple = parseInt(current.urgency);
gs.addInfoMessage(current.hello);
// CHECK IF THE IMPACT FIELD INTEGER VALUE IS A MULTIPLE OF THE URGENCY FIELD INPUT VALUE
// USE THE CUSTOM isMultipleOf() FUNCTION ON THE current(GlideRecord) OBJECT
gs.addInfoMessage('Is impact a multiple of ' + multiple + '?: ' + current.isMultipleOf('impact', multiple));
}
Result:
Why?:
When I think about tables, especially custom ones, I like to think of them like objects or more accurately classes that define what an instance of a table record "object" should look like. In the world of object oriented programming classes will typically consist of a combination of properties, which describes the state of the object at a given point in time, and functions which define behaviors that the object is capable of. In the world of Service Now, table fields become properties of the GlideRecord object when an instance of that object is created. I thought to myself, since we have the ability to create custom properties on the GlideRecord, wouldn't it be nice to have the ability to create custom functions as well. That is how this idea came to mind. The beauty of the approach above is that I've made the isMultipleOf function available to all GlideRecord objects created anywhere in the system because I modified the prototype in a Global business rule. If you wanted your custom functions/properties to only be available to a GlideRecord of a specific table, for example 'incident', you could always modify the prototype inside of a business rule that applies against the incident table specifically.
Disclaimer: I am not responsible if you break your system with this approach. Use at your own risk!
Michael Moody
Software Engineer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.