Skip to content

context

Overview

The "Server Script" is used to reference user request information such as user IDs and site IDs. It is also used for data sharing between scripts, logging, and message output.

Properties

No Name Get Set Type Description
1 UserData Yes Yes ExpandoObject Allow data sharing between scripts
2 QueryStrings Yes No Object Get the value of URL query parameters
3 Forms Yes No Forms Get form information
4 FormStringRaw Yes No string Information entered in the form (column)
5 FormString Yes No string Information entered in the form (column)
6 Ajax Yes No bool Flag indicating whether the request was made via Ajax
7 Mobile Yes No bool Flag indicating whether the request was made via Mobile
8 ApplicationPath Yes No string Application path
9 AbsoluteUri Yes No string Absolute URI
10 AbsolutePath Yes No string Absolute path
11 Url Yes No string URL
12 UrlReferrer Yes No string Previously requested URL
13 Controller Yes No string Controller name
14 Query Yes No string URL query parameters
15 Action Yes No string Action name
16 TenantId Yes No int Tenant ID
17 SiteId Yes No long Site ID
18 Id Yes No long Record ID
19 Groups Yes No IEnumerable Collection of group IDs of groups to which the user belongs
20 TenantTitle Yes No string Tenant title
21 SiteTitle Yes No string Site title
22 RecordTitle Yes No string Record title
23 DeptId Yes No int Department ID
24 UserId Yes No int User ID
25 LoginId Yes No string Log ID
26 Language Yes No string Setting language
27 TimeZoneInfo Yes No string Time zone
28 HasPrivilege Yes No bool Flag indicating whether the operating user is a privileged user
29 ApiVersion Yes No decimal API version
30 ApiRequestBody Yes No string API request content
31 RequestDataString Yes No string Request data
32 ContentType Yes No string Response header type
33 ControlId Yes No string Request source control ID
34 Condition Yes No string Name of server script condition

Methods

No Name Description
1 AddMessage Output a message at the bottom of the browser screen.
2 Error Cancel the user's create, update, or delete operation and outputs an error message.
3 Log Output a log to the browser console.
4 Redirect Cause the browser to transition to a new page.
5 AddResponse Return an arbitrary client response.
6 ResponseSet Store information in a form.

Usage Example 1

In the example below, if the user ID of the logged-in user is not 1, the filter will display only records whose creator is own. The condition is checked "When Processing Views."

JavaScript
if (context.UserId !== 1) {
    view.Filters.Creator = context.UserId;
}

Usage Example 2

The following example outputs to the log the group IDs of all groups to which the logged-in user belongs.

JavaScript
for (let groupId of context.Groups){
    context.Log(groupId);
}

Code Samples

Notes

Modify 【 ... 】 in the code as necessary.

1. Run a server script when an arbitrary process is executed

Use context.ControlId to determine which process was executed, and run an arbitrary server script process.

function itemsUpsert() {
    // Specify the site name
    const siteName = '【Site name】';
    // Get the site information
    const site = items.GetClosestSite(siteName);
    if (!site) {
        logs.LogInfo(`${siteName} Failed to get the site information`);
        return false;
    }
    // "Foreign key" for one-to-one synchronization
    // * Save the source record (ResultId) in ClassB of the destination table, and specify it in Keys of Upsert
    const sourceId = model.ResultId;
    // Generate the request
    const data = {
        Keys: ['ClassB'], // Foreign key column (this value determines update or create)
        Title: model.Title,
        Status: model.Status,
        ClassHash: {
            ClassA: model.ClassA,
            ClassB: sourceId, // Foreign key (source ID)
        },
    };
    // Create/update the record
    const result = items.Upsert(site.SiteId, JSON.stringify(data));
    if (result) {
        logs.LogInfo(`Upsert succeeded (foreign key=${sourceId})`);
    } else {
        logs.LogUserError(`Upsert failed (foreign key=${sourceId})`);
    }
}
logs.LogInfo(`ControlId=${context.ControlId}`);
switch (context.ControlId) {
    // Determine the executed process by ControlId
    case 'Process_1':
        // Process for process ID: 1
        itemsUpsert();
        break;
    default:
        // Do nothing
        break;
}
Execution Result
Execution Result
(Info):ControlId=Process_1
(Info):Upsert succeeded (foreign key=9999)
2. Control screen items according to the attributes of the logged-in user

Control the screen items (with columns.ReadOnly,Hide) according to the attributes of the logged-in user (determined with context.HasPrivilege and context.Groups) and the status. The sample code implements the following specifications.

Determining the control pattern

User type Status Control pattern
Privileged user - Pattern A
User who belongs to an administrator group (ID 1, 2) - Pattern B
Other users Completed (900) or On hold (910) Pattern C
Other users Other than the above Pattern D

Controlling screen items

Screen item Column ID Pattern A Pattern B Pattern C Pattern D
Title Title Editable Editable Read only Editable
Body Body Editable Editable Read only Editable
Status Status Editable Editable Read only Editable
Classification A ClassA Editable Editable Read only Editable
Classification B ClassB Editable Read only Hidden Hidden
Date A DateA Editable Read only Read only Editable
Description A DescriptionA Editable Editable Hidden Hidden

JavaScript

Condition: Before opening the page

// Settings
const ADMIN_GROUP_IDS = new Set([1, 2]);
const STATUS_PATTERN_C = new Set([900, 910]); // Completed/On hold
const PATTERN_RULES = {
    // Pattern A: Do nothing (all editable)
    A: [],
    // Pattern B: Make ClassB and DateA read only
    B: [
        ['ClassB', 'ReadOnly'],
        ['DateA', 'ReadOnly'],
    ],
    // Pattern C: Make Title/Body/Status/ClassA/DateA read only, and hide ClassB/DescriptionA
    C: [
        ['Title', 'ReadOnly'],
        ['Body', 'ReadOnly'],
        ['Status', 'ReadOnly'],
        ['ClassA', 'ReadOnly'],
        ['DateA', 'ReadOnly'],
        ['ClassB', 'Hide'],
        ['DescriptionA', 'Hide'],
    ],
    // Pattern D: Hide ClassB and DescriptionA
    D: [
        ['ClassB', 'Hide'],
        ['DescriptionA', 'Hide'],
    ],
};
const DEBUG = true;
// Determination (business rules)
function decidePattern() {
    if (context.HasPrivilege) return 'A';
    // context.Groups is an Iterable, so convert it to a Set to keep the check simple
    const userGroupIds = new Set(context.Groups);
    const isAdminGroupUser = [...ADMIN_GROUP_IDS].some((id) =>
        userGroupIds.has(id)
    );
    if (isAdminGroupUser) return 'B';
    // Other users
    return STATUS_PATTERN_C.has(model.Status) ? 'C' : 'D';
}
// Application (UI control)
function applyPattern(pattern) {
    const rules = PATTERN_RULES[pattern] ?? [];
    for (const [columnName, control] of rules) {
        // As a sample: skip without failing even if the column does not exist
        if (!columns[columnName]) continue;
        columns[columnName][control] = true;
    }
}
// Run
const pattern = decidePattern();
if (DEBUG) context.Log(`pattern=${pattern}`);
applyPattern(pattern);
Execution Result
Execution Result
(Info):pattern=C

Supported Versions

Supported versions Body
1.4.17.0 and later Added the context.ReferenceType property to server scripts
1.4.17.0 and later Added the context.HttpMethod property to server scripts