Skip to the content.

1. Server-side Scripting

Server-side scripts execute on the ServiceNow server or database.

Some examples:

1.1 Business Rules

Business Rules are server-side logic that execute when database records are queried, updated, inserted, or deleted. Use business rules to automatically change values in form fields when the specified conditions are met.

To create a Business Rule: open System Application Studio -> Create Application File -> Type Business Rule in Filter.

Key: "interact with the databases".

When Business Rules Run

business-rules-process-flow
Process Flow of Business Rules

image

Example of Business Rule

1.2 Business Rule Actions

Using for set fields values, Add a message to the top of a form, Abort the Business Rule execution.

Notes: Client-side Script also can set fields values with GlideForm (g_form) and GlideUser (g_user).

1.3 Business Rule Scripts

Use the server-side API. Some actions:

image

Example of Business Rule Script

UPDATING……

GlideRecord

The GlideRecord class is the way to interact with the ServiceNow database from a script.

See more: GlideRecord API

NOTE: The GlideRecord API discussed here is a server-side API. 
There is a client-side GlideRecord API for global applications. 
The client-side GlideRecord API cannot be used in scoped applications.

GlideDateTime

GlideDateTime methods to perform date-time operations, such as instantiating a GlideDateTime object, performing date-time calculations, formatting a date-time, or converting between date-time formats.

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

    // rightnow stores the current time
    var rightnow = new GlideDateTime();
    // Create a GlideDateTime object for the When needed date
    var whenNeeded = new GlideDateTime(current.u_when_needed);
    // Challenge:  Do not allow same-day requests
    // Get the date portion of rightnow and whenNeeded (no timestamp)

    // If the When needed date is before rightnow, do not write the record to the database
    // Output an error message to the screen
    if (whenNeeded.before(rightnow)) {
        gs.addErrorMessage("When needed date cannot be in the past.  Your request has not been saved to the database.");
        current.setAbortAction(true);
    }
    var today = rightnow.getLocalDate();
    var istoday = whenNeeded.getLocalDate();
    // Compare today and istoday to see if they are the same day
    if (today.compareTo(istoday) == 0) {
        gs.addErrorMessage("You cannot submit NeedIt requests for today.");
        current.setAbortAction(true);
    }



})(current, previous);

Example of Bussiness Rule prevent users choose a date in the past and today.

Debugging Business Rules