例 2:サードパーティソースから特定のレコードを取得する
この例では、サードパーティソースから特定のタイプのインシデントレコードを取得するスクリプトを作成します。
リモートテーブル API 情報については、次を参照してください。
/**
* Using `v_query`, add the rows to `v_table`
*/
(function executeQuery(v_table, v_query) {
if (v_query.isGet()) {
bySysId(v_table, v_query, v_query.getSysId());
}
else if (v_query.getParameter("caller_id")) {
byCallerId(v_table, v_query, v_query.getParameter("caller_id"));
}
else {
fetchAllIncidents(v_table, v_query);
}
/**
* fetch all incidents records from the remote instance
*/
function fetchAllIncidents(v_table, v_query) {
// Uses RestMessage with name 'Remote Instance Incidents' and function 'All Incidents'
// Create a RestMessage first which calls an external REST service
var restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'All Incidents');
loadData(v_table, v_query, restMessage);
}
/**
* fetches a specific record from the remote instance with Sys Id being `sys_id`
*/
function bySysId(v_table, v_query, sys_id) {
// Uses RestMessage with name 'Remote Instance Incidents' and function 'By SysId'
var restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'By SysId');
// sets variable 'sys_id'
restMessage.setStringParameterNoEscape("sys_id", sys_id);
loadData(v_table, v_query, restMessage);
}
/**
* fetches records from the remote instance with Caller (caller_id) being `caller_id`
*/
function byCallerId(v_table, v_query, caller_id) {
var restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'By CallerId');
restMessage.setStringParameterNoEscape("caller_id", caller_id);
loadData(v_table, v_query, restMessage);
}
/**
* Adds rows to `v_table` using `restMessage`
*/
function loadData(v_table, v_query, restMessage) {
try {
var response = restMessage.execute();
var responseBody = response.getBody();
// if REST call ends up in an error, set the last error message which shows up
// at the bottom of the list view
if (response.haveError()) {
v_query.setLastErrorMessage(response.getErrorMessage());
// can use gs.error() or gs.addErrorMessage() while debugging
// gs.debug() messages visible in session debugger
// gs.debug(response.getErrorMessage());
return;
}
} catch (ex) {
v_query.setLastErrorMessage(ex.message);
// gs.debug(ex.message);
return;
}
var transformerDefinition = getTransformerDefinition();
var transformer = new sn_tfrm.Transformer(transformerDefinition, responseBody);
// transformer parses the responseBody and extracts rows
while (transformer.transform()) {
// row is field-value map e.g. { active:"true", number: "INC0000001"}
var row = transformer.getRow();
// you may do any additional transformations to the row like GlideDuration, GlideDataTime etc. For example,
// row.duration = new GlideDuration(row.duration);
// finally add the row to the remote table
v_table.addRow(row);
}
}
/**
* returns a sn_tfrm.TransformerDefinition, which defines the mapping of the table fields and elements in the response body
*/
function getTransformerDefinition() {
// create a rule list to map a field to its element path
var ruleList = new sn_tfrm.TransformerRuleList()
.fromJSON() // the response body is a JSON
// 'active' field maps to path '$.active'
.addRule("active", "$.active")
.addRule("caller_id", "$.caller_id.value")
.addRule("number", "$.number")
.addRule("short_description", "$.short_description")
.addRule("sys_id", "$.sys_id")
.addRule("updates", "$.sys_mod_count");
var recordPath = "$.result";
return new sn_tfrm.TransformerDefinition(ruleList, recordPath);
}
})(v_table, v_query);
このスクリプトでは、次のコードスニペットを確認してください。
function fetchAllIncidents(v_table, v_query) {
// Uses RestMessage with name 'Remote Instance Incidents' and function 'All Incidents'
// Create a RestMessage first which calls an external REST service
var restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'All Incidents');
loadData(v_table, v_query, restMessage);
}
RestMessage を作成して、スクリプトでこれを直接使用することができます。この例では、リモートインスタンスインシデントという名前の RESTMessageV2 API と、すべてのインシデントデータを取得する関数 All Incidents を使用しています。サーバーから応答が返されると、データ取得で問題が発生した場合にエラーメッセージが表示されます。
function bySysId(v_table, v_query, sys_id) {
// Uses RestMessage with name 'Remote Instance Incidents' and function 'By SysId'
var restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'By SysId');
// sets variable 'sys_id'
restMessage.setStringParameterNoEscape("sys_id", sys_id);
loadData(v_table, v_query, restMessage);
}
GlideRecord.get(“<sys_id>“) を使用して特定のレコードをクエリする場合、v_query.isGet()` は `true` です。外部サービスから特定のレコードをフェッチします。
function byCallerId(v_table, v_query, caller_id) {
var restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'By CallerId');
restMessage.setStringParameterNoEscape("caller_id", caller_id);
loadData(v_table, v_query, restMessage);
}
上記のコードスニペットで caller_id を使用するなど、スクリプト内の他の特定のクエリ条件を処理することもできます。このスクリプトの残りの部分は、例 1 と同様に動作します。
function getTransformerDefinition() {
// create a rule list to map a field to its element path
var ruleList = new sn_tfrm.TransformerRuleList()
.fromJSON() // the response body is a JSON
// 'active' field maps to path '$.active'
.addRule("active", "$.active")
.addRule("caller_id", "$.caller_id.value")
.addRule("number", "$.number")
.addRule("short_description", "$.short_description")
.addRule("sys_id", "$.sys_id")
.addRule("updates", "$.sys_mod_count");
var recordPath = "$.result";
return new sn_tfrm.TransformerDefinition(ruleList, recordPath);
}
})(v_table, v_query);データ取得で問題が発生しなかった場合は、レコードのデータ本文を取得します。次に、Transformer API を使用して必要なデータ変換を実行し、行を抽出して、各レコードの行をリモートテーブルに追加します。
getTransformerDefinition は、外部 API 応答本文でレコードのスキーマを定義します。テーブルスクリプトの各フィールドを外部レコードの要素にマップします。このマッピング外の外部データ要素は、リモートテーブルでは使用できません。