LANG SELRCT

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

Tuesday, February 4, 2020

Salesforce APIで指定したCaseの情報を取得したい


ここで使うCase IDとは
対象ケースを開いた時にアドレスバーのCase/の後に表示される文字列↓




事前準備

接続アプリケーションを作成して
アクセストークンなどのデータをスクリプトのプロパティに保存しておく

手順は以下のブログに書きました。

SalesforceでAPIを使うために接続アプリケーションを作成したい
Salesforceで接続アプリケーションを作成したい(Lightningの方で)



コード.gs
function getData() {
  var options = {
    "method" : "GET",
    "headers" : {
      "Authorization": "Bearer " + getProp("access_token")
    }
  }
  var url = getProp("instance_url") + "/services/data/v47.0/sobjects/Case/5002w000002HSlkAAG";// Case IDのデータを取得する
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}

function getProp(key) {
  return PropertiesService.getScriptProperties().getProperty(key);
}


getData()を実行すると、以下のような結果が返ってきました。

実行結果
{
  "attributes": {
    "type": "Case",
    "url": "/services/data/v47.0/sobjects/Case/5002w000002HSlkAAG"
  },
  "Id": "5002w000002HSlkAAG",
  "IsDeleted": false,
  "MasterRecordId": null,
  "CaseNumber": "00001006",
  "ContactId": "0032w000002UiHqAAK",
  "AccountId": "0012w000004AAugAAG",
  "ParentId": null,
  "SuppliedName": null,
  "SuppliedEmail": null,
  "SuppliedPhone": null,
  "SuppliedCompany": null,
  "Type": "問題",
  "Status": "エスカレーション済み",
  "Reason": "既存の問題",
  "Origin": "Web",
  "Subject": "(サンプル)Service Cloud の使い方",
  "Priority": "中",
  "Description": null,
  "IsClosed": false,
  "ClosedDate": null,
  "IsEscalated": false,
  "OwnerId": "0052w000001oTfEAAU",
  "CreatedDate": "2020-01-16T04:17:22.000+0000",
  "CreatedById": "0052w000001oTfEAAU",
  "LastModifiedDate": "2020-02-03T02:04:50.000+0000",
  "LastModifiedById": "0052w000001oTfEAAU",
  "SystemModstamp": "2020-02-03T02:04:50.000+0000",
  "ContactPhone": "(06) 6666-1111",
  "ContactMobile": "090-0000-0000",
  "ContactEmail": "info@salesforce.com",
  "ContactFax": "(06) 6666-1112",
  "Comments": null,
  "LastViewedDate": "2020-02-03T02:04:50.000+0000",
  "LastReferencedDate": "2020-02-03T02:04:50.000+0000",
  "Channel__c": "<img src=\"/resource/OriginIcon/Web.png\" alt=\"Web\" style=\"height:22px; width:22px;\" border=\"0\"/>",
  "PriortyIcon__c": "<img src=\"/img/samples/flag_yellow.gif\" alt=\"priority flag\" border=\"0\"/>",
  "Product__c": null
}


関連記事

SalesforceでAPIを使うために接続アプリケーションを作成したい
Salesforceで接続アプリケーションを作成したい(Lightningの方で)


参考

サンプルコードを実行する
https://developer.salesforce.com/docs/atlas.ja-jp.222.0.api_rest.meta/api_rest/quickstart_code.htm

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...