kintoneのカレンダーアプリのレコード保存時に日程をGoogleカレンダーに公開したい

【実現したいこと】

レコード保存時にスケジュール内容をgoogleカレンダーに登録したい。

アプリ内にボタンを設置し、ボタン押下時に同期することは下記記事を参考に実現できました。

kintoneのイベント・フェアカレンダーの日程をGoogleカレンダーに公開しよう!

今回実現したいのは「レコード保存時」に同様の処理をしたいですがうまくいきません。

どなたかお知恵をお借りできないでしょうか。

 

【テストしたコード】

(function() {
  'use strict';

  // API キー
const api_key = '✖✖';
  // クライアントID
const client_id = '✖✖';
  // カレンダーID
const calendar_id = '✖✖';
  // 認証用URL(読み取り/更新)
  const scope = 'https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.events';
  // Discovery Docs
  const discovery_docs = ['https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest'];

  // APIクライアントライブラリの初期化とサインイン
  function initClient() {
    gapi.client.init({
      'apiKey': api_key,
      'discoveryDocs': discovery_docs,
      'clientId': client_id,
      'scope': scope
    }, (error) => {
      alert('Googleへの認証に失敗しました。: ' + error);
    });
  }

  // APIクライアントとOAuth2ライブラリのロード
  gapi.load('client:auth2', initClient);

    // レコード詳細画面の表示後イベント
    //   kintone.events.on('app.record.detail.show', (event) => {
    // 増殖バグ回避
    //     if (document.getElementById('publish_button') !== null) {
    //       return event;
    //     }
    // 画面下部にボタンを設置
    //     const publishButton = document.createElement('button');
    //     publishButton.id = 'publish_button';
    //     publishButton.innerHTML = '公開する';
    //     publishButton.className = 'button-simple-cybozu geo-search-btn';
    //     publishButton.style = 'margin-top: 30px; margin-left: 10px;';
    //     publishButton.addEventListener('click', () => {
    //       publishEvent();
    //     });
    //     kintone.app.record.getSpaceElement('publish_button_space').appendChild(publishButton);
    //     return event;
    //   });

  kintone.events.on(['app.record.create.show', 'app.record.edit.show'], (event) => {
    // フィールドを編集不可へ
    event.record.event_id.disabled = true;
    return event;
  });

  kintone.events.on('app.record.edit.submit.success', async () => {
    // レコードのデータの取得
    const record = kintone.app.record.get().record;
    if (record) {
      // Google認証済みのチェック
      if (!gapi.auth2.getAuthInstance().isSignedIn.get()) {
        // Google認証の呼び出し
        await gapi.auth2.getAuthInstance().signIn();
      }
      // API リクエスト
      // リクエストパラメータの設定
      const params = {
        // イベントのタイトル
        'summary': record.event_name.value,
        'start': {
          // 開始日・時刻
          'dateTime': record.start_datetime.value,
          'timeZone': 'Asia/Tokyo'
        },
        'end': {
          // 終了日・時刻
          'dateTime': record.end_datetime.value,
          'timeZone': 'Asia/Tokyo'
        },
        // 場所の指定
        'location': record.event_location.value,
        'attendees': [
    {
      // ゲストの追加
      'email': record.attendees.value
    }],
        // イベントの説明
        'description': record.event_description.value
      };
      let request;
      // リクエストメソッドとパラメータの設定
      if (record.event_id.value) { // 公開済みイベントを更新
        request = gapi.client.calendar.events.update(
          {
            'calendarId': calendar_id,
            'eventId': record.event_id.value,
            'resource': params
          });
      } else { // 未公開のイベントを追加
        request = gapi.client.calendar.events.insert(
          {
            'calendarId': calendar_id,
            'resource': params
          });
      }
      // Googleカレンダーへのイベント登録の実行
      request.execute((resp) => {
        if (resp.error) {
          alert('イベントの登録に失敗しました。' + resp.error.message);
        } else {
          const body = {
            'app': kintone.app.getId(),
            'id': record.$id.value,
            'record': {
              'event_id': {
                'value': resp.result.id
              }
            }
          };
          return kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', body).then((success) => {
            alert('カレンダーにイベントを登録しました。');
            location.reload();
          }).catch((error) => {
            alert('Google イベントIDの登録に失敗しました。' + error);
          });
        }
        return true;
      }, (error) => {
        alert('Google イベントIDの登録に失敗しました。' + error);
      });
    }
  })
})();

この状態でレコードを保存しようとするとコンソールログで下記エラーが出て進めずにおります。

Uncaught Error: You cannot call kintone.app.record.get() in handler or during processing a handler.

 

「You cannot call kintone.app.record.get() in handler or during processing a handler.」

ということはつまり、kintone.app.record.get() が使えないという意味だということはエラーメッセージからわかると思います。

ドキュメントの「レコードの値を取得する」には

kintone.events.on のイベントハンドラ内で kintone.app.record.get および kintone.mobile.app.record.get を実行することはできません。 上記のイベントハンドラ内ではレコードデータの取得は引数のeventオブジェクトを 、レコードデータの更新はeventオブジェクトのreturnを使用してください。

とあります。
上記のイベントハンドラ内ではレコードデータの取得は引数のeventオブジェクトを 」ということなので

↓この部分を「レコード編集画面の保存成功後イベント」を参考にeventオブジェクトを使って書き換えるといいと思います。

  kintone.events.on('app.record.edit.submit.success', async () => {
    // レコードのデータの取得
    const record = kintone.app.record.get().record;

 

このトピックはベストアンサーに選ばれた返信から 3 日が経過したので自動的にクローズされました。新たに返信することはできません。