Can't get attachments to work in record producer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2024 08:57 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 12:26 PM - edited 10-18-2024 12:30 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 04:24 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 09:30 PM
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.");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2024 10:03 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 05:20 PM - edited 10-18-2024 05:21 PM
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