Attachment variable on record producer not getting added as attachment on incident created

ritaaudi
Tera Contributor

Hi:

I created an attachment variable on a record producer. It seems that when the user adds the attachment and submits the form, it is not getting attached to the incident record created by the record producer. I see it creates the highlighted as example but does not get attached to the incident record.

find_real_file.png

 

Please advise.

Thank you, Rita

1 ACCEPTED SOLUTION

 

You can use following script in record producer script section to attach the attachment to current record, you can update it accordingly 

var gr = new GlideRecord('sys_attachment');
if(gr.get(producer.attachment)){ //attachment field name
	gr.table_name='incident';     //copy to table name
	gr.table_sys_id=current.sys_id;//copy to record sys_id
	gr.insert();
	
}

View solution in original post

14 REPLIES 14

Does not work.

"GlideSysAttachment.copy will always cause the new copy to have a table name starts with ZZ_YY for attachments variables, which according to this KB0868420"

@NagyDan I didn't use GlideSysAttachment.copy in the use case above. Although, I'll detail where I've had success with the script provided so my answer is clearer for other developers coming to this post.

It basically just updates the reference on the sys_attachment table to point to the table you want the attachment to live on, by directly manipulating the record.

For Example:

I created a record producer that points to the Incident table and a client script that logs the newValue parameter in an OnChange client script for My Attachment.

Note the sys_id in the console.

 

find_real_file.png

 I can then go to the sys_attachment(s) table and see that this is available to me by this filter seen in the image below. Also, correct about the Table Name using ZZ_YY.

find_real_file.png

After submitting the Record producer, you can refresh the Attachments [ sys_attachment ] table to see the Table Name field has now been updated to the table name your Record Producer is pointed to. Note the Table Sys ID field is the sys_id of the incident my example has just created, so nothing to do here just more information for future decisions if needed.

find_real_file.png

 

Here is the example of my code in the Record Producer.

find_real_file.png

 

As you can see all it is doing is calling the record from the sys_attachment table directly and updating the Table Name reference.

I've not ran into any problems with this solution so far, but if someone notices something in the community reading this post please add to it so we can make the community stronger.

Thanks for reading.

 

 

This is poor logic, if there is an attachment file with the same name that already exists. It's possible for multiple users to upload identical files. Better to use an After Business Rule on the record producer table to find the attachment via the table_sys_id and update the table_name via glideRecord update. Also, what would you do if there are multiple attachments? Here's an example of a script include, and it's working well for me.

 

 

var AssociateCaseAttachment = Class.create();
AssociateCaseAttachment.prototype = {
initialize: function() {},
updateCaseAttachment: function(sys_id) {
if (!sys_id) {
return;
}
var grAttach = new GlideRecord('sys_attachment');
grAttach.setLimit(10);
grAttach.addEncodedQuery('table_sys_id=' + sys_id);
grAttach.query();
while (grAttach.next()) {
grAttach.table_name = 'YOUR Table NAME';
grAttach.update();
}

},

type: 'AssociateCaseAttachment'
};

 

Hello Brian, this use case was for an attachment field on a record producer. When an attachment is uploaded to SN, it gives it the generic table name and the predefined sys_id for the record to be created from the SC Catalog Item widget. On submission of the form, you can use the sys_id of the attachment (captured in the attachment field) to look distinctly at that record and just point it to the table you are submitting the form too. There wasn't a need to look for multiple attachments since we are using the primary ID for the table. Hope that helps.

Query1
Tera Contributor

Hi, I tried below code for this case, the attachment table name is showing correctly i.e sc_req_item in sys_attachment.  but the attachment is either corrupt/showing blank file. could you please help?

 

I've added this code in record producer script part:

 

var gr = new GlideRecord('sys_attachment');
if(gr.get(producer.Attachment_field_name)){ 
gr.table_name='sc_req_item'; //copy to table name
gr.table_sys_id=current.sys_id;//copy to record sys_id
gr.insert();
}