チケットのJSONから特定のフォームフィールドの入力を取得する


ワークフローはkickflow内部でバージョン管理されているため、管理者がワークフローを編集する度に内部のフォームのidが変わります。 代わりに、codeというワークフローの編集をまたいで不変のキーが存在しておりますので、こちらを軸に入力を取得する必要があります。

以下の手順で指定したフィールドの入力を取得できます。

  1. 対象のフォームフィールドのcodeから、フォームフィールドのidを取得します。
    • フォームフィールドのcodeはワークフローの管理画面やREST APIで確認できます。
  2. チケットの入力値の配列inputsから、formFieldIdが1で取得したidに一致するものを取得します。

JavaScriptでの実装例を以下に示します。

/**
 * チケットのJSONから特定のフォームフィールドの入力をcodeを軸に取得する
 *
 * @param {Object} ticketJson - チケットのJSONデータ
 * @param {string} formFieldCode - 取得したいフォームフィールドのコード
 * @returns {any | null} 指定したフォームフィールドの入力値、または見つからない場合はnull
 */
function getFormFieldInputByCode(ticketJson, formFieldCode) {
  // 1. 対象のフォームフィールドのcodeから、フォームフィールドのidを取得します。
  //    ワークフローのsectionListからformFieldsを検索します。
  let targetFormFieldId = null;
  if (ticketJson && ticketJson.workflow && ticketJson.workflow.sectionList) {
    for (const section of ticketJson.workflow.sectionList) {
      if (section.formFields) {
        for (const field of section.formFields) {
          if (field.code === formFieldCode) {
            targetFormFieldId = field.id;
            break;
          }
        }
      }
      if (targetFormFieldId) {
        break;
      }
    }
  }

  if (!targetFormFieldId) {
    console.warn(`フォームフィールドのコード '${formFieldCode}' に対応するIDが見つかりませんでした。`);
    return null;
  }

  // 2. チケットの入力値の配列inputsから、formFieldIdが1で取得したidに一致するものを取得します。
  if (ticketJson && ticketJson.inputs) {
    for (const input of ticketJson.inputs) {
      if (input.formField && input.formField.id === targetFormFieldId) {
        return input.value;
      }
    }
  }

  console.warn(`フォームフィールドID '${targetFormFieldId}' に対応する入力が見つかりませんでした。`);
  return null;
}

// 使用例:
const sampleTicketJson = {
  "id": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
  "ticketNumber": "T-00001",
  "title": "備品購入申請",
  "status": "in_progress",
  "workflow": {
    "id": "workflow-uuid-1",
    "name": "備品購入ワークフロー",
    "sectionList": [
      {
        "sectionType": "form",
        "title": "申請情報",
        "formFields": [
          {
            "id": "form-field-id-1",
            "code": "item_name",
            "title": "品目名",
            "fieldType": "text"
          },
          {
            "id": "form-field-id-2",
            "code": "quantity",
            "title": "数量",
            "fieldType": "integer"
          },
          {
            "id": "form-field-id-3",
            "code": "reason",
            "title": "理由",
            "fieldType": "text_long"
          },
          {
            "id": "form-field-id-4",
            "code": "attached_file",
            "title": "添付資料",
            "fieldType": "file"
          }
        ]
      }
    ]
  },
  "inputs": [
    {
      "id": "input-id-1",
      "formField": { "id": "form-field-id-1" },
      "value": "ノートPC"
    },
    {
      "id": "input-id-2",
      "formField": { "id": "form-field-id-2" },
      "value": 1
    },
    {
      "id": "input-id-3",
      "formField": { "id": "form-field-id-3" },
      "value": "業務効率化のため"
    },
    {
        "id": "input-id-4",
        "formField": { "id": "form-field-id-4" },
        "value": null,
        "attachments": [
            {
                "signedId": "file-signed-id-1",
                "filename": "document.pdf",
                "url": "https://example.com/document.pdf"
            }
        ]
    }
  ]
};

const itemName = getFormFieldInputByCode(sampleTicketJson, "item_name");
console.log(`品目名: ${itemName}`); // 出力: 品目名: ノートPC

const quantity = getFormFieldInputByCode(sampleTicketJson, "quantity");
console.log(`数量: ${quantity}`); // 出力: 数量: 1

const reason = getFormFieldInputByCode(sampleTicketJson, "reason");
console.log(`理由: ${reason}`); // 出力: 理由: 業務効率化のため

const nonExistentField = getFormFieldInputByCode(sampleTicketJson, "non_existent_code");
console.log(`存在しないフィールド: ${nonExistentField}`); // 出力: 存在しないフィールド: null (警告も出力されます)

const attachedFile = getFormFieldInputByCode(sampleTicketJson, "attached_file");
console.log("添付ファイル(直接は値を持たないためnullが表示されます):", attachedFile); // 出力: null
// 添付ファイルの情報を取得するには、以下のようにattachmentsプロパティを参照します
const attachedFileAttachments = sampleTicketJson.inputs.find(input => input.formField.id === "form-field-id-4")?.attachments;
console.log("添付ファイルの詳細:", attachedFileAttachments);
/*
出力:
添付ファイルの詳細: [
  {
    "signedId": "file-signed-id-1",
    "filename": "document.pdf",
    "url": "https://example.com/document.pdf"
  }
]
*/

Copyright © 2025 kickflow, Inc.