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

9 REPLIES 9

JenniferRah
Mega Sage

I believe the GlideSysAttachment.copy command is intended to copy attachments from one record to another. So I don't think you can put sys_attachment as the first parameter. You should be able to do something like this to copy all the attachments from your current record to the new one: 

 

GlideSysAttachment.copy("global", current.sys_id, tableName, grSysId);

 

Ken Berger
Giga Guru

Hi Jennifer,

 

Thanks for your reply.  Looking at the logs, it seems that the attachments are not being pushed to the workflow.  My producer script is running the:

} else {

gs.info("No attachments to process for the record " + grSysId);

}

of the if statement, and the log shows:

 

No attachments to process for the record fa5ba0319311d61000edb8084dba1083

 

I have the record producer set to the global table, as I mentioned, because I want the workflow script to handle the record insertion on either the incident or the sc_request table (depending on the user input). 

 

Do you see where this could be the issue why there are no attachments being sent to the workflow?

 

Thanks,

Ken

 

JenniferRah
Mega Sage

Honestly, I'm surprised it works to create a record producer for the global table. I didn't even know that was possible. I'm wondering if the task table might be a better fit since sc_request and incident both extend the task table? Then you can change the sys_class_name variable to the correct table and you wouldn't even have to move any attachments. 

Ken Berger
Giga Guru

I tried using the task table but then it raised a task record and this is not desired.  When I use the global table, I see the record being added there with the attachments.  I also see the references to them in the sys_attach table.  I have abandoned trying to push the attachments from the producer to the workflow, as apparantly this can be accomplished through the workflow script alone.  This is my workflow script for handling attachments:

	// Process attachments
    // Get the producer record sys_id
    var producerSysId = current.sys_id;
	gs.info("Producer SysId is " + producerSysId);
	
    // Get the table name
    var producerTableName = current.getTableName();
	gs.info("Producer table name is " + producerTableName);
	
    // Find all attachments associated with the record producer form
    var grAttachment = new GlideRecord('sys_attachment');
    grAttachment.addQuery('table_name', producerTableName);  // Get the correct table name for the producer
    grAttachment.addQuery('table_sys_id', producerSysId); // Attachments for the producer form
    grAttachment.query();
	
    while (grAttachment.next()) {
        // Copy each attachment to the new record
        var newAttachment = new GlideSysAttachment();
		gs.info("grAttachment table name is " + grAttachment.table_name); 
		gs.info("grAttachment Sys ID is " + grAttachment.sys_id);
		
        GlideSysAttachment.copy(grAttachment.table_name, grAttachment.sys_id, tableName, grSysId);
    }	

 

It seems the issue is that the query is not returning any records.  There is nothing in the logs for the following lines:

		gs.info("grAttachment table name is " + grAttachment.table_name); 
		gs.info("grAttachment Sys ID is " + grAttachment.sys_id);

 

When I run the query on the sys_attachment table direclty, I get the expected records returned, so I don't know what could be the issue.

 

Any ideas?

 

-Ken