Skip to content

apiModel.Create

Overview

Create a record in the server script with the contents of the apiModel object.

Limitations

  1. The ID of a created record cannot be retrieved from the return value of apiModel.Create. If you write a script as shown in the "Usage Example" below, you can retrieve the ID of the created record.

Syntax

apiModel.Create(siteId)

Parameters

Parameter Type Required Description
siteId object Yes Specify the site ID of the target table

Return Value

If the record is created, true is returned; if not, false is returned.
The contents of the created record, including the record ID, are reflected in the apiModel.

Usage Examples

The following example creates a record in a time-limited table for site ID 123 with a title of "About Pleasanter Upgrade Procedure" and outputs the record ID that was created.

JavaScript
// Script for time-limited table
let sampleApiModel = items.NewIssue();
sampleApiModel.Title = 'About Pleasanter Upgrade Procedure';
sampleApiModel.Create(123);
context.Log(sampleApiModel.IssueId); // Output ID of the record created to the browser console

To create a record in the recorded table, write the following script. The contents written in the first and fifth lines are different from those for the time limited table.

JavaScript
// Recorded table script
let sampleApiModel = items.NewResult();
sampleApiModel.Title = 'About Pleasanter Upgrade Procedure';
sampleApiModel.Create(123);
context.Log(sampleApiModel.ResultId); // Output ID of the record created to the browser console

Code Samples

1. Create records in a child table linked to a parent table

Prepare a "Project management" table (parent) that manages projects, and a "Task management" table (child) linked to it.

When a parent project management record is created, the standard tasks "'Requirements definition', 'Design', 'Development', 'Test', 'Release'" are created linked to it.

To do so, the record ID is retrieved after the project management record is created. By registering that ID in the task management records, records that keep the parent-child link can be created.

JavaScript

Condition: After create, After update, etc.

// Site ID cache
const _siteIdCache = new Map();
// Get the SiteId from the site name (cached)
function getSiteIdOrNull(siteName) {
    if (_siteIdCache.has(siteName)) return _siteIdCache.get(siteName);
    const site = items.GetClosestSite(siteName);
    if (!site || !site.SiteId) {
        logs.LogInfo(`${siteName} Failed to get the site information`);
        return null;
    }
    _siteIdCache.set(siteName, site.SiteId);
    return site.SiteId;
}
// Common process for creating a record
function createAndGetId(siteId, apiModel, fields) {
    // Put fields into apiModel
    for (const [key, value] of Object.entries(fields)) {
        apiModel[key] = value;
    }
    try {
        apiModel.Create(siteId);
    } catch (e) {
        logs.LogException(
            `apiModel.Create Error: ${e && e.stack ? e.stack : e}`
        );
        return null;
    }
    // Return the ID
    const id = apiModel.ResultId ?? apiModel.IssueId ?? null;
    logs.LogInfo(`created id: ${id}`);
    return id;
}
// Main process
// Create the parent (Project management)
const caseSiteId = getSiteIdOrNull('Project management');
if (!caseSiteId) {
    logs.LogException('Failed to get the Project management site.');
    return;
}
const caseId = createAndGetId(caseSiteId, items.NewResult(), {
    Title: 'Sample project',
    Status: '200',
    ClassA: 'Renewal of the customer management system',
    DateA: '2025/01/10',
    NumA: 5,
});
if (!caseId) {
    logs.LogException('Failed to create the project.');
    return;
}
// Create the children (Task management) (set the parent ID in the link column)
const taskSiteId = getSiteIdOrNull('Task management');
if (!taskSiteId) {
    logs.LogException('Failed to get the Task management site.');
    return;
}
const taskList = ['Requirements definition', 'Design', 'Development', 'Test', 'Release'];
for (const task of taskList) {
    const taskId = createAndGetId(taskSiteId, items.NewIssue(), {
        Title: 'Sample project',
        ClassA: task,
        ClassD: String(caseId),
    });
    if (!taskId) {
        logs.LogException('Failed to create the task.');
        return;
    }
}

Execution Result
(Info):created id: 9001
(Info):created id: 9002
(Info):created id: 9003
(Info):created id: 9004
(Info):created id: 9005
(Info):created id: 9006