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

En mi caso el GlideRecord no traía ningún registro y pensé que era porque los adjuntos creados desde un Record Producer no quedan con el table_name real, sino con un prefijo automático ZZ_YY.

Por ejemplo, si el Record Producer crea registros en la tabla dmn_demand, los adjuntos no quedan como:

table_name = dmn_demand

sino así:

table_name = ZZ_YYdmn_demand

Por eso una consulta como:

grAttachment.addQuery('table_name', producerTableName);

no encontraba nada.

Probé buscando usando el prefijo:

grAttachment.addQuery('table_name', 'ZZ_YY' + producerTableName);

pero al final descubrí que realmente no era necesario filtrar por table_name.

El adjunto sí estaba relacionado correctamente con el Record Producer, pero el filtro por table_name hacía que la consulta no devolviera resultados.

La solución fue dejar únicamente:

grAttachment.addQuery('table_sys_id', producerSysId);

Con eso el GlideRecord sí devolvió los adjuntos, sin importar si el campo table_name tenía prefijo o no.

JenniferRah
Mega Sage
Mega Sage

I still think you are using the GlideSysAttachment.copy command incorrectly. What you are sending to it basically is this: 

 

GlideSysAttachment.copy('sys_attachment', {sys_id of sys_attachment record}, {desired table}, {desired record's sys_id});

 

 

What you need to be sending is something like this: 

 

GlideSysAttachment.copy(producerTableName, producerSysId, tableName, grSysId);

 

 

One command copies all the attachments from the source document into the destination document. Try that in place of everything from your comment of "Find all attachments associated with the record producer form" down and see if that works.

-O-
Kilo Patron

When you say attachment, do you mean the paperclip icon, or (a) variable(s) of type Attachment?

Though, based on the information provided, I would try to make this into an Order Guide; place the variable that decides whether it is going to be an incident, or a request on the "Describe Needs" tab, include one of the two record producers based on the answer to that variable and that's it.

I hard coded the values as a test:

// Process attachments
	var attachment = new GlideSysAttachment();
	var producerSysId = current.sys_id;
	var producerTableName = current.getTableName();
	
	gs.info("Producer SysId is " + producerSysId);
	gs.info("Producer table name is " + producerTableName);
	gs.info("Destination table name is " + tableName);
	gs.info("Destination record sysId is " + grSysId);


	attachment.copy('global', '5297beca93d152908d977e9a7bba10b8', 'incident', 'e4eb2f0e93d952908d977e9a7bba1071');

 

This worked and the attachments were copied.  I don't understand why it doesn't work with the variables.  The logs seem to show that they are strings.  I also added logic to make sure the destination record is fully written before running the attachments script:

var gRecord = new GlideRecord(tableName);
if (gRecord.get(grSysId)) {

	// Process attachments
	var attachment = new GlideSysAttachment();
	var producerSysId = current.sys_id;
	var producerTableName = current.getTableName();
	
	gs.info("Producer SysId is " + producerSysId);
	gs.info("Producer table name is " + producerTableName);
	gs.info("Destination table name is " + tableName);
	gs.info("Destination record sysId is " + grSysId);

	attachment.copy(producerTableName, producerSysId, tableName, grSysId);
} else {
    gs.error("Destination record not found.");
}

Well, I would say that the problem is that you are using something you think exists, but it does not actually.

Like I don't understand what do you mean by "and send any attachments to my workflow"?

The only way to do that is to programmatically start one.

But I see no trace of that in your script - probably you are just not including it.

But than the question becomes: what creates the actual record?

Anyway, without the whole picture, which would shed lite over sequencing, it is impossible to tell where the code goes wrong.

 

But, again, I would try to solve it with Order Guide, with Rules (which include the Record Producers) conditioned on Order Guide variables.