items.NewResult
Overview¶
Create a new instance of the "apiModel object" for the "Recorded Table" in the "Server Script".
Syntax¶
Return Value¶
Return the "apiModel object" of the "Recorded Table".
Usage Examples¶
The following example creates a record in the "Recorded Table" for site ID 3 with the title "About Pleasanter Version Upgrade".
JavaScript¶
let siteId = 3;
let item = items.NewResult();
item.Title = 'About Pleasanter Version Upgrade';
items.Create(siteId, item);
context.Log(item.ResultId); // Output ID of the created record
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;
}
}