GlideSysAttachment - スコープ指定
GlideSysAttachment API は、添付ファイルを処理するメソッドを提供します。
getContent() が呼び出されると、コンテンツはバイトアレイではなく文字列として返されます。
getContentStream() が呼び出されると、コンテンツは GlideScriptableInputStream オブジェクトとして返されます。GlideScriptableInputStream には、文字列に変換されない実際のバイト数が含まれています。
スコープ対象 GlideSysAttachment - GlideSysAttachment()
GlideSysAttachment クラスのインスタンスを作成します。
| 名前 | タイプ | 説明 |
|---|---|---|
| なし |
スコープ対象 GlideSysAttachment - addAttribute(String sysAttachmentID, String attrKey, String attrValue)
既存の添付ファイルレコードに単一の属性を追加します。
- 追加複数属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAllAttributes()
- fetchAttribute()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| sysAttachmentID | 文字列 | 添付ファイル [sys_attachment] テーブルにリストされている添付ファイルの sys_id。 |
| attrKey | 文字列 | 添付ファイル属性のキーと値のペアのキー部分。 |
| attrValue | 文字列 | 添付ファイル属性のキーと値のペアの値の部分。 |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、キー 作成者 と値 Fred Luddy を持つ属性を、指定された添付ファイルに追加する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルに表示されます。
var attach = new GlideSysAttachment();
attach.addAttribute("9e1b714601932210f877b455304df0af", "author", "Fred Luddy");
var result = attach.fetchAttribute("9e1b714601932210f877b455304df0af", "author");
while(result.next()) {
gs.info("Key: " + result.getValue('key') + " \nValue: " + result.getValue('value'));
}
出力:
Key: author
Value: Fred Luddy
スコープ付き GlideSysAttachment - addMultipleAttributes(String sysAttachmentID, Object attrsKeyValuePair)
添付ファイルレコードに複数の属性を追加します。
- 追加属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAllAttributes()
- fetchAttribute()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| sysAttachmentID | 文字列 | 添付ファイル [sys_attachment] テーブルにリストされている添付ファイルの sys_id。 |
| attrsKeyValuePair | オブジェクト | 一連のキーと値のペアの添付ファイル属性。各属性キーとその値は文字列として指定する必要があります。 たとえば、次のようになります。 |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、指定した添付ファイルに属性を追加する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルでも確認できます。
var attach = new GlideSysAttachment();
var attachmentID = '9e1b714601932210f877b455304df0af';
var attrsKeyValuePair = { };
attrsKeyValuePair["title"] = "Add multiple attributes to an attachment";
attrsKeyValuePair["category"] = "KB";
attrsKeyValuePair["length"] = "5000";
attach.addMultipleAttributes(attachmentID, attrsKeyValuePair);
var result = attach.fetchAllAttributes(attachmentID);
while(result.next()) {
gs.info("Key: " + result.getValue('key') + ", Value: " + result.getValue('value'));
}
出力:
Key: title, Value: Add multiple attributes to an attachment
Key: author, Value: Abel Tuter
Key: category, Value: KB
Key: length, Value: 5000
スコープ対象 GlideSysAttachment - copy(文字列 sourceTable, 文字列 sourceID, 文字列 targetTable, 文字列 targetID)
添付ファイルをソースレコードからターゲットレコードにコピーします。
| 名前 | タイプ | 説明 |
|---|---|---|
| sourceTable | 文字列 | コピーする添付ファイルを含むテーブルの名前。 |
| sourceID | 文字列 | ソーステーブルの sys_id。 |
| targetTable | 文字列 | 添付ファイルを追加するテーブルの名前。 |
| targetID | 文字列 | ターゲットテーブルの sys_id。 |
| タイプ | 説明 |
|---|---|
| 文字列 | コピーされた添付ファイルの sys_ids のアレイ。 |
var attachment = new GlideSysAttachment();
var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
var incGR = new GlideRecord('incident');
incGR.get(incidentSysID);
var copiedAttachments = attachment.copy('incident', incidentSysID, 'problem', incGR.getValue('problem_id'));
gs.info('Copied attachments: ' + copiedAttachments);
出力:
Copied attachments: 6e4621df1bc420501363ff37dc4bcbb0,a87769531b0820501363ff37dc4bcba2
スコープ付き GlideSysAttachment - deleteAllAttributes(String sysAttachmentID)
既存の添付ファイルレコードからすべての属性を削除します。
- 追加属性()
- 追加複数属性()
- deleteAttribute()
- fetchAllAttributes()
- fetchAttribute()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| sysAttachmentID | 文字列 | 添付ファイル [sys_attachment] テーブルにリストされている添付ファイルの sys_id。 |
| タイプ | 説明 |
|---|---|
| ブール | 属性が正常に削除されたかどうかを示すフラグ。 有効な値:
|
次の例は、指定した添付ファイルに割り当てられたすべての属性を削除する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルでも確認できます。
var attach = new GlideSysAttachment();
var attachmentID = '9e1b714601932210f877b455304df0af';
attach.deleteAllAttributes(attachmentID);
var result = attach.fetchAllAttributes(attachmentID);
while(result.next()) {
gs.info("Key: " + result.getValue('key') + ", Value: " + result.getValue('value'));
}
出力:
Attribute "null" not found for attachment "04f48541083e2e50f877df7a115e31f3"
スコープ対象 GlideSysAttachment - deleteAttachment(文字列 attachmentID)
指定した添付ファイルを削除します。
| 名前 | タイプ | 説明 |
|---|---|---|
| attachmentID | 文字列 | 添付ファイルの sys_id。 |
| タイプ | 説明 |
|---|---|
| なし |
var attachment = new GlideSysAttachment();
var attachmentSysID = 'a87769531b0820501363ff37dc4bcba2';
attachment.deleteAttachment(attachmentSysID);
スコープ対象 GlideSysAttachment - deleteAttribute(String sysAttachmentID, String attrKey)
添付ファイルレコードから指定された属性を削除します。
- 追加属性()
- 追加複数属性()
- deleteAllAttributes()
- fetchAllAttributes()
- fetchAttribute()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| sysAttachmentID | 文字列 | 添付ファイル [sys_attachment] テーブルにリストされている添付ファイルの sys_id。 |
| attrKey | 文字列 | 添付ファイル属性のキーと値のペアのキー部分。 |
| タイプ | 説明 |
|---|---|
| ブール | 添付ファイル属性が正常に削除されたかどうかを示すフラグ。 有効な値:
|
次の例は、キー 作成者を持つ指定された添付ファイルに割り当てられた属性を削除する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルでも確認できます。
var attach = new GlideSysAttachment();
var attachmentID = '9e1b714601932210f877b455304df0af';
attach.deleteAttribute(attachmentID, "author");
var result = attach.fetchAttribute(attachmentID, "author");
while(result.next()) {
gs.info("Key: " + result.getValue('key') + " \nValue: " + result.getValue('value'));
}
出力:
Attribute "author" not found for attachment "04f48541083e2e50f877df7a115e31f3"
スコープ対象 GlideSysAttachment - getAttachments(文字列 tableName, 文字列 sys_id)
名前、タイプ、サイズなど、一致する添付ファイルのメタデータを含む GlideRecord を返します。
| 名前 | タイプ | 説明 |
|---|---|---|
| tableName | 文字列 | 添付ファイルが属するテーブルの名前。たとえば incident。 |
| sys_id | 文字列 | 添付ファイルが属しているレコードの sys_id。 |
| タイプ | 説明 |
|---|---|
| GlideRecord | 名前、タイプ、サイズなど、一致する添付ファイルのメタデータを含む GlideRecord オブジェクト。 |
次のスクリプトは、2 つの添付ファイルがあるレコードの添付ファイル名をリストします。
var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments('<table_name>', '<record_sys_id>');
while(agr.next())
gs.info(agr.getValue('file_name'));
出力:
*** Script: filename1.txt
*** Script: filename2.txt
スコープ対象 GlideSysAttachment - getContent(GlideRecord sysAttachment)
添付ファイルのコンテンツを文字列として返します。
このメソッドは、スコープ対象のアプリケーションでのみ使用します。このメソッドは、グローバルスコープで使用されると undefined を返します。グローバルスコープで添付ファイルのコンテンツを読み取るには、GlideSysAttachment - Global API の getContentStream() メソッドを使用します。
- CSV (*.csv)
- JSON (*.json)
- テキスト (*.txt)
| 名前 | タイプ | 説明 |
|---|---|---|
| sysAttachment | GlideRecord | 添付ファイルレコード。 |
| タイプ | 説明 |
|---|---|
| 文字列 | 文字列としての添付ファイルのコンテンツ。最大 5 MB のデータを返します。 |
var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments('incident', '78271e1347c12200e0ef563dbb9a7109'); //create attachment GlideRecord
while(agr.next()) { //for each attachment on the incident record
gs.info(agr.getValue('file_name')); //print file name of attachment
var attachmentContent = attachment.getContent(agr);
gs.info('Attachment content: ' + attachmentContent); //print attachment content
}
出力:
Doc1.txt
Attachment content: I am text in a txt file attached to a record.
スコープ対象 GlideSysAttachment - getContentBase64(GlideRecord sysAttachment)
添付ファイルのコンテンツを Base64 エンコードを使用する文字列として返します。
このメソッドは、スコープ対象のアプリケーションでのみ使用します。このメソッドは、グローバルスコープで使用されると undefined を返します。グローバルスコープで添付ファイルのコンテンツを読み取るには、GlideSysAttachment - Global API の getContentStream メソッドを使用します。
| 名前 | タイプ | 説明 |
|---|---|---|
| sysAttachment | GlideRecord | 添付ファイルレコード。 |
| タイプ | 説明 |
|---|---|
| 文字列 | Base64 エンコードを使用する文字列としての添付ファイルのコンテンツ。最大 5 MB のデータを返します。 |
var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments('incident', '78271e1347c12200e0ef563dbb9a7109'); //create attachment GlideRecord
while(agr.next()) { //for each attachment on the incident record
gs.info(agr.getValue('file_name')); //print file name of attachment
var attachmentContent = attachment.getContentBase64(agr);
gs.info('Attachment content: ' + attachmentContent); //print attachment content
}
出力:
Doc1.txt
Attachment content: SSBhbSB0ZXh0IGluIGEgdHh0IGZpbGUgYXR0YWNoZWQgdG8gYSByZWNvcmQuIA0=
スコープ対象 GlideSysAttachment - getContentStream(文字列 sysID)
添付ファイルの sys_id を指定して GlideScriptableInputStream オブジェクトを返します。
GlideTextReader API を使用してコンテンツストリームを読み込むことができます。
| 名前 | タイプ | 説明 |
|---|---|---|
| sysID | 文字列 | 添付ファイルの sys_id。 |
| タイプ | 説明 |
|---|---|
| GlideScriptableInputStream | 添付ファイルのコンテンツを含むストリーム。 |
var attachment = new GlideSysAttachment();
var attachmentSysID = '6e4621df1bc420501363ff37dc4bcbb0';
var attachmentContentStream = attachment.getContentStream(attachmentSysID);
gs.info('Attachment content stream: ' + attachmentContentStream);
出力:
Attachment content stream: com.glide.communications.GlideScriptableInputStream@14bd299
スコープ対象 GlideSysAttachment: fetchAllAttributes(String sysAttachmentID)
指定された添付ファイルレコードからすべての属性をフェッチします。
- 追加属性()
- 追加複数属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAttribute()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| sysAttachmentID | 文字列 | 添付ファイル [sys_attachment] テーブルにリストされている添付ファイルの sys_id。 |
| タイプ | 説明 |
|---|---|
| GlideRecord | 添付ファイルレコードとそのすべての属性。 |
次の例は、指定された添付ファイルに割り当てられたすべての属性を取得する方法を示しています。
var attach = new GlideSysAttachment();
var attachmentID = '9e1b714601932210f877b455304df0af';
var result = attach.fetchAllAttributes(attachmentID);
while(result.next()) {
gs.info("Key: " + result.getValue('key') + ", Value: " + result.getValue('value'));
}
出力:
Key: title, Value: Add multiple attributes to an attachment
Key: author, Value: Abel Tuter
Key: category, Value: KB
Key: length, Value: 5000
スコープ対象 GlideSysAttachment - fetchAttribute(String sysAttachmentID, String attrKey)
添付ファイルレコードから指定された属性をフェッチします。
- 追加属性()
- 追加複数属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAllAttributes()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| sysAttachmentID | 文字列 | 添付ファイル [sys_attachment] テーブルにリストされている添付ファイルの sys_id。 |
| attrKey | 文字列 | 添付ファイル属性のキーと値のペアのキー部分。 |
| タイプ | 説明 |
|---|---|
| GlideRecord | 添付ファイルの GlideRecord と指定された属性。 |
次の例は、キー 作成者を持つ添付ファイル属性を取得する方法を示しています。
var attach = new GlideSysAttachment();
var attachmentID = '9e1b714601932210f877b455304df0af';
var result = attach.fetchAttribute(attachmentID, "author");
while(result.next()) {
gs.info("Key: " + result.getValue('key') + " \nValue: " + result.getValue('value'));
}
出力:
Key: author
Value: Abel Tuter
スコープ対象 GlideSysAttachment - updateAllAttributes(String sysAttachmentID, Object attrsKeyValuePair)
既存の添付ファイルレコードのすべての属性を更新します。
- 追加属性()
- 追加複数属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAllAttributes()
- fetchAttribute()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| sysAttachmentID | 文字列 | 添付ファイル [sys_attachment] テーブルにリストされている添付ファイルの sys_id。 |
| attrsKeyValuePair | オブジェクト | 一連のキーと値のペアの添付ファイル属性。各属性キーとその値は文字列として指定する必要があります。 たとえば、次のようになります。 |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、指定した添付ファイルの各属性を更新する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルでも確認できます。
var attach = new GlideSysAttachment();
var attachmentID = '9e1b714601932210f877b455304df0af';
var attachmentAttributes = {
"category" : "Training",
"title" : "Updating attachment attributes",
"author" : "Abraham Lincoln",
"length" : "6000"
}
attach.updateAllAttributes(attachmentID, attachmentAttributes);
var result = attach.fetchAllAttributes(attachmentID);
while(result.next()) {
gs.info("Key: " + result.getValue('key') + ", Value: " + result.getValue('value'));
}
出力:
Key: title, Value: Updating attachment attributes
Key: author, Value: Abraham Lincoln
Key: category, Value: Training
Key: length, Value: 6000
スコープ付き GlideSysAttachment - updateAttribute(String sysAttachmentID, String attrKey, String attrValue)
既存の添付ファイルレコードの 1 つの属性を更新します。
- 追加属性()
- 追加複数属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAllAttributes()
- fetchAttribute()
- updateAllAttributes()
| 名前 | タイプ | 説明 |
|---|---|---|
| sysAttachmentID | 文字列 | 添付ファイル [sys_attachment] テーブルにリストされている添付ファイルの sys_id。 |
| attrKey | 文字列 | 添付ファイル属性のキーと値のペアのキー部分。 |
| attrValue | 文字列 | 添付ファイル属性のキーと値のペアの値の部分。 |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、キー 作成者 を使用して、指定された添付ファイルに割り当てられた属性を更新し、値を Abel Tuter に変更する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルでも確認できます。
var attach = new GlideSysAttachment();
var attachmentID = '9e1b714601932210f877b455304df0af';
var originalVal = attach.fetchAttribute(attachmentID, "author");
while(originalVal.next()) {
gs.info("Original attribute value: " + originalVal.getValue('value') + "\n");
}
attach.updateAttribute(attachmentID, "author", "Abel Tuter");
var updatedVal = attach.fetchAttribute(attachmentID, "author");
while(updatedVal.next()) {
gs.info("Updated attribute value: " + updatedVal.getValue('value'));
}
出力:
Original attribute value: Fred Luddy
Updated attribute value: Abel Tuter
スコープ対象 GlideSysAttachment - write(GlideRecord record, 文字列 fileName, 文字列 contentType, 文字列 content)
指定された添付ファイルを指定されたレコードに添付します。
| 名前 | タイプ | 説明 |
|---|---|---|
| record | GlideRecord | 添付ファイルを添付するレコード。 |
| fileName | 文字列 | 添付ファイル名。 |
| contentType | 文字列 | 添付ファイルの MIME タイプ ( image/png など)。添付ファイル [sys_attachment] テーブルの [コンテンツタイプ ] フィールドにあります。 |
| コンテンツ | 文字列 | 添付ファイルのコンテンツ。 |
| タイプ | 説明 |
|---|---|
| 文字列 | 添付ファイルの sys_id。添付ファイルが追加されていない場合は null を返します。 |
var attachment = new GlideSysAttachment();
//set up inputs
var rec = new GlideRecord('incident');
rec.get('78271e1347c12200e0ef563dbb9a7109');
var fileName = 'example.txt';
var contentType = 'text/csv';
var content = 'The text that is stored inside my file';
var agr = attachment.write(rec, fileName, contentType, content);
gs.info('The attachment sys_id is: ' + agr);
出力:
The attachment sys_id is: 01271e4317c13311e0ef563dbb9abe34
スコープ対象 GlideSysAttachment - writeBase64(GlideRecord now_GR, 文字列 fileName, 文字列 contentType, 文字列 content_base64Encoded)
Base64 でエンコードされたコンテンツを使用して、指定されたレコードの添付ファイルを挿入します。
| 名前 | タイプ | 説明 |
|---|---|---|
| now_GR | GlideRecord | 添付ファイルを添付するレコード。 |
| fileName | 文字列 | 添付ファイル名。 |
| contentType | 文字列 | 添付ファイルの MIME タイプ ( image/png など)。添付ファイル [sys_attachment] テーブルの [コンテンツタイプ ] フィールドにあります。 |
| コンテンツ | 文字列 | Base64 形式の添付ファイルのコンテンツ。 |
| タイプ | 説明 |
|---|---|
| 文字列 | 作成される添付ファイルの sys_id。 |
var attachment = new GlideSysAttachment();
var rec = new GlideRecord('incident');
var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
rec.get(incidentSysID);
var fileName = 'example.txt';
var contentType = 'text/csv';
var base64Encodedcontent = 'SSBhbSB0ZXh0Lg==';
var agr = attachment.writeBase64(rec, fileName, contentType, base64Encodedcontent);
gs.info('The attachment sys_id is: ' + agr);
出力:
The attachment sys_id is: 10cde9971b0820501363ff37dc4bcba6
スコープ対象 GlideSysAttachment - writeContentStream(GlideRecord now_GR, 文字列 fileName, 文字列 contentType, GlideScriptableInputStream inputStream)
入力ストリームを使用して添付ファイルを挿入します。
| 名前 | タイプ | 説明 |
|---|---|---|
| now_GR | GlideRecord | 添付ファイルを添付するレコード。 |
| fileName | 文字列 | 添付ファイル名。 |
| contentType | 文字列 | 添付ファイルの MIME タイプ ( image/png など)。添付ファイル [sys_attachment] テーブルの [コンテンツタイプ ] フィールドにあります。 |
| コンテンツ | GlideScriptableInputStream | 添付ファイルのコンテンツ。 |
| タイプ | 説明 |
|---|---|
| 文字列 | 作成される添付ファイルの sys_id。 |
sys_attachment テーブルから test_table レコードにコンテンツ ストリームを添付します。
function copyAttachmentToGlideRecord(conceptSysId) {
// Get record from test_table using sys_id
var targetGlideRecord = new GlideRecord("test_table");
if (!targetGlideRecord.get(conceptSysId)) {
throw ("Cannot find record created by test with sys_id: " + conceptSysId);
}
// Get record from sys_attachment table
var sourceAttachmentGlideRecord = new GlideRecord('sys_attachment');
sourceAttachmentGlideRecord.query();
sourceAttachmentGlideRecord.next();
// Get field values from retrieved sys_attachment record
var fileName = sourceAttachmentGlideRecord.getValue('file_name');
var contentType = sourceAttachmentGlideRecord.getValue('content_type');
var sourceAttachmentSysId = sourceAttachmentGlideRecord.getValue('sys_id');
// Attach sys_attachment record content stream to test_table record
var gsa = new GlideSysAttachment();
gsa.writeContentStream(
targetGlideRecord,
fileName,
contentType,
gsa.getContentStream(sourceAttachmentSysId));
gs.info("Attachment created");
}