Failed to Retrieve the attachment from mid server

DASAMANDAMS
Tera Contributor

Hi Team,
I am importing the attachment from mid server ,record is creating in ecc_agent_attachment table unfortunately the attachment is not retrieving I am using the below code through flow designer.

(function execute(inputs, outputs) {

    var midName = inputs.midServerName;

    var fileName = inputs.fileName;

    var folderPath = inputs.folderPath || '/Asset_Create_Import/RITM_Reports/';

    var ritmSysId = inputs.ritmSysId;

 

    // 1. Create ECC Agent Attachment

    var eccAtt = new GlideRecord('ecc_agent_attachment');

    eccAtt.initialize();

    eccAtt.name = "Import from MID";

    eccAtt.queue = 'input';

    eccAtt.source = 'MID Stream Import';

    eccAtt.setValue('agent', 'mid.server.' + midName);

    eccAtt.table_name = 'ecc_agent_attachment';

    eccAtt.table_sys_id = ritmSysId;

 

    var eccAttSysId = eccAtt.insert();

 

    // 2. Build XML payload

    var xmlString = '<?xml version="1.0" encoding="UTF-8"?>' +

      '<parameters>' +

        '<parameter name="stream_relay_response_topic" value="ExportSetResult"/>' +

        '<stage>' +

          '<stream_relay_source path="' + folderPath + fileName + '" type="FileSource"/>' +

          '<stream_relay_transform ' +

            'attachment.table_name="ecc_agent_attachment" ' +

            'attachment.table_sys_id="' + eccAttSysId + '" ' +

            'order="0" ' +

            'stream_relay_transfer_progress_interval="150" ' +

            'type="AttachmentProgressTransformer"/>' +

          '<stream_relay_sink attachment_sys_id="' + eccAttSysId + '" type="AttachmentSink"/>' +

        '</stage>' +

      '</parameters>';

 

    // 3. Send to ECC Queue for MID

    var eccQueue = new GlideRecord('ecc_queue');

    eccQueue.initialize();

    eccQueue.agent = 'mid.server.' + midName;

    eccQueue.topic = 'StreamPipeline';

    eccQueue.queue = 'output';

    eccQueue.name = 'Import File from MID';

    eccQueue.payload = xmlString;

    eccQueue.insert();

})(inputs, outputs);
Regards

2 REPLIES 2

AJ-TechTrek
Giga Sage
Giga Sage

Hi @DASAMANDAMS ,

 

As per my understanding why it fails — common root causes:

 

Issue Why it causes problem
Wrong table_name on ecc_agent_attachment Needs to be the target table you actually want the attachment on (e.g., sc_req_item), not ecc_agent_attachment
Wrong or missing agent field Should be exactly mid.server.<midName>
Missing file or wrong path File on MID not present in folder you point to
Not using correct payload / missing required attributes MID expects very specific attributes in the StreamPipeline payload
MID user permissions MID user might lack permission to read file
Attachment sink mismatch stream_relay_sink must use the same attachment_sys_id and correct table_name

 

Specific problem in your code -

You set:
eccAtt.table_name = 'ecc_agent_attachment';
eccAtt.table_sys_id = ritmSysId;

 

But that makes the attachment logically belong to table ecc_agent_attachment, which is not correct.


Instead:
* You should set:
eccAtt.table_name = 'sc_req_item'; // or the table you want the attachment on
eccAtt.table_sys_id = ritmSysId;


Because the MID Server will create an attachment on the business record, not on ecc_agent_attachment itself.

 

Corrected solution As per my understanding :-

 

1.Create ecc_agent_attachment:
Set the correct table:


eccAtt.name = fileName; // or descriptive
eccAtt.queue = 'input';
eccAtt.source = 'MID Stream Import';
eccAtt.setValue('agent', 'mid.server.' + midName);
eccAtt.table_name = 'sc_req_item'; // use the table where you want the attachment
eccAtt.table_sys_id = ritmSysId;
var eccAttSysId = eccAtt.insert();

 

2. Build XML payload:
Your structure is mostly good.
Make sure:
* attachment.table_name matches the above table
* attachment.table_sys_id matches the target record Sys ID

 

3. Send to ecc_queue:
Same as you have:
eccQueue.initialize();
eccQueue.agent = 'mid.server.' + midName;
eccQueue.topic = 'StreamPipeline';
eccQueue.queue = 'output';
eccQueue.name = 'Import File from MID';
eccQueue.payload = xmlString;
eccQueue.insert();

 

4. Validate:
* Confirm file exists at folderPath + fileName on the MID server.
* Confirm MID user has read permission.
* Confirm no errors in ECC Queue logs on the MID.

 

5. Extra improvement (recommended):
To make it robust, add:


* Check if MID Server is up & validated:
var mid = new GlideRecord('ecc_agent');
if (mid.get('name', midName) && mid.status == 'Up') {
// ok
} else {
gs.error('MID Server not available');
}
* Check file exists on MID side (may require custom probe).

 

Please appreciate the efforts of community contributors by marking appropriate response as Mark my Answer Helpful or Accept Solution this may help other community users to follow correct solution in future.
 

Thank You
AJ - TechTrek with AJ - ITOM Trainer
LinkedIn:- https://www.linkedin.com/in/ajay-kumar-66a91385/
YouTube:- https://www.youtube.com/@learnitomwithaj
Topmate:- https://topmate.io/aj_techtrekwithaj (Connect for 1-1 Session)
ServiceNow Community MVP 2025

 

Ankur Bawiskar
Tera Patron
Tera Patron

@DASAMANDAMS 

check this

How to import a CSV file from a MID Server 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader