LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Sunday, April 28, 2019

node-zendeskでカスタムフィールドの値が一致するチケットを更新する(search.query, tickets.updateMany)


この記事では
node-zendeks の  search.query と tickets.updateMany 
カスタムフィールドの値(一意)を検索して
一致したチケットを更新するコードを書いて実行するところまで行きます


node-zendesk を使ったことがない場合はまず Example を試しましょう
node-zendeskを使ってみる(Example)


以下3つの記事の合わせ技です



STEP
  1. index.jsを書いて保存する
  2. index.jsを実行する


STEP1:index.jsを書いて保存します
  • username, token, remoteUri は各自の環境に応じたものを入れます
  • query の fieldValue: に検索したい値を入れます
  • additional_tags, custom_fields の id, value を設定します

index.js 
var zendesk = require('node-zendesk');

var client = zendesk.createClient({
  username: 'Email',
  token: 'API TOKEN',
  remoteUri: 'https://NAME.zendesk.com/api/v2'
});

/************************************
チケットを探索する
*************************************/
var query = "fieldValue:issue-00001";
client.search.query(query, function(err, req, result) {
  if (err) return handleError(err);
  console.log(JSON.stringify(result, null, 2, true));
  var ticketId = result[0]['id'];
  console.log(ticketId);
  updateTicket(ticketId);
});

function handleError(err) {
  console.log(err);
  process.exit(-1);
}

/************************************
チケットを更新する
*************************************/
var updateTicket = function(ticketId) {
  client.tickets.updateMany(ticketId, ticket, function(err, req, result) {
    if (err) return handleError(err);
    console.log(JSON.stringify(result, null, 2, true));
  });
}

var ticket = {
  "ticket": {
    "additional_tags": ["test4"],
    "custom_fields": [{
      "id": 360018428654,
      "value": "みかん"
    }]
  }
};

function handleError(err) {
  console.log(err);
  process.exit(-1);
}




STEP2:実行してみる

ターミナル
index.jsが入っているディレクトリ $ node index.js

zendeskの対象チケットの情報が、このように返ってきました
MacBook-Air-8:nodezendeskTest NAME$ node index.js
[
  {
    "url": "https://NAME.zendesk.com/api/v2/tickets/16.json",
    "id": 16,
    "external_id": null,
    "via": {
      "channel": "api",
      "source": {
        "from": {},
        "to": {},
        "rel": null
      }
    },
    "created_at": "DATETIME",
    "updated_at": "DATETIME",
    "type": null,
    "subject": "テストタイトルです!",
    "raw_subject": "テストタイトルです!",
    "description": "本文です",
    "priority": null,
    "status": "open",
    "recipient": null,
    "requester_id": ID,
    "submitter_id": ID,
    "assignee_id": ID,
    "organization_id": ID,
    "group_id": ID,
    "collaborator_ids": [],
    "follower_ids": [],
    "email_cc_ids": [],
    "forum_topic_id": null,
    "problem_id": null,
    "has_incidents": false,
    "is_public": true,
    "due_at": null,
    "tags": [
      "test1",
      "test2",
      "test3",
      "test4",
      "みかん"
    ],
    "custom_fields": [
      {
        "id": 360018014474,
        "value": "めろん"
      },
      {
        "id": 360018442253,
        "value": ""
      },
      {
        "id": 360018495613,
        "value": "issue-00001"
      },
      {
        "id": 360017830594,
        "value": ""
      },
      {
        "id": 360018428654,
        "value": "みかん"
      }
    ],
    "satisfaction_rating": null,
    "sharing_agreement_ids": [],
    "fields": [
      {
        "id": 360018014474,
        "value": "めろん"
      },
      {
        "id": 360018442253,
        "value": ""
      },
      {
        "id": 360018495613,
        "value": "issue-00001"
      },
      {
        "id": 360017830594,
        "value": ""
      },
      {
        "id": 360018428654,
        "value": "みかん"
      }
    ],
    "followup_ids": [],
    "brand_id": ID,
    "allow_channelback": false,
    "allow_attachments": true,
    "result_type": "ticket"
  }
]
16
{
  "job_status": {
    "id": "ID",
    "url": "https://NAME.zendesk.com/api/v2/job_statuses/ID.json",
    "total": null,
    "progress": null,
    "status": "queued",
    "message": null,
    "results": null
  }
}


Latest post

Extracting data from Google Sheets with regular expressions

Introduction Regular expressions are a powerful tool that can be used to extract data from text.  In Google Sheets, regular expressions ca...