//参考スクリプト startLineworksSurvey.js // botId = $('#Results_Class001').val(); // surveyId = $('#Results_Class002').val(); // targetId = $('#Results_Class003').val(); const WEBHOOK_URL = 'https://neo999.next-hd.net/lineworksSurvey/webhook'; const API_KEY = 'pgqhLFWbDFu4Byz#4afNYX2F6Fa1&$KPjved$8%sUdTQV52caip#EpIKxYUkdd4S'; $p.events.on_grid_load = function () { // アンケート一括発行ボタンの作成 createSurveyAllButton(); } function createSurveyAllButton() { if ($('#surveyAll').length > 0) { return; } //処理内容 let button = document.createElement('button'); button.type = 'button'; button.id = 'surveyAll'; button.name = 'surveyAll'; button.innerText = 'アンケート一括発行'; button.setAttribute('onclick', 'sendSurveyAll()'); $("#MainCommands button:last-child").after(button); $('#surveyAll').button({ icon: 'ui-icon-disk' }); } async function sendStartRequest(botId, surveyId, targetId, resultId) { // const botId = $('#Results_Class001').val(); // const surveyId = $('#Results_Class002').val(); // const targetId = $('#Results_Class003').val(); // const resultId = $p.id(); if (!surveyId || !targetId || !botId) { console.log('surveyId, targetId, botId をソース内で設定してください。'); return false; } if (!API_KEY) { console.log('API_KEY is empty. Check config.js / environment.'); return false; } const payload = { surveyId, targetId, botId, resultId }; const res = await fetch(WEBHOOK_URL, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': API_KEY }, body: JSON.stringify(payload) }); const text = await res.text(); let body; try { body = JSON.parse(text); } catch { body = { raw: text }; } if (!res.ok) { console.log('Start request failed:', res.status, body); process.exit(1); } /* Start request success: { result: 'ok', sessionId: 'eef7af18-065a-4486-b5a5-9f65bca61d5a', message: 'アンケートを開始しました' } */ console.log('Start request success:', body); return true; } /* アンケート発行処理 */ async function sendSurveyAll() { let siteId = $p.siteId(); /*#Grid .grid-check 一覧のチェックボックスがオンのものの、data-idの値を全て取得し、配列に格納 */ let checkedIds = []; $('#Grid .grid-check:checked').each(function () { checkedIds.push($(this).data('id')); } ); console.log('Checked IDs:', checkedIds); if (checkedIds.length === 0) { console.log('No records selected. Please check at least one record.'); $p.clearMessage(); $p.setMessage('#Message', JSON.stringify({ Css: 'alert-warning', Text: 'アンケートを一括発行するレコードの左端のチェックボックスをオンにしてください。' })); //return; } else { $p.apiGet({ 'id': siteId, 'data': { 'View': { 'ColumnFilterHash': { 'Status': "['100']" } } }, 'done': function (data) { console.log('API data:', data); if (data.Response.TotalCount > 0) { let records = data.Response.Data.filter(record => checkedIds.includes(record.ResultId)); $.each(records, async function (index, record) { let resultId = record.ResultId;; let botId = record.ClassHash.Class001; let surveyId = record.ClassHash.Class002; let targetId = record.ClassHash.Class003; console.log(`Processing record ${index + 1}/${records.length}:`, { resultId, botId, surveyId, targetId }); // アンケート開始リクエストを送信 if (await sendStartRequest(botId, surveyId, targetId, resultId)) { console.log(`Survey started for record ${resultId}. Updating status...`); await updateSurveyRecord(resultId); } }); $p.clearMessage(); $p.setMessage('#Message', JSON.stringify({ Css: 'alert-success', Text: 'アンケートを一括発行しました' })); setTimeout(() => { location.reload(); }, 3000); } } }); } } // アンケート開始後、ステータスを150に更新する関数 async function updateSurveyRecord(resultId) { $p.apiUpdate({ 'id': resultId, 'data': { 'ApiVersion': 1.1, 'Status': '150' }, 'done': function (data) { console.log(data); } }); }