---
sourceDocument: Xanadu API リファレンス
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/ja-JP/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - ja-JP

ft:publication_title :

    - Xanadu API リファレンス

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# GlideSysAttachment - スコープ指定

# GlideSysAttachment - スコープ指定 {#ariaid-title1}

* リリースバージョン: Xanadu
* 
* 更新日 2024年08月01日
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 所要時間：13分

GlideSysAttachment API は添付ファイルを処理するメソッドを提供します。

getContent() が呼び出されると、コンテンツはバイトアレイではなく文字列として返されます。

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

## スコープ対象 GlideSysAttachment - GlideSysAttachment() {#ariaid-title2}

GlideSysAttachment クラスのインスタンスを作成します。
{#r_SGSA-GlideSysAttachment__table_iys_ctb_fv__entry__3}

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

{#r_SGSA-GlideSysAttachment__table_iys_ctb_fv}

## スコープ対象 GlideSysAttachment - copy(文字列 sourceTable, 文字列 sourceID, 文字列 targetTable, 文字列 targetID) {#ariaid-title3}

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

| 名前 | タイプ | 説明 |
|-|-|-|
| sourceTable | 文字列 | コピーする添付ファイルを含むテーブルの名前。 |
| sourceID | 文字列 | ソーステーブルの sys_id。 |
| targetTable | 文字列 | 添付ファイルを追加するテーブルの名前。 |
| targetID | 文字列 | ターゲットテーブルの sys_id。 |
[表 : 2. パラメーター]

{#r_SGSA-copy_S_S_S_S__id_kdj_vjz_g5b} {#r_SGSA-copy_S_S_S_S__id_db3_wjz_g5b__entry__2}

| タイプ | 説明 |
|-|-|
| 文字列 | コピーされた添付ファイルの sys_id のアレイ。 |
[表 : 3. 返される内容]

{#r_SGSA-copy_S_S_S_S__id_db3_wjz_g5b}  

    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) {#ariaid-title4}

指定した添付ファイルを削除します。
{#r_SGSA-deleteAttachment_S__id_ajy_dlz_g5b__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| attachmentID | 文字列 | 添付ファイルの sys_id。 |
[表 : 4. パラメーター]

{#r_SGSA-deleteAttachment_S__id_ajy_dlz_g5b} {#r_SGSA-deleteAttachment_S__table_gjy_lyb_fv__entry__2}

| タイプ | 説明 |
|-|-|
| なし |   |
[表 : 5. 返される内容]

{#r_SGSA-deleteAttachment_S__table_gjy_lyb_fv}  

    var attachment = new GlideSysAttachment();
    var attachmentSysID = 'a87769531b0820501363ff37dc4bcba2';
    attachment.deleteAttachment(attachmentSysID);

## スコープ対象 GlideSysAttachment - getAttachments(文字列 tableName, 文字列 sys_id) {#ariaid-title5}

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

| 名前 | タイプ | 説明 |
|-|-|-|
| tableName | 文字列 | 添付ファイルが属するテーブルの名前。たとえば incident。 |
| sys_id | 文字列 | 添付ファイルが属しているレコードの sys_id。 |
[表 : 6. パラメーター]

{#r_SGSA-getAttachments_S_S__id_bvk_jmz_g5b} {#r_SGSA-getAttachments_S_S__id_urm_kmz_g5b__entry__2}

| タイプ | 説明 |
|-|-|
| GlideRecord | 名前、タイプ、サイズなど、一致する添付ファイルのメタデータを含む GlideRecord オブジェクト。 |
[表 : 7. 返される内容]

{#r_SGSA-getAttachments_S_S__id_urm_kmz_g5b}  
次のスクリプトは、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) {#ariaid-title6}

添付ファイルのコンテンツを文字列として返します。
このメソッドは、スコープ対象のアプリケーションでのみ使用します。このメソッドは、グローバルスコープで使用すると `undefined` を返します。グローバルスコープで添付ファイルのコンテンツを読み取るには、GlideSysAttachment - Global API の getContentStream メソッドを使用します。  
この方法では、添付ファイルに対して次のファイルタイプがサポートされています。

* CSV (\*.csv)
* JSON (\*.json)
* テキスト (\*.txt)
{#r_SGSA-getContent_GR__ul_zd2_w21_cwb}
{#r_SGSA-getContent_GR__table_r2d_cng_2v__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| sysAttachment | GlideRecord | 添付ファイルレコード。 |
[表 : 8. パラメーター]

{#r_SGSA-getContent_GR__table_r2d_cng_2v} {#r_SGSA-getContent_GR__table_s2d_cng_2v__entry__2}

| タイプ | 説明 |
|-|-|
| 文字列 | 文字列としての添付ファイルのコンテンツ。最大 5 MB のデータを返します。 |
[表 : 9. 返される内容]

{#r_SGSA-getContent_GR__table_s2d_cng_2v}  

    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) {#ariaid-title7}

添付ファイルのコンテンツを Base64 エンコードを使用する文字列として返します。
このメソッドは、スコープ対象のアプリケーションでのみ使用します。このメソッドは、グローバルスコープで使用すると `undefined` を返します。グローバルスコープで添付ファイルのコンテンツを読み取るには、GlideSysAttachment - Global API の getContentStream メソッドを使用します。
{#r_SGSA-getContentBase64_GR__table_kds_4sb_fv__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| sysAttachment | GlideRecord | 添付ファイルレコード。 |
[表 : 10. パラメーター]

{#r_SGSA-getContentBase64_GR__table_kds_4sb_fv} {#r_SGSA-getContentBase64_GR__table_lds_4sb_fv__entry__2}

| タイプ | 説明 |
|-|-|
| 文字列 | Base64 エンコードを使用する文字列としての添付ファイルのコンテンツ。最大 5 MB のデータを返します。 |
[表 : 11. 返される内容]

{#r_SGSA-getContentBase64_GR__table_lds_4sb_fv}  

    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) {#ariaid-title8}

添付ファイルの sys_id を指定して GlideScriptableInputStream オブジェクトを返します。
[GlideTextReader](https://servicenow-prod.fluidtopics.net/wv87gT0LbIiSaTf0X36YMQ#c_GlideTextReaderScopedAPI "GlideTextReader API は、入力ストリームから 1 行を読み取る機能を提供します。入力ストリームが使用されるため、5 MB の添付ファイルサイズ制限の対象にはなりません。") API を使用してコンテンツストリームを読み込むことができます。
{#r_SGSA-getContentStream_S__id_xhr_q4z_g5b__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| sysID | 文字列 | 添付ファイルの sys_id。 |
[表 : 12. パラメーター]

{#r_SGSA-getContentStream_S__id_xhr_q4z_g5b} {#r_SGSA-getContentStream_S__id_yry_q4z_g5b__entry__2}

| タイプ | 説明 |
|-|-|
| GlideScriptableInputStream | 添付ファイルのコンテンツを含むストリーム。 |
[表 : 13. 返される内容]

{#r_SGSA-getContentStream_S__id_yry_q4z_g5b}  

    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) {#ariaid-title9}

指定された添付ファイルを指定されたレコードに添付します。
{#r_SGSA-write_GR_S_S_S__id_zx4_v4z_g5b__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| record | GlideRecord | 添付ファイルを添付するレコード。 |
| fileName | 文字列 | 添付ファイル名。 |
| contentType | 文字列 | 添付ファイルのコンテンツタイプ。 |
| コンテンツ | 文字列 | 添付ファイルのコンテンツ。 |
[表 : 14. パラメーター]

{#r_SGSA-write_GR_S_S_S__id_zx4_v4z_g5b} {#r_SGSA-write_GR_S_S_S__id_rnx_v4z_g5b__entry__2}

| タイプ | 説明 |
|-|-|
| 文字列 | 添付ファイルの sys_id。添付ファイルが追加されていない場合は null を返します。 |
[表 : 15. 返される内容]

{#r_SGSA-write_GR_S_S_S__id_rnx_v4z_g5b}  

    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) {#ariaid-title10}

Base64 でエンコードされたコンテンツを使用して、指定されたレコードの添付ファイルを挿入します。
{#r_SGSA-writeBase64_GR_S_S_S__table_nfx_fwx_hx__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| now_GR | GlideRecord | 添付ファイルを添付するレコード。 |
| fileName | 文字列 | 添付ファイルのファイル名。 |
| contentType | 文字列 | 添付ファイルのコンテンツタイプ。 |
| content | 文字列 | Base64 形式の添付ファイルのコンテンツ。 |
[表 : 16. パラメーター]

{#r_SGSA-writeBase64_GR_S_S_S__table_nfx_fwx_hx} {#r_SGSA-writeBase64_GR_S_S_S__table_ofx_fwx_hx__entry__2}

| タイプ | 説明 |
|-|-|
| 文字列 | 作成される添付ファイルの sys_id。 |
[表 : 17. 返される内容]

{#r_SGSA-writeBase64_GR_S_S_S__table_ofx_fwx_hx}  

    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) {#ariaid-title11}

入力ストリームを使用して添付ファイルを挿入します。
{#r_SGS-writeContentStrm_GR_S_S_GSIS__id_n5v_npz_g5b__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| now_GR | GlideRecord | 添付ファイルを添付するレコード。 |
| fileName | 文字列 | 添付ファイルのファイル名。 |
| contentType | 文字列 | 添付ファイルのコンテンツタイプ。 |
| コンテンツ | GlideScriptableInputStream | 添付ファイルのコンテンツ。 |
[表 : 18. パラメーター]

{#r_SGS-writeContentStrm_GR_S_S_GSIS__id_n5v_npz_g5b} {#r_SGS-writeContentStrm_GR_S_S_GSIS__id_lkf_npz_g5b__entry__2}

| タイプ | 説明 |
|-|-|
| 文字列 | 作成される添付ファイルの sys_id。 |
[表 : 19. 返される内容]

{#r_SGS-writeContentStrm_GR_S_S_GSIS__id_lkf_npz_g5b}  
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");
    }


