items.Create
Overview¶
Create a record in the table for the specified site ID. This method can be used to specify an object (Syntax 1) or a JSON string (Syntax 2).
Limitations¶
- The ID of the created record cannot be obtained from the return value of items.Create. If you write a script using syntax 1 as shown in Usage Example 1 below, you can obtain the ID of the created record. If you use Syntax 2 as in Usage Example 2 and 3, you cannot get the ID of the created record.
Syntax 1¶
Parameters¶
| Parameter | Type | Required | Description |
|---|---|---|---|
| siteId | object | Yes | Specify the site ID of the target table. |
| model | object | Yes | Specify the apiModel object obtained by the items.NewSite, items.NewIssue, Items.Result methods. |
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 model.
Syntax 2¶
Parameters¶
| Parameter | Type | Required | Description |
|---|---|---|---|
| siteId | object | Yes | Specify the site ID of the target table |
| json | string | Yes | Specify the JSON string. Please refer to "JSON Data Layout: Common" and "JSON Data Layout: Item" |
Return Value¶
If the record is created, it returns true; if not, it returns false.
Usage Example 1¶
The following is an example of using Syntax 2. Create an apiModel object with the items.NewIssue method, and create a record with the title "How to Install Pleasanter" in the time-limited table with site ID 123.
JavaScript¶
// Script for time limited table
let sampleApiModel = items.NewIssue();
sampleApiModel.Title = 'How to install Pleasanter';
items.Create(123, sampleApiModel);
context.Log(sampleApiModel.IssueId); // Output the 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 = 'How to install Pleasanter';
items.Create(123, sampleApiModel);
context.Log(sampleApiModel.ResultId); // Output the ID of the created record to the browser console
Usage Example 2¶
The following is an example of using Syntax 2. Create a record with the title "About the Filter Function of Pleasanter" in the table with site ID 123.
JavaScript¶
Usage Example 3¶
The following is an example of using Syntax 2. Create a Wiki site under the folder with site ID 1.
JavaScript¶
let data = {
ApiVersion: 1.1,
Title: "Wiki test",
ReferenceType: "Wikis",
ParentId: 1,
InheritPermission: 1,
SiteSettings: {
ReferenceType: "Wikis"
}
}
items.Create(1, JSON.stringify(data));
Code Samples¶
Modify 【 ... 】 in the code as necessary.¶
1. Create a record with Syntax 1
Create a new record with Syntax 1.
JavaScript¶
// Specify the site name and create one record
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;
}
let apiModel = items.NewResult();
apiModel.Title = 'Sample project_P1';
apiModel.Status = 200;
apiModel.ClassA = 'Inquiry';
apiModel.DateA = '2025/01/10';
apiModel.NumA = 5;
items.Create(site.SiteId, apiModel);
// Output the ID of the created record to the browser console
logs.LogInfo(apiModel.ResultId);
Execution Result¶
2. Create a record with Syntax 2
Create a new record with Syntax 2.
JavaScript¶
// Specify the site name and create one record
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;
}
const data = {
ApiVersion: 1.1,
Title: 'Sample project_P1',
Status: 200,
ClassHash: { ClassA: 'Inquiry' },
DateHash: { DateA: '2025/1/10' },
NumHash: { NumA: 5 },
};
const result = items.Create(site.SiteId, JSON.stringify(data));
if (result) {
logs.LogInfo('Record created successfully');
} else {
logs.LogInfo('Failed to create the record');
}
Execution Result¶
3. Read a CSV file and create multiple records
Read a CSV file and create multiple records.
CSV(projects_data.csv)¶
Sample project E,900,Improvement request,10,9,2025/1/20,2025/2/10,Specifications finalized and released
Sample project D,900,Inquiry,2,2,2025/1/15,2025/1/18,Closed
Sample project C,200,Failure,4,2,2025/1/12,2025/1/13,Temporarily worked around; permanent fix under consideration
Sample project B,100,Improvement request,16,0,2025/1/11,2025/1/31,Candidate for the next release
JavaScript¶
// 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;
}
// Specify the section and file path, and read the CSV file
const section = 'files';
const path = 'projects_data.csv';
const file = $ps.file.readAllText(section, path);
// Convert the CSV string into an array
const csvRec = $ps.CSV.str2csv(file);
// Create each row of the array as a record
for (const rec of csvRec) {
let apiModel = items.NewResult();
apiModel.Title = rec[0];
apiModel.Status = rec[1];
apiModel.ClassA = rec[2];
apiModel.NumA = rec[3];
apiModel.NumB = rec[4];
apiModel.DateA = rec[5];
apiModel.DateB = rec[6];
apiModel.DescriptionA = rec[7];
items.Create(site.SiteId, apiModel);
// Output the ID of the created record to the browser console
logs.LogInfo(`Record created/${apiModel.ResultId}`);
}