KrewData Features / Real Time Execution / Execute Process from External App
In This Topic
    Execute Process from External App
    In This Topic

    An execution API can be executed not just by calling from JavaScript code that customizes kintone app, but can also be executed from any application running outside kintone.

    Execution API of krewData does not allow cross-domain communication due to CORS constraints.

    Set Parameters

    Parameters used while execution of data editing flow set the record information(record) as request parameter of execution request API(run). The record information that you set is in the same format as the record information object that you get or set in Get Records (GET)Add Records (POST)Update Records (PUT)(record.json) of kintone API.

    In sample code used in this topic, following record information is specified as parameter. Value "Sales Department" of the field code "DepartmentName" included in the record information can be referenced by specifying the parameter "%DepartmentName%" in commands of the data editing flow.

    var record = {
       "DepartmentName": {
            "value": "Sales Department"
        }
    }

    Sample Code (Node.js)

    Below is the Node.js sample code to run execution API of krewData.

    For sample code to call execution API used to customize the kintone app, see API Reference.

    Sample Code
    Copy Code
    const request = require('request-promise');
    async function runKrewData() {
      var url = 'https://api.krewdata.mescius.com/trigger/v1/abcdefghijklmn';
      var krewdataAppUrl = 'https://yourdomain.cybozu.com/k/123';
      var serialNumber = 'abcdef-jklmno-pqrstu-xyz123-456789';
      // Request parameter of execution request API (run)
      var user = 'krewdata@example.com';
      var appId = '1234';
      var record = {
        "DepartmentName": {
          "value": "Sales Department"
        }
      }
      // Request access token
      var tokenBody = {
        'krewdataAppUrl': krewdataAppUrl,
        'serialNumber': Buffer.from(serialNumber).toString('base64')
      };
      var tokenResponse = await sendRequest('token', url, tokenBody);
      console.log("token response: " + JSON.stringify(tokenResponse));
      // Request execution of execution unit
      if (tokenResponse.code == 200) {
        var accessToken = tokenResponse.accessToken;
        var runBody = {
          'accessToken': accessToken,
          'user': user,
          'appId': appId,
          'record': record
        };
        var runResponse = await sendRequest('run', url, runBody);
        console.log("run response: " + JSON.stringify(runResponse));
        // Verify the state of execution unit
        if (runResponse.code == 200) {
          var taskId = runResponse.taskId;
          var statusBody = {
            'accessToken': accessToken,
            'taskId': taskId
          };
          var statusInterval = setInterval(async function () {
            var statusResponse = await sendRequest('status', url, statusBody);
            console.log("status response: " + JSON.stringify(statusResponse));
            if (statusResponse.taskStatus == 'Success' || statusResponse.taskStatus == 'Failure') {
              clearInterval(statusInterval);
            }
          }, 5000); // 5sec
        }
      }
    }
    
    async function sendRequest(api, url, body) {
      var options = {
        method: 'POST',
        uri: url + '/' + api,
        json: body,
        headers: {
          'Content-type': 'application/json'
        }
      };
      return new Promise((resolve, reject) => {
        request(options)
          .then((response) => {
            resolve(response);
          })
          .catch((err) => {
            console.log("Error: " + err);
            reject(err);
          });
      });
    }
    
    runKrewData();
    
    See Also