Can't get attachments to work in record producer

Ken Berger
Giga Guru

Hi folks,

 

I have created a record producer and have the table name set to "Global" (I don't want the record producer to generate a record because I am handling that via a workflow).  In my record producer script, I have the following code to capture and send any attachments to my workflow:

 

// Capture attachments from the record producer
var attachment = new GlideSysAttachment();
var recordProducerSysId = current.getUniqueValue();  // The sys_id of the record producer
var tableName = current.getTableName();  // Table name (e.g., sc_req_item)

var attachmentIds = [];  // Array to store attachment sys_ids
var attachmentGr = new GlideRecord('sys_attachment');
attachmentGr.addQuery('table_sys_id', recordProducerSysId);
attachmentGr.addQuery('table_name', tableName);
attachmentGr.query();

while (attachmentGr.next()) {
    attachmentIds.push(attachmentGr.sys_id.toString());  // Store each attachment's sys_id
}

// Pass attachments to the workflow only if attachments exist
if (attachmentIds.length > 0) {
    inputs.attachments = attachmentIds;
} else {
    gs.info("No attachments found for this record.");
}

 

The workflow raises a record in the incident or sc_request table according to the value of a variable on the producer.  This is working properly but I am not seeing any attachments on the generated record.  Here is the workflow code to handle the attachments:

// Set the link for the confirmation pop-up and the table name for attachments
if (inputs.server_type == 'server_trouble' || inputs.network_type == 'network_troubleshooting') {
	var link = '<a class="breadcrumb" href="incident.do?sys_id=';
	var tableName = 'incident';
} else {
	var link = '<a class="breadcrumb" href="sc_request.do?sys_id=';
	var tableName = 'sc_request';
}

//omitted some code here for brevity

	// Process attachments
	if (inputs.attachments && inputs.attachments.length > 0) {
		var attachment = new GlideSysAttachment();
		
		for (var i = 0; i < inputs.attachments.length; i++) {
			var attachmentSysId = inputs.attachments[i];
			attachment.copy('sys_attachment', attachmentSysId, tableName, grSysId);  // Correct table name passed here
			gs.info("Attachment " + attachmentSysId + " copied to record " + grSysId);
		}
	} else {
		gs.info("No attachments to process for the record " + grSysId);
	}

 

As always, any help is appreciated.  Thanks in advance.

 

-Ken

10 REPLIES 10

Ken Berger
Giga Guru

We are using the paper clip icon.  That's a good idea about using an order guide.  I did not know something like that existed.  Unfortunately, no formal SNOW training.  Only Google and the forums 😞

 

I am researching to see if that would be a better flow.

 

Thanks,

Ken