プロセス管理の設定を別アプリにコピーする

著者名:近本昌也

目次

はじめに

今回の Tips では A アプリのプロセス管理の設定を B アプリにコピーするというカスタマイズを紹介します。

アプリの準備

本 Tips ではコピー先とコピー元のアプリをそれぞれ用意します。

  • コピー元:「旅費精算申請」アプリ(アプリストアより利用可能)
  • コピー先:「新しいアプリ」アプリ(アプリをはじめから作成し、デフォルトのまま作成したもの)

コピー元「旅費精算申請」アプリ

「旅費精算申請」アプリには次のワークフローが設定されています。

ワークフローのイメージ図はこちらです。

「旅費精算申請」アプリのプロセス管理の設定画面を開き、「上長」フィールドを削除します(コピー先のアプリに設定されていないフィールドを設定すると、更新時にエラーが発生するためです)。

コピー先「新しいアプリ」アプリ

「新しいアプリ」アプリは特に設定を変更する必要がありません。
デフォルトで次のワークフローがプロセス管理に設定されていることを確認してください。

ワークフローのイメージ図はこちらです。

カスタマイズ

以下の 4 つのステップでコピー元「旅費精算申請」アプリに JavaScript カスタマイズを適用させ、プロセス管理の設定をコピー先「新しいアプリ」アプリに更新してみましょう。

  1. コピー元「旅費精算申請」アプリの一覧画面にボタンを配置
  2. コピー元のプロセス管理の設定を取得
  3. コピー先の設定を 2 で取得した設定に更新
  4. コピー先アプリの設定を反映

1. コピー元「旅費精算申請」アプリの一覧画面にボタンを配置

ボタンの配置方法は 第2回 レコード一覧画面にボタンを置いてみよう! を参照しました。
コピー先のアプリ番号を控え、変数に入力します。
ボタンクリック時の関数については 2 から 4 で説明します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(() => {
  'use strict';

  const COPY_APPID = '1000';// コピー対象のアプリ番号を設定

  kintone.events.on(['app.record.index.show'], (event) => {
    if (document.getElementById('CopyProcessManagement') !== null) {
      return;
    }
    const menuButton = document.createElement('button');
    menuButton.id = 'CopyProcessManagement';
    menuButton.innerHTML = 'Copy Process Management';
    menuButton.onclick = function() {
      if (window.confirm('アプリ番号「' + COPY_APPID + '」にプロセス管理の設定をコピーしますか?')) {
        getProcessManagement()
          .then(putProcessManagement)
          .then(deployProcessManagement)
          .catch((error) => {
            alert(error);
          });
      }
    };
    kintone.app.getHeaderMenuSpaceElement().appendChild(menuButton);

  });
})();

2. コピー元のプロセス管理の設定を取得

プロセス管理の設定を取得する を利用してプロセス管理の設定を取得します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function getProcessManagement() {
  const body = {
    app: kintone.app.getId()
  };
  return kintone.api(kintone.api.url('/k/v1/app/status', true), 'GET', body).then((resp) => {
    return resp;
  }, (error) => {
    return kintone.Promise.reject(error.message);
  });
}

3. コピー先の設定を②で取得した設定に更新

プロセス管理の設定を変更する を利用して 2 で取得した設定をコピー先「新しいアプリ」アプリに更新をかけます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function putProcessManagement(original) {
  const body = {
    app: COPY_APPID,
    enable: original.enable,
    actions: original.actions,
    states: original.states
  };

  return kintone.api(kintone.api.url('/k/v1/preview/app/status', true), 'PUT', body).then((resp) => {

  }, (error) => {
    return kintone.Promise.reject(error.message);
  });
}

4. コピー先アプリの設定を反映

アプリの設定を運用環境へ反映する を利用して、3 で更新した内容を運用環境に反映すると完成です。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function deployProcessManagement(copy) {
  const body = {
    apps: [
      {
        app: COPY_APPID
      }
    ]
  };
  return kintone.api(kintone.api.url('/k/v1/preview/app/deploy', true), 'POST', body).then((resp) => {
    alert('アプリ番号「' + COPY_APPID + '」にプロセス管理の設定をコピーしました。');
  }, (error) => {
    return kintone.Promise.reject(error.message);
  });
}

サンプルコード

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright (c) 2016 Cybozu
 * Licensed under the MIT License
 */
(() => {
  'use strict';

  const COPY_APPID = 1000;

  function getProcessManagement() {
    const body = {
      app: kintone.app.getId()
    };
    return kintone.api(kintone.api.url('/k/v1/app/status', true), 'GET', body).then((resp) => {
      return resp;
    }, (error) => {
      return kintone.Promise.reject(error.message);
    });
  }

  function putProcessManagement(original) {
    const body = {
      app: COPY_APPID,
      enable: original.enable,
      actions: original.actions,
      states: original.states
    };

    return kintone.api(kintone.api.url('/k/v1/preview/app/status', true), 'PUT', body).then((resp) => {

    }, (error) => {
      return kintone.Promise.reject(error.message);
    });
  }

  function deployProcessManagement() {
    const body = {
      apps: [
        {
          app: COPY_APPID
        }
      ]
    };
    return kintone.api(kintone.api.url('/k/v1/preview/app/deploy', true), 'POST', body).then((resp) => {
      alert('アプリ番号「' + COPY_APPID + '」にプロセス管理の設定をコピーしました。');
    }, (error) => {
      return kintone.Promise.reject(error.message);
    });
  }

  kintone.events.on(['app.record.index.show'], (event) => {
    if (document.getElementById('CopyProcessManagement') !== null) {
      return;
    }
    const menuButton = document.createElement('button');
    menuButton.id = 'CopyProcessManagement';
    menuButton.innerHTML = 'Copy Process Management';
    menuButton.onclick = function() {
      if (window.confirm('アプリ番号「' + COPY_APPID + '」にプロセス管理の設定をコピーしますか?')) {
        getProcessManagement()
          .then(putProcessManagement)
          .then(deployProcessManagement)
          .catch((error) => {
            alert(error);
          });
      }
    };
    kintone.app.getHeaderMenuSpaceElement().appendChild(menuButton);

  });
})();

動作確認

  • コピー元のアプリのレコード一覧画面にプロセス管理の設定コピー用のボタンを設置

  • ボタンをクリックするとプロセス管理の設定をコピーし、アラートを表示

  • コピー前のプロセス管理の設定画面

  • コピー後のプロセス管理の設定画面

利用した API

参考 Tips

最後に

今回はプロセス管理の設定の取得/変更を使ったカスタマイズ例を紹介しました。
利用ケースの多いワークフローを使い回したいときにボタンひとつで設定できてとても便利なしくみになっているので、本 Tips を活用してカスタマイズしていただければと思います。

information

この Tips は、2016 年 12 月版 kintone で動作を確認しています。