depts
Overview
This is an object that performs operations related to "Depts" that can be used in "Server Script".
Properties
There are no properties.
Methods
Usage Examples
The following example gets information about the depts to which the logged-in user belongs.
| let myDept = depts.Get(context.DeptId);
if (myDept) {
context.Log(`DeptName: ${myDept.DeptName}`);
}
|
Code Samples
1. Controlling button display with combined conditions
A sample script that judges dept and group membership in combination and controls whether buttons are displayed.
Functional Requirements
-
The display of the buttons is controlled under the following conditions.
| Division (Dept) | Position (Group) | Approve | Update |
|---|
| Sales Department | Department Manager | Shown | Shown |
| Section Manager | Hidden | Shown |
| General Employee | Hidden | Hidden |
| Development Department | Department Manager | Hidden | Shown |
| Section Manager | Hidden | Shown |
| General Employee | Hidden | Hidden |
-
The divisions and positions are defined as follows.
| Type | ID | Dept/Group |
| Sales Department | 6 | Dept |
| Development Department | 7 | Dept |
| Department Manager | 3 | Group |
| Section Manager | 2 | Group |
General employees are assumed to belong to no group.
-
Button control
- Approve: set with "Processes"
- Update: the standard "Update" button
These are controlled with elements.DisplayType.
| Condition: Before opening the page |
|---|
| // ===== Settings =====
const deptSales = 6; // Sales Department
const deptDev = 7; // Development Department
const groupManager = 3; // Department Manager
const groupSecManager = 2; // Section Manager
// Display condition for each button:
// Shown when the user belongs to any of "depts" AND any of "groups".
// An empty array [] means that axis is not checked (everyone passes).
const buttonRules = [
{
id: 'Process_1',
depts: [deptSales],
groups: [groupManager]
}, // Approve: Sales Department x Department Manager
{
id: 'UpdateCommand',
depts: [deptSales, deptDev],
groups: [groupManager, groupSecManager],
}, // Update: Sales/Development x Department Manager/Section Manager
];
// ===== Engine: normally left untouched =====
const SHOW = 0; // DisplayType 0: Standard (shown)
const HIDE = 1; // DisplayType 1: None (hidden)
function isUserInDept(deptId, userId) {
const dept = depts.Get(deptId);
if (!dept) return false;
return Array.from(dept.GetMembers()).some(function (m) {
return m.UserId === userId;
});
}
function isUserInGroup(groupId, userId) {
const group = groups.Get(groupId);
if (!group) return false;
return group.ContainsUser(userId);
}
// Judge one axis (division or position): true unconditionally when not specified (empty array), otherwise true when any of them matches
function matchesAny(ids, isMemberOf) {
return ids.length === 0 || ids.some(isMemberOf);
}
const userId = context.UserId;
buttonRules.forEach(function (rule) {
const visible =
matchesAny(rule.depts, function (id) {
return isUserInDept(id, userId);
}) &&
matchesAny(rule.groups, function (id) {
return isUserInGroup(id, userId);
});
elements.DisplayType(rule.id, visible ? SHOW : HIDE);
});
|