基本環境
Pleasanterバージョン:1.5.2.0
Windows Server 2025 Standard 24H2
SQL Server 16.0.1
やりたいこと
Pleasanterで設定した特定のエクスポートをした時のみ、選択したレコードのステータスを変更したい
テーブルの管理>エクスポート>ID2
Status 200 → 300
アクション情報のメソッドを取得
if (context.Action === 'export') {
}
エクスポートという処理を行った時、IFの中を実行する
サーバースクリプトに保存
条件:レコード読込み時
エクスポートIDを取得
if (context.Action === 'export') {
// エクスポートIDを取得
function getParam(str, key) {
let match = str.match(new RegExp("(^|&)" + key + "=([^&]*)"));
return match ? decodeURIComponent(match[2]) : null;
}
let ExportId = getParam(context.RequestDataString, "ExportId");
logs.LogInfo(10, `エクスポートID=${ExportId}`);
}
システムログから、エクスポート処理時に
ExportId=2&ExportEncoding=Shift-JIS&GridCheckAll=undefined&GridUnCheckedItems=undefined&GridCheckedItems=2543&ExportCommentsJsonFormat=true
というデータが送信され、context.RequestDataString で取得できる
その中からExportId=2の2だけを正規表現で抜き出してExportIdの値として保存する
これによってエクスポートID2だけを判別できる
logs.LogInfo(10, `エクスポートID=${ExportId}`)
システムログでexport: エクスポートID=2と表示されれば成功
if (context.Action === 'export') {
// エクスポートIDを取得
function getParam(str, key) {
let match = str.match(new RegExp("(^|&)" + key + "=([^&]*)"));
return match ? decodeURIComponent(match[2]) : null;
}
let ExportId = getParam(context.RequestDataString, "ExportId");
logs.LogInfo(10, `エクスポートID=${ExportId}`);
// エクスポートID=2のみ動作
if (ExportId === '2') {
}
}
エクスポートID2の処理のみに反応させる
レコードIDを取得
if (context.Action === 'export') {
// エクスポートIDを取得
function getParam(str, key) {
let match = str.match(new RegExp("(^|&)" + key + "=([^&]*)"));
return match ? decodeURIComponent(match[2]) : null;
}
let ExportId = getParam(context.RequestDataString, "ExportId");
logs.LogInfo(10, `エクスポートID=${ExportId}`);
// エクスポートID=2のみ動作
if (ExportId === '2') {
// レコードID取得
function getParam02(str, key) {
let match = str.match(new RegExp("(^|&)" + key + "=([^&]*)"));
return match ? decodeURIComponent(match[2]) : null;
}
let IssueId = getParam(context.RequestDataString, "GridCheckedItems");
logs.LogInfo(10, `レコードID=${IssueId}`);
}
}
システムログから、エクスポート処理時に
ExportId=2&ExportEncoding=Shift-JIS&GridCheckAll=undefined&GridUnCheckedItems=undefined&GridCheckedItems=2543&ExportCommentsJsonFormat=true
というデータが送信され、context.RequestDataString で取得できる
その中からGridCheckedItems=2543の2543だけを正規表現で抜き出してIssueIdの値として保存する
これによってチェックを入れたレコードIDだけを判別できる
logs.LogInfo(10, `レコードID=${IssueId}`);
システムログでexport: レコードID=xxxxと表示されれば成功
チェックを入れたレコードのステータスを取得
if (context.Action === 'export') {
// エクスポートIDを取得
function getParam(str, key) {
let match = str.match(new RegExp("(^|&)" + key + "=([^&]*)"));
return match ? decodeURIComponent(match[2]) : null;
}
let ExportId = getParam(context.RequestDataString, "ExportId");
logs.LogInfo(10, `エクスポートID=${ExportId}`);
// エクスポートID=2のみ動作
if (ExportId === '2') {
// レコードID取得
function getParam02(str, key) {
let match = str.match(new RegExp("(^|&)" + key + "=([^&]*)"));
return match ? decodeURIComponent(match[2]) : null;
}
let IssueId = getParam(context.RequestDataString, "GridCheckedItems");
logs.LogInfo(10, `レコードID=${IssueId}`);
// ステータスを取得
const results = items.Get(IssueId);
const currentStatus = `${results[0].Status}`;
logs.LogInfo(10, `現在のステータス=${currentStatus}`);
}
}
logs.LogInfo(10, `現在のステータス=${currentStatus}`);
システムログで現在のステータスが表示されれば成功
ステータス200のレコードのみステータス300へ上書き更新
if (context.Action === 'export') {
function getParam(str, key) {
let match = str.match(new RegExp("(^|&)" + key + "=([^&]*)"));
return match ? decodeURIComponent(match[2]) : null;
}
let ExportId = getParam(context.RequestDataString, "ExportId");
logs.LogInfo(10, `エクスポートID=${ExportId}`);
// エクスポートID=2のみ動作
if (ExportId === '2') {
// レコードID取得
function getParam02(str, key) {
let match = str.match(new RegExp("(^|&)" + key + "=([^&]*)"));
return match ? decodeURIComponent(match[2]) : null;
}
let IssueId = getParam(context.RequestDataString, "GridCheckedItems");
logs.LogInfo(10, `レコードID=${IssueId}`);
// ステータスを取得
const results = items.Get(IssueId);
const currentStatus = `${results[0].Status}`;
logs.LogInfo(10, `現在のステータス=${currentStatus}`);
if (currentStatus == 200) {
logs.LogInfo(10, `ステータスを300に上書きします。`);
const data = {
Status: '300',
};
const result = items.Update(IssueId, JSON.stringify(data));
}
}
}
複数のレコードをエクスポートした場合
IssueIdを配列化して1データごとにステータスを取得し判別する
// 一覧画面でエクスポートをクリックした時に発火
if (context.Action === 'export') {
// エクスポートIDを取得
function getParam(str, key) {
let match = str.match(new RegExp("(^|&)" + key + "=([^&]*)"));
return match ? decodeURIComponent(match[2]) : null;
}
let ExportId = getParam(context.RequestDataString, "ExportId");
logs.LogInfo(10, `エクスポートID=${ExportId}`);
// エクスポートID=2のみ動作
if (ExportId === '2') {
// レコードIDを取得
let IssueIdStr = getParam(context.RequestDataString, "GridCheckedItems");
logs.LogInfo(10, `レコードID一覧=${IssueIdStr}`);
if (IssueIdStr) {
// カンマ区切りを配列化
const recordIds = IssueIdStr.split(",").map(id => id.trim()).filter(id => id !== "");
recordIds.forEach(function (recordId) {
// 1件ずつステータスを取得
const results = items.Get(recordId);
if (results && results[0]) {
// ステータスを取得
const currentStatus = `${results[0].Status}`;
logs.LogInfo(10, `レコードID=${recordId} 現在のステータス=${currentStatus}`);
// ステータスの値で判別
if (currentStatus == 730) {
logs.LogInfo(10, `レコードID=${recordId} のステータスを740に上書きします。`);
const data = {
Status: '740',
};
const result = items.Update(recordId, JSON.stringify(data));
}
} else {
logs.LogInfo(10, `レコードID=${recordId} のレコードが取得できませんでした`);
}
});
}
}
}
参考記事
開発者向け機能:サーバスクリプト:grid.SelectedIds
開発者向け機能:サーバスクリプト:items.Get
開発者向け機能:サーバスクリプト:items.Update
開発者向け機能:サーバスクリプト:logs.LogInfo