GlideSysAttachment - グローバル

  • リリースバージョン: Yokohama
  • 更新日 2025年02月18日
  • 所要時間:8分
  • GlideSysAttachment API は、添付ファイルを処理するためのメソッドを提供します。

    getContentStream() が呼び出されると、コンテンツは GlideScriptableInputStream オブジェクトとして返されます。GlideScriptableInputStream には、文字列に変換されない実際のバイト数が含まれています。

    GlideSysAttachment - GlideSysAttachment()

    GlideSysAttachment クラスのインスタンスを作成します。

    表 : 1. パラメーター
    名前 タイプ 説明
    なし

    GlideSysAttachment - copy(文字列 sourceTable, 文字列 sourceID, 文字列 targetTable, 文字列 targetID)

    添付ファイルをソースレコードからターゲットレコードにコピーします。

    表 : 2. パラメーター
    名前 タイプ 説明
    sourceTable 文字列 コピーする添付ファイルを含むテーブルの名前。
    sourceID 文字列 ソーステーブルの sys_id。
    targetTable 文字列 添付ファイルを追加するテーブルの名前。
    targetID 文字列 ターゲットテーブルの sys_id。
    表 : 3. 返される内容
    タイプ 説明
    文字列 コピーされた添付ファイルの sys_id のアレイ。
    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 - deleteAttachment(文字列 attachmentID)

    指定した添付ファイルを削除します。

    表 : 4. パラメーター
    名前 タイプ 説明
    attachmentID 文字列 添付ファイルの sys_id。
    表 : 5. 戻り値
    タイプ 説明
    なし
    var attachment = new GlideSysAttachment();
    var attachmentSysID = 'a87769531b0820501363ff37dc4bcba2';
    attachment.deleteAttachment(attachmentSysID);

    GlideSysAttachment - getAttachments(文字列 tableName, 文字列 sys_id)

    名前、タイプ、サイズなど、一致する添付ファイルのメタデータを含む GlideRecord を返します。

    表 : 6. パラメーター
    名前 タイプ 説明
    tableName 文字列 添付ファイルが属するテーブルの名前。たとえば incident。
    sys_id 文字列 添付ファイルが属しているレコードの sys_id。
    表 : 7. 戻り値
    タイプ 説明
    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 を使用してコンテンツストリームを読み込むことができます。

    表 : 8. パラメーター
    名前 タイプ 説明
    sysID 文字列 添付ファイルの sys_id。
    表 : 9. 返される内容
    タイプ 説明
    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 - write(GlideRecord record, 文字列 fileName, 文字列 contentType, 文字列 content)

    指定された添付ファイルを指定されたレコードに添付します。

    表 : 10. パラメーター
    名前 タイプ 説明
    record GlideRecord 添付ファイルを添付するレコード。
    fileName 文字列 添付ファイル名。
    contentType 文字列 添付ファイルの MIME タイプ ( image/png など)。添付ファイル [sys_attachment] テーブルの [コンテンツタイプ ] フィールドにあります。
    コンテンツ 文字列 添付ファイルのコンテンツ。
    表 : 11. 返される内容
    タイプ 説明
    文字列 添付ファイルの 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)

    入力ストリームを使用して添付ファイルを挿入します。

    表 : 12. パラメーター
    名前 タイプ 説明
    now_GR GlideRecord 添付ファイルを添付するレコード。
    fileName 文字列 添付ファイル名。
    contentType 文字列 添付ファイルの MIME タイプ ( image/png など)。添付ファイル [sys_attachment] テーブルの [コンテンツタイプ ] フィールドにあります。
    コンテンツ GlideScriptableInputStream 添付ファイルのコンテンツ。
    表 : 13. 返される内容
    タイプ 説明
    文字列 作成される添付ファイルの 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");
    }