Server-side script use cases
-
- UpdatedFeb 4, 2025
- 13 minutes to read
- Yokohama
- Building applications
Use cases for server-side scripts include logging output, getting user objects, and modifying date/time values.
Accessing the workflow scratchpad from business rules
A catalog item has been requested, and the attached workflow contains a run script activity that populates a value in the scratchpad. From a business rule running on the requested item, you want to retrieve or set scratchpad values.
Prerequisites
Role required: admin.
Name: Access Workflow Scratchpad from Business Rules.
Type: Business Rule.
Table: sc_req_item (Requested Item).
Description: A catalog item has been requested, the attached workflow contains a run script activity that populates a value in the scratchpad. From a business rule running on the requested item, you want to retrieve or set scratchpad values.
Parameters: n/a.
Assign a catalog item to a group based on a delivery plan task
Assign a service catalog item to the database group if it uses a delivery plan that has a catalog task that is assigned to the desktop group.
Prerequisites
Role required: admin
Name: Assign Catalog Item to Group Based on Delivery Plan Task.
Type: Assignment Rule.
Description: This assignment rule assigns a service catalog item to the database group if it uses a delivery plan that has a catalog task assigned to the desktop group.
Script:
Calculating durations
Often you may need to provide users with a way to specify when a task or process is due. Using the DurationCalculator script include, you can calculate the due date using either a simple duration or relative duration.
For information on schedules, which you can use as inputs to DurationCalculator methods, see Creating and using schedules.
This script demonstrates how to use DurationCalculator to compute a due date.
Simple duration vs relative duration
How much work is required to complete a task can be expressed as a "relative duration".
Relative duration determines the expected due date and time relative to the starting time. Examples of relative durations include "Next business day by 4pm", or "2 business days by 10:30am".
- If it is Monday at 12pm: Next business day by 4pm => Tuesday at 4pm
- If it is Friday at 2pm: Next business day by 4pm => the following Monday at 4pm
For more information on relative durations, see Define a relative duration.
Calculating a simple duration
This business rule and script example demonstrate how to calculate a simple duration.
This script demonstrates how to use DurationCalculator to calculate a simple duration.
Calculating a relative duration
An example of a relative duration calculation script.
This script demonstrates how to use DurationCalculator to calculate a relative duration.
How to implement a relative duration
You can implement a relative duration by creating the cmn_relative_duration table and the DurationCalculator script include.
Before you begin
Procedure
- Create the cmn_relative_duration table.
- Create the DurationCalculator script include.
- Create a sample relative duration entry (for example, "Next business day by 4pm").
- Add the needed fields to SLA tables to support relative durations.
- Modify duration calculation for SLAs.
- Modify SLA Percentage timer calculation for SLAs (this must use work_seconds).
- Add schedule fields to the Workflow: Schedule and Timezone (selected based on the field from workflow table).
- Add duration support fields to the Workflow Task activity.
- Implement duration calculation script for the task activity.
The relative duration table and the DurationCalculator methods
The cmn_relative_duration table supports the definition of a due date as either a duration of time or a relative duration.
This table consists of two fields: "name" and "script." The "script" field contains the relative duration calculation script. This script includes the "calculator" variable, which is used to calculate the due date.
The DurationCalculator script include can be used to perform the duration calculations. The following are methods that are available in this script include.
The following functions are used in relative duration scripts:
Function | Description |
---|---|
boolean isAfter(GlideDateTime dt, String time) | Is 'time' of day after the time of day specified by 'dt'? dt, if blank, uses current date/time. time is in "hh:mm:ss" in 24-hour format. |
calcRelativeDueDate(GlideDateTime start, int days, String endTime) | Calculates the due date starting at 'start' and adding 'days' using the schedule and time zone. When we find the day that the work is due on, set the time to 'endTime' of that day. Upon completion, this.endDateTime and this.seconds properties will be set to indicate the results of the calculation. If endTime is blank, use end of the ending work day. |
Get a user object
In a business rule or other server script, the gs.getUser() method returns a user object. The user object is an internal representation of the currently logged in user and provides information about the user and various utility functions.
About this task
Procedure
Log output
GSLog is a script include that simplifies script logging and debugging by implementing levels of log output, selectable by per-caller identified sys_properties values.
Log level
Logs can be at the level of debug, info, notice, warning, err, or crit (after BSD syslog.h and followers). The default logging level is notice, so levels should be chosen accordingly.
Where to use
Use for any server-side script where you want to implement event logging.
For the API reference, see GSLog().
For more information, see Debugging scripts
Modify a GlideDateTime field value
This example demonstrates how to modify a GlideDateTime field value using a server-side script.
Using custom queues to process events
You can use custom queues for applications that create a large volume of events or events that take a long time to process. This task shows how to create a custom queue, its monitoring process, and use a script to send events to the queue.
Before you begin
Role required: admin
Procedure