GlideSysAttachment - グローバル
GlideSysAttachment API は、添付ファイルを処理するためのメソッドを提供します。
getContentStream() が呼び出されると、コンテンツは GlideScriptableInputStream オブジェクトとして返されます。GlideScriptableInputStream には、文字列に変換されない実際のバイト数が含まれています。
GlideSysAttachment:GlideSysAttachment()
GlideSysAttachment クラスのインスタンスを作成します。
| 名前 | タイプ | 説明 |
|---|---|---|
| idAttachmentAttributes | 文字列 | 属性を持つ添付ファイルのSys_id。添付ファイルに属性があるかどうかを判断するには、 GlideSysAttachment:fetchAllAttributes()を使用します。 テーブル:添付ファイル [sys_attachment] |
次のスクリプトは、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
次の例は、指定された添付ファイルに割り当てられたすべての属性を取得する方法を示しています。
var attach = new GlideSysAttachment("04f48541083e2e50f877df7a115e31f3");
var result = attach.fetchAllAttributes();
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 - addAttribute(String attrKey, String attrValue)
既存の添付ファイルレコードに単一の属性を追加します。
- 追加複数属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAllAttributes()
- fetchAttribute()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| attrKey | 文字列 | 添付ファイル属性のキーと値のペアのキー部分。 |
| attrValue | 文字列 | 添付ファイル属性のキーと値のペアの値の部分。 |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、キー 作成者 と値 Fred Luddy を持つ属性を、指定された添付ファイルに追加する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルに表示されます。
var attach = new GlideSysAttachment("04f48541083e2e50f877df7a115e31f3");
attach.addAttribute("author", "Fred Luddy");
var result = attach.fetchAttribute("author");
while(result.next()) {
gs.info("Key: " + result.getValue('key') + " \nValue: " + result.getValue('value'));
}
出力:
Key: author
Value: Fred Luddy
GlideSysAttachment:addMultipleAttributes(オブジェクトattrsKeyValuePair)
添付ファイルレコードに複数の属性を追加します。
- 追加属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAllAttributes()
- fetchAttribute()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| attrsKeyValuePair | オブジェクト | 一連のキーと値のペアの添付ファイル属性。各属性キーとその値は文字列として指定する必要があります。 たとえば、次のようになります。 |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、指定した添付ファイルに属性を追加する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルでも確認できます。
var attach = new GlideSysAttachment("04f48541083e2e50f877df7a115e31f3");
var attrsKeyValuePair = { };
attrsKeyValuePair["title"] = "Add multiple attributes to an attachment";
attrsKeyValuePair["category"] = "KB";
attrsKeyValuePair["length"] = "5000";
attach.addMultipleAttributes(attrsKeyValuePair);
var result = attach.fetchAllAttributes();
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()
既存の添付ファイルレコードからすべての属性を削除します。
- 追加属性()
- 追加複数属性()
- deleteAttribute()
- fetchAllAttributes()
- fetchAttribute()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| なし |
| タイプ | 説明 |
|---|---|
| ブール | 属性が正常に削除されたかどうかを示すフラグ。 有効な値:
|
次の例は、指定した添付ファイルに割り当てられたすべての属性を削除する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルでも確認できます。
var attach = new GlideSysAttachment("04f48541083e2e50f877df7a115e31f3");
attach.deleteAllAttributes();
var result = attach.fetchAllAttributes();
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(文字列 attrKey)
添付ファイルレコードから指定された属性を削除します。
- 追加属性()
- 追加複数属性()
- deleteAllAttributes()
- fetchAllAttributes()
- fetchAttribute()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| attrKey | 文字列 | 添付ファイル属性のキーと値のペアのキー部分。 |
| タイプ | 説明 |
|---|---|
| ブール | 添付ファイル属性が正常に削除されたかどうかを示すフラグ。 有効な値:
|
次の例は、キー 作成者を持つ指定された添付ファイルに割り当てられた属性を削除する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルでも確認できます。
var attach = new GlideSysAttachment("04f48541083e2e50f877df7a115e31f3v");
attach.deleteAttribute("author");
var result = attach.fetchAttribute("author");
while(result.next()) {
gs.info("Key: " + result.getValue('key') + " \nValue: " + result.getValue('value'));
}
出力:
Attribute "author" not found for attachment "04f48541083e2e50f877df7a115e31f3"
GlideSysAttachment:fetchAllAttributes()
指定された添付ファイルレコードからすべての属性をフェッチします。
- 追加属性()
- 追加複数属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAttribute()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| なし |
| タイプ | 説明 |
|---|---|
| GlideRecord | 添付ファイルレコードとそのすべての属性。 |
次の例は、指定された添付ファイルに割り当てられたすべての属性を取得する方法を示しています。
var attach = new GlideSysAttachment("04f48541083e2e50f877df7a115e31f3");
var result = attach.fetchAllAttributes();
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(文字列 attrKey)
添付ファイルレコードから指定された属性をフェッチします。
- 追加属性()
- 追加複数属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAllAttributes()
- updateAllAttributes()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| attrKey | 文字列 | 添付ファイル属性のキーと値のペアのキー部分。 |
| タイプ | 説明 |
|---|---|
| GlideRecord | 添付ファイルの GlideRecord と指定された属性。 |
次の例は、キー 作成者を持つ添付ファイル属性を取得する方法を示しています。
var attach = new GlideSysAttachment("04f48541083e2e50f877df7a115e31f3");
var result = attach.fetchAttribute("author");
while(result.next()) {
gs.info("Key: " + result.getValue('key') + " \nValue: " + result.getValue('value'));
}
出力:
Key: author
Value: Abel Tuter
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 - 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 - updateAllAttributes(オブジェクトattrsKeyValuePair)
既存の添付ファイルレコードのすべての属性を更新します。
- 追加属性()
- 追加複数属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAllAttributes()
- fetchAttribute()
- updateAttribute()
| 名前 | タイプ | 説明 |
|---|---|---|
| attrsKeyValuePair | オブジェクト | 一連のキーと値のペアの添付ファイル属性。各属性キーとその値は文字列として指定する必要があります。 たとえば、次のようになります。 |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、指定した添付ファイルの各属性を更新する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルでも確認できます。
var attach = new GlideSysAttachment("04f48541083e2e50f877df7a115e31f3");
var attachmentAttributes = {
"category" : "Training",
"title" : "Updating attachment attributes",
"author" : "Abraham Lincoln",
"length" : "6000"
}
attach.updateAllAttributes(attachmentAttributes);
var result = attach.fetchAllAttributes();
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 attrKey, String attrValue)
既存の添付ファイルレコードの 1 つの属性を更新します。
- 追加属性()
- 追加複数属性()
- deleteAllAttributes()
- deleteAttribute()
- fetchAllAttributes()
- fetchAttribute()
- updateAllAttributes()
| 名前 | タイプ | 説明 |
|---|---|---|
| attrKey | 文字列 | 添付ファイル属性のキーと値のペアのキー部分。 |
| attrValue | 文字列 | 添付ファイル属性のキーと値のペアの値の部分。 |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、キー 作成者 を使用して、指定された添付ファイルに割り当てられた属性を更新し、値を Abel Tuter に変更する方法を示しています。結果は、添付ファイル属性 [sys_attachment_attribute] テーブルでも確認できます。
var attach = new GlideSysAttachment("04f48541083e2e50f877df7a115e31f3");
var originalVal = attach.fetchAttribute("author");
while(originalVal.next()) {
gs.info("Original attribute value: " + originalVal.getValue('value') + "\n");
}
attach.updateAttribute("author", "Abel Tuter");
var updatedVal = attach.fetchAttribute("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 - 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");
}