Skip to content

notifications.New

Overview

Create a notification object. Notification settings can be added to the generated object and sent anytime.

Syntax

notifications.New()

Return Value

Return the notification object.

Usage Examples

The following example sends a notification to xxxxx@example.com with the subject "Notification Test" and the content "Notified by server script".

JavaScript
let notification = notifications.New();
notification.Address = 'xxxxx@example.com';
notification.Title = 'Notification Test';
notification.Body = 'Notified by server script.';
notification.Send();

Code Samples

1. Send dynamic email notifications according to the status and content
Overview

This is a sample code that dynamically builds the subject and body according to the state (status) and content of a purchase request, and sends email notifications to the target people. The notification is assumed to be triggered from a process registered as desired.

Fields used

The following fields are used.

Field ctx property Content
model.Title title Project name
model.ClassA applicant Applicant (converted from the user ID to the name)
model.ClassD creatorMail Applicant's email address
model.ClassB - Approver
model.ClassE approverAddr Approver's email address
model.ClassC - Purchasing staff
model.ClassF purchaseMgr Purchasing staff's email address
model.NumA amount Requested amount
model.DateA deadline Desired delivery date (converted to yyyy/mm/dd format)
model.DateB approved_at Approval date and time (converted to yyyy/mm/dd format)
model.Status status Status (see the table below)
model.CheckA isUrgent Urgent flag
model.CheckB isReapply Reapplication flag
model.DescriptionA comment Comment
Meaning of the status values
Value Meaning
100 Waiting for approval
200 Approved
300 Sent back
900 Waiting for delivery confirmation
Conditions for editing the email text and recipients

The subject, header, body, footer, and recipients are edited under the following conditions. The email body consists of header + body + footer.

  1. Subject
Condition Subject
Urgent flag ON [Urgent] Request for approval of purchase request:
Requested amount 500,000 yen or more [High-value] Request for approval of purchase request:
Otherwise [Approval request] Purchase request:

If more than one condition is met, the first match from the top is used.

  1. Header
Condition Header text
Urgent flag ON A purchase request that requires urgent action has been submitted. Please check it promptly and approve or send it back.
Requested amount 500,000 yen or more A high-value purchase request has been submitted. Please check it carefully and approve or send it back.
Reapplication flag ON A request that was previously sent back has been corrected and resubmitted. Please check the changes and take action.
Otherwise A new purchase request has been submitted. Please check the content and take action.

If more than one condition is met, the first match from the top is used.

  1. Body
Condition (status) Body content
100: Waiting for approval Request details (project name, applicant, requested amount, desired delivery date)
200: Approved Approval result (project name, requested amount, approval date and time)
300: Sent back Send-back notice (project name, requested amount, prompt to see the comment)
900: Delivery confirmation Request for delivery confirmation (project name, requested amount, delivery date)

If the comment field has a value, a "■ Comment" section is added at the end of the body.

  1. Footer
Send to Footer text
Approver Please log in to the system to approve. If no action is taken by the deadline, an automatic reminder will be sent.
Applicant Please check or correct the request in the system. If you have any questions, please contact the purchasing staff.
Purchasing staff Please place the order from the purchase management screen of the system. Please be careful of the deadline.
  1. Recipients
Send to Sending condition
Approver (ClassE) The status is 100 (Waiting for approval) or 300 (Sent back), and the address is set
Applicant (ClassD) The status is 200 (Approved) or 300 (Sent back), and the address is set
Purchasing staff (ClassF) The status is 200 (Approved) or 900 (Delivery confirmation), and the address is set

If more than one recipient meets the conditions, a separate email is sent to each of them (with status 200, it is sent to both the applicant and the purchasing staff).

Image of the email sent
**Subject**

[Approval request] Purchase request: Purchase of monitors (10 units for the Development Dept.)

**Content**

A new purchase request has been submitted.
Please check the content and take action.

■ Request details
  Project name    : Purchase of monitors (10 units for the Development Dept.)
  Applicant       : Ichiro Takahashi
  Requested amount: 480,000 yen
  Desired delivery: 2026/04/15

Please log in to the system to approve.
If no action is taken by the deadline, an automatic reminder will be sent.
JavaScript
// ----------------------------------------------------------------
// [Definitions] Text of the subject, header, body content, and footer
// ----------------------------------------------------------------
const SUBJECT_DEF = {
    urgent: (vars) => `[Urgent] Request for approval of purchase request: ${vars.title}`,
    high_amount: (vars) => `[High-value] Request for approval of purchase request: ${vars.title}`,
    normal: (vars) => `[Approval request] Purchase request: ${vars.title}`,
};
const HEADER_DEF = {
    urgent:
        'A purchase request that requires urgent action has been submitted.\n' +
        'Please check it promptly and approve or send it back.\n',
    high_amount:
        'A high-value purchase request has been submitted.\n' +
        'Please check it carefully and approve or send it back.\n',
    reapply:
        'A request that was previously sent back has been corrected and resubmitted.\n' +
        'Please check the changes and take action.\n',
    normal:
        'A new purchase request has been submitted.\n' +
        'Please check the content and take action.\n',
};
const BODY_DEF = {
    waiting: (vars) =>
        '■ Request details\n' +
        `  Project name    : ${vars.title}\n` +
        `  Applicant       : ${vars.applicant}\n` +
        `  Requested amount: ${vars.amount} yen\n` +
        `  Desired delivery: ${vars.deadline}\n`,
    approved: (vars) =>
        '■ Approval result\n' +
        '  Status          : Approved\n' +
        `  Project name    : ${vars.title}\n` +
        `  Requested amount: ${vars.amount} yen\n` +
        `  Approved at     : ${vars.approved_at}\n`,
    rejected: (vars) =>
        '■ Send-back notice\n' +
        `  Project name    : ${vars.title}\n` +
        `  Requested amount: ${vars.amount} yen\n` +
        '  For the reason for sending back and the correction request, please see the comment below.\n',
    delivery: (vars) =>
        '■ Request for delivery confirmation\n' +
        `  Project name    : ${vars.title}\n` +
        `  Requested amount: ${vars.amount} yen\n` +
        `  Delivery date   : ${vars.deadline}\n` +
        '  * After confirming the delivery, please complete the acceptance inspection promptly.\n',
};
const FOOTER_DEF = {
    approver:
        '\nPlease log in to the system to approve.\n' +
        'If no action is taken by the deadline, an automatic reminder will be sent.\n',
    applicant:
        '\nPlease check or correct the request in the system.\n' +
        'If you have any questions, please contact the purchasing staff.\n',
    purchase_mgr:
        '\nPlease place the order from the purchase management screen of the system.\n' +
        'Please be careful of the deadline.\n',
};
// ----------------------------------------------------------------
// [Definitions] Key decision rules (RULE_DEF)
// ----------------------------------------------------------------
const RULE_DEF = {
    // Decision rules for the subject (3 patterns)
    subject: {
        fallback: 'normal',
        rules: [
            {
                key: 'urgent',
                when: (ctx) => ctx.isUrgent,
            },
            {
                key: 'high_amount',
                when: (ctx) => ctx.amount >= 500000,
            },
        ],
    },
    // Decision rules for the header (4 patterns)
    header: {
        fallback: 'normal',
        rules: [
            {
                key: 'urgent',
                when: (ctx) => ctx.isUrgent,
            },
            {
                key: 'high_amount',
                when: (ctx) => ctx.amount >= 500000,
            },
            {
                key: 'reapply',
                when: (ctx) => ctx.isReapply,
            },
        ],
    },
    // Decision rules for the body content (4 patterns)
    body: {
        fallback: 'waiting',
        rules: [
            {
                key: 'waiting',
                when: (ctx) => ctx.status === 100,
            },
            {
                key: 'approved',
                when: (ctx) => ctx.status === 200,
            },
            {
                key: 'rejected',
                when: (ctx) => ctx.status === 300,
            },
            {
                key: 'delivery',
                when: (ctx) => ctx.status === 900,
            },
        ],
    },
    // Footer and sending condition for each recipient (3 patterns)
    recipients: [
        {
            footerKey: 'approver',
            address: (ctx) => ctx.approverAddr,
            sendWhen: (ctx) =>
                (ctx.status === 100 || ctx.status === 300) &&
                !!ctx.approverAddr,
        },
        {
            footerKey: 'applicant',
            address: (ctx) => ctx.creatorMail,
            sendWhen: (ctx) =>
                (ctx.status === 200 || ctx.status === 300) && !!ctx.creatorMail,
        },
        {
            footerKey: 'purchase_mgr',
            address: (ctx) => ctx.purchaseMgr,
            sendWhen: (ctx) =>
                (ctx.status === 200 || ctx.status === 900) && !!ctx.purchaseMgr,
        },
    ],
};
// Utilities
function toYmd(d) {
    const y = d.getFullYear();
    const m = String(d.getMonth() + 1).padStart(2, '0');
    const day = String(d.getDate()).padStart(2, '0');
    return `${y}/${m}/${day}`;
}
function getUserName(userId) {
    const user = users.Get(userId);
    return user.Name;
}
// ================================================================
// General-purpose engine: scans the rule definitions to decide the key
// ================================================================
function resolveKey(ruleDef, ctx) {
    for (const rule of ruleDef.rules) {
        if (rule.when(ctx)) {
            return rule.key;
        }
    }
    return ruleDef.fallback;
}
// ================================================================
// Main process
// ================================================================
function sendMailMain() {
    // --- Collect the context (record values) ---
    const ctx = {
        title: model.Title,
        applicant: getUserName(model.ClassA),
        approverAddr: model.ClassE,
        purchaseMgr: model.ClassF,
        amount: model.NumA,
        deadline: toYmd(model.DateA),
        approved_at: toYmd(model.DateB),
        status: model.Status,
        isUrgent: model.CheckA,
        isReapply: model.CheckB,
        comment: model.DescriptionA,
        creatorMail: model.ClassD,
    };
    // --- Value map for placeholders ---
    const vars = {
        title: ctx.title,
        applicant: ctx.applicant,
        amount: Number(ctx.amount).toLocaleString('ja-JP'),
        deadline: ctx.deadline,
        approved_at: ctx.approved_at,
    };
    // --- Decide the keys with the rule engine ---
    const subjectKey = resolveKey(RULE_DEF.subject, ctx);
    const headerKey = resolveKey(RULE_DEF.header, ctx);
    const bodyKey = resolveKey(RULE_DEF.body, ctx);
    // --- Generate the subject ---
    const subject = SUBJECT_DEF[subjectKey](vars);
    logs.LogInfo(`Generated subject: ${subject}`, 'MainProcess');
    // --- Function that builds the body (switches the footer key by argument) ---
    function buildBody(footerKey) {
        let body = HEADER_DEF[headerKey] + '\n' + BODY_DEF[bodyKey](vars);
        if (ctx.comment && ctx.comment.trim() !== '') {
            body += '\n■ Comment\n  ' + ctx.comment + '\n';
        }
        body += FOOTER_DEF[footerKey];
        return body;
    }
    // --- Loop through the recipient rules and send notifications ---
    for (const r of RULE_DEF.recipients) {
        if (!r.sendWhen(ctx)) continue;
        const n = notifications.New();
        n.Address = r.address(ctx);
        n.Title = subject;
        n.Body = buildBody(r.footerKey);
        n.Send();
    }
}
switch (context.ControlId) {
    // Determine the executed process by ControlId
    case 'Process_1':
        // Process for process ID 1
        sendMailMain();
        break;
    default:
        // Do nothing
        break;
}

Cautions

This is a method used in "Server Script". It cannot be used in "Script".

Supported Versions

Supported versions Body
1.4.10.0 and later Added the CcAddress and BccAddress properties to the notification object