Skip to content

items.Update

Overview

Update the specified record.

Syntax

items.Update(recordId, data)

Parameters

Parameter Type Required Description
recordId object Yes Specify the record ID of the target record
data string Yes Specify the update content in JSON format

Return Value

If the record is updated, true is returned; if not, false is returned.

Usage Example 1

In the example below, the record title with record ID 123 is updated to "Change Title" and ClassA is updated to "Change ClassA".

JavaScript
const data = {
    Title: 'Change Title',
    ClassHash: {
        ClassA: 'Change ClassA'
    }
};
items.Update(123, JSON.stringify(data));

Usage Example 2

In the example below, the record is updated with the specific user permissions. Please write the API key of a user with write permissions in ApiKey.

JavaScript
let data = {
    ApiKey: 'xxxxx...',
    Title: 'Document management software sales',
    ClassHash: {
        ClassA: 'Software Sales'
    },
    NumHash: {
        NumA: 750000
    }
};
items.Update(123, JSON.stringify(data));

Code Samples

Modify 【 ... 】 in the code as necessary.
1. Update a record by specifying the record ID directly

Update a record by specifying the record ID directly.

JavaScript
// Specify the record ID and update the record
const recordId = 【Record ID】;
const data = {
    Title: 'Title changed',
    ClassHash: {
        ClassA: 'Failure',
    },
};
const result = items.Update(recordId, JSON.stringify(data));
if (result) {
    logs.LogInfo(`Update succeeded`);
} else {
    logs.LogUserError(`Update failed`);
}
Execution Result
(Info):Update succeeded
2. Update multiple records

Get the records that meet a condition and update those records.

JavaScript
// Specify the name of the site to process
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;
}
// Target the records whose status is Not started (100)
const status_codes = `["100"]`;
const data = {
    View: {
        ColumnFilterHash: {
            Status: `${status_codes}`,
        },
    },
};
const results = Array.from(items.Get(site.SiteId, JSON.stringify(data)));
for (const item of results) {
    // Update the status to On hold (910)
    const data = {
        Status: '910',
    };
    const result = items.Update(item.ResultId, JSON.stringify(data));
    if (result) {
        logs.LogInfo(`Update succeeded: Id=${item.ResultId}`);
    } else {
        logs.LogUserError(`Update failed: Id=${item.ResultId}`);
    }
}
Execution Result
(Info):Update succeeded: Id=9999
(Info):Update succeeded: Id=9999
(Info):Update succeeded: Id=9999
3. Delete attachments

Delete all the attachments attached to a specific record.

JavaScript
// ID of the record to process
const recordId = 【Record ID】;
// Get the record
const results = Array.from(items.Get(recordId));
if (!results || results.length === 0) {
    logs.LogInfo(`The record was not found: Id=${recordId}`);
    return;
}
// Parse AttachmentsA into an easy-to-handle form
const attachments = JSON.parse(
    $ps.JSON.stringify(results[0].AttachmentsA || [])
);
if (attachments.length === 0) {
    logs.LogInfo(`No update needed because there are no attachments: Id=${recordId}`);
    return;
}
// Specify the Guid of every attachment and update with Deleted=1 (delete the attachments)
const data = {
    AttachmentsHash: {
        AttachmentsA: attachments.map((attachment) => ({
            Guid: attachment.Guid,
            Deleted: 1,
        })),
    },
};
const result = items.Update(recordId, JSON.stringify(data));
if (result) {
    logs.LogInfo(
        `Update succeeded: Id=${recordId}, Deleted=${attachments.length}`
    );
} else {
    logs.LogUserError(`Update failed: Id=${recordId}`);
}
Execution Result
(Info):Update succeeded: Id=9999, Deleted=3
4. Set record access control on a record

Set record access control on a specific record.

JavaScript
// ID of the record to process
const recordId = 【Record ID】;
// Grant the Manager permission to group ID=1 in the record access control
const data = {
    RecordPermissions: ['Group,1,511'],
};
const result = items.Update(recordId, JSON.stringify(data));
if (result) {
    logs.LogInfo(`Update succeeded: Id=${recordId}`);
} else {
    logs.LogUserError(`Update failed: Id=${recordId}`);
}
Execution Result
(Info):Update succeeded: Id=9999

Cautions

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