2 records inserted in Attachment Table

ashwinishedge
Tera Contributor

Hi All,

 

I am working on Import set API Rest Integration (Using Transform Map)

My ABC (ServiceNow) Instance will receive an attachment file from a third party (ServiceNow) in base64 format and that file needs to be attached to the incident record after decoding from base64 format.

 

Scenario 1: To achieve this I have created a separate Inbound Import set with Target Table "Sys_Attachment" and created the below fields on the stagging form.

ashwinishedge_0-1729679027629.png

 

 

I created a Transform Map with a run script, but the Write() and writeBase64() methods did not work due to limitations in the global scope.

 

  var attachmentTable = "incident"// Replace with the correct target table (e.g., incident, task, etc.)
 
  var attachment = new GlideSysAttachment();
  attachment.writeBase64(attachmentTable, targetSysId, fileName, contentType, base64Encoded);
OR 
attachment.write(attachmentTable, targetSysId, fileName, contentType, decodedData);

 

I used the ECC_Queue to insert an attachment record and create it in the Attachment Table, which should link it to the Incident record. While this process worked, it resulted in the creation of two separate records in the Sys_attachment form. Consequently, the Incident form now has two attachments (if I map the file name and Table sys ID)else it will be single on the incident form. 

 

1. The first attachment has empty details because I haven’t mapped any fields from the Attachment table as it will get append on Incident form with 0 bytes file. I’m sending all the necessary fields through the ECC Queue in the script. (This record sysid I can see in response while testing in Postman)

   

2. The second attachment record contains proper details with all fields mapping and decoded files (refer to the screenshot below) This record Sysid is not coming in Response.

 

When I do map four fields, those fields are populating instead of empty records, but the resulting attachment has a size of 0 bytes.

 

ashwinishedge_1-1729679027657.png

 

 

 

If I set the "ignore" parameter to true in the Transform MAP to skip creating empty records, it results in only one record being created from ECC_Queue. However, the API response states, "Response: record is ignored by script," which does not provide me with the sys_id needed for the integration response. Therefore, I'm unable to proceed with this approach.

 

I have tried creating a Transform map script on Before/ on After but still, this script is creating a separate record on the attachment form.

 

Please see the below script.

 

(function transformRow(source, target, map, log, isUpdate) {

    // Retrieve data from Import Set table
    var base64Encoded = source.u_data; // Base64-encoded attachment data
    var fileName = source.u_file_name.toString(); // Attachment file name
    var contentType = source.u_content_type.toString(); // MIME type
    var targetNumber = source.u_number; // Target record number (e.g., incident)
    var attachmentTable = "incident"// Replace with the correct target table (e.g., incident, task, etc.)
    base64Encoded = base64Encoded ? base64Encoded.trim() : "";

    if (base64Encoded) {

        // Get the sys_id of the target incident record
        var gr = new GlideRecord(attachmentTable);
        gr.addQuery("number", targetNumber);
        gr.query();
        if (gr.next()) {
            var targetSysId = gr.sys_id;

            // Check if the attachment already exists
            var attachmentGR = new GlideRecord('sys_attachment');
            attachmentGR.addQuery('table_sys_id', targetSysId);
            attachmentGR.addQuery('file_name', fileName);
            attachmentGR.query();

            if (!attachmentGR.next()) {
                // No attachment exists, proceed to create one

                var eccGr = new GlideRecord('ecc_queue');
                eccGr.initialize();
                eccGr.setValue('agent''AttachmentCreator');
                eccGr.setValue('topic''AttachmentCreator');
                eccGr.setValue('name', fileName + ':' + contentType);
                eccGr.setValue('source', attachmentTable + ':' + targetSysId);
                eccGr.setValue('payload', base64Encoded);
                eccGr.insert();
                gs.log('Attachment created for incident with sys_id: ' + targetSysId + ' and file: ' + fileName + ' and base64Encoded: ' + base64Encoded + ' and contentType: ' + contentType + ' and attachmentTable: ' + attachmentTable);
            } else {
                log.error('attachment exists.');
            }
        } else {
            log.info('No target sys_id found for the incident.');
        }
    } else {
        log.info('No Base64 data found to decode.');
    }
})(source, target, map, log, action === "update");

 

 

Could you please suggest alternative ways we can achieve this?

 

Note: I prefer not to create a scoped application for the write() method, as it involves extra work and the need to provide roles, permissions, ACLs, and a scoped application.

0 REPLIES 0