0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

kintone アプリアクション先のレコード保存後にアプリアクション元のレコードを更新する

Last updated at Posted at 2021-05-23

アプリアクションを実行して、アクション先のアプリのレコード保存成功後イベントを受けて、アクション元のレコードを更新するサンプルです。

※コードにはエラー処理などは入れてませんのであくまで参考程度にしてください。

環境

  • TypeScript

動作イメージ

kintoneな人はイメージできると思いますが。。。

Aアプリ・・・アクション元
Bアプリ・・・アクション先

  1. Aアプリ---(アプリアクション)---> Bアプリの新規登録画面が別タブで開かれる
  2. Bアプリ---(いろいろ編集)--->保存--->サーバーの保存成功
  3. Bアプリで登録したデータをAアプリに更新登録

コード

'use strict'
interface KintoneEvent {
  record: kintone.types.SavedFields;
}
class AppAction {
  appId: number
  recordId: number
  action: number
  constructor(action?: number, appId?: number, recordId?: number) {
    this.action = action;
    this.appId = appId;
    this.recordId = recordId;
  }
  isAction() {
    if (this.action !== null) {
      return true;
    } else {
      return false;
    }
  }
}
// アプリアクションで呼ばれたレコード作成時にレコード保存成功後イベントを受けて
// 別アプリのレコードを更新する
const EVENTS = ['app.record.create.submit.success'];
kintone.events.on(EVENTS, (event: KintoneEvent) => {
  // アプリアクションからの同線かを確認して、違ったらリターンする。
  // https://example.cybozu.com/k/100/edit?action=1234567&app=99&record=1
  const href: string = window.location.href;
  const params = (new URL(href)).searchParams;
  if (!params.has('action')) {
    return event;
  }
  let appAction = 
    new AppAction(
      Number(params.get('action')),
      Number(params.get('app')),
      Number(params.get('record')),
    );

    return new kintone.Promise((resolve) => {
    // アプリアクション元のレコードを更新する
    const record = event.record;
    const body = {
      'app': appAction.appId,
      'id': appAction.recordId,
      'record': {
        'customerCode': {
          'value': record['顧客コード'].value
        }
      }
    };
    resolve(kintone.api(kintone.api.url('/k/v1/record.json', true), 'PUT', body));
  })
  .then(result => {
    // アプリアクションの呼び出し元のウィンドウをリロードする。
    const response = result;
    console.log(response);
    const windowOpenerRef = window.opener;
    windowOpenerRef.location.reload(true);
    return event;
  })
  .catch(error => {
    console.log(error);
  })

});

スクリーンショット

アプリAでアプリアクション実行。
スクリーンショット 2021-05-23 11.48.45.png

アプリBでデータ入力して保存。
スクリーンショット 2021-05-23 11.49.13.png

保存成功時に、顧客コードが自動で採番されます。
スクリーンショット 2021-05-23 11.49.27.png

自動採番された顧客コードをアプリAに戻してリロードします。
スクリーンショット 2021-05-23 11.49.39.png

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?