Add attachment to incident Record Producer on Service Portal via client script?

rita_m
Tera Guru

Hi - 

 

I have a need to attach an already-existing sys_attachment record in ServiceNow to a Service Portal Record Producer (incident) on page load. 

 

I have access to the attachment's sys_id in the client script of the record producer, and I could get any other portion of the attachment data if needed. (Name, content type, etc)

 

I would be happy to either copy the already existing attachment or to use the one already existing in SN whose sys-id I have, but each particular attachment would just be needed once, so using the already existing one would be preferable.

 

What would be the best way to do this?  Is there already-existing functionality in SN, or would it involve DOM manipulation?

 

 

rita_m_1-1698105026138.png

I just need to auto-add this HERE, and it is apparently NOT easy.

Any advice appreciated!

 

Thanks,

-R

 

3 REPLIES 3

Tony Chatfield1
Kilo Patron

Hi, I would look at GlideAjax in your onLoad script, with the script-include utilizing OOB GlideSysAttachment copy; and the source and destination sys_id's being passed in as glideajax parameters - you could also pass in the table names, allowing the script to be reused outside of the initial record producer context, or just hard code them if they are constants.

Abhijeet_Pawar
Tera Guru

Hello @rita_m ,

To attach an already-existing sys_attachment record to a Service Portal Record Producer (incident) on page load, you can use a GlideRecord query in a client script to fetch the attachment and then use the GlideSysAttachment API to copy the attachment to the new record.

Create a client script for the record producer with below code:

var ga = new GlideAjax('CopyAttachment');
ga.addParam('sysparm_name', 'copyAttachment');
ga.addParam('sysparm_attachment_sys_id', 'sys_id_of_attachment');
ga.addParam('sysparm_new_record_sys_id', 'sys_id_of_new_record');
ga.getXML();

Now write a script include:

var CopyAttachment = Class.create();
CopyAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
copyAttachment: function() {
var attachmentSysId = this.getParameter('sysparm_attachment_sys_id');
var newRecordSysId = this.getParameter('sysparm_new_record_sys_id');

var gr = new GlideRecord('sys_attachment');
if (gr.get(attachmentSysId)) {
var sa = new GlideSysAttachment();
sa.copy('incident', newRecordSysId, gr.getValue('table_name'), gr.getValue('table_sys_id'));
}
},

type: 'CopyAttachment'
});

Please replace 'sys_id_of_attachment' and 'sys_id_of_new_record' with the actual sys_ids. Also, replace 'table_name_of_new_record' with the actual table name of the new record.

If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 

Thanks and Regards,
Abhijeet Pawar.

Hi Abhijeet - 

 

Thanks for your reply, but there are some issues with going this route.  Maybe you know a workaround I don't?

 

First, it's apparently very tricky to get the sys_id of the new record that the record producer will create before submit.  I'd apparently have to modify the SC Catalog Item widget to make it visible.  

(https://www.servicenow.com/community/developer-forum/service-portal-record-producers-do-they-instant...

This is certainly possible, but it's not something worth doing unless there's some sort of solution for the second issue.

 

The second issue is that if I add an attachment via this method, it will appear in the incident AFTER the record producer creates it, but it will not show up as an attachment in the record producer catalog item BEFORE submit like I was hoping.  This means that the user will not be able to review all aspects of the ticket they're about to submit.  They may get confused because they're expecting to see an attachment and there isn't one.  If they expect the attachment to just appear after submit, what if there's an error at some point and it doesn't correctly attach? I have tested that the attachment doesn't show up before submit by 1) adding 1 attachment on the record producer before submit to get the sys_id of the soon-to-be-created incident, and 2) I then modified the related record sys_id of another attachment record to match that soon-to-be-created incident's sys_id and confirmed the record producer did not have more attachments listed.  After the record producer was submitted, I confirmed that the new incident had both attachments attached.

 

If it's not going to be available to be visible as an attachment before submit, I think it'd be easier to just add the attachment to the incident via the Record Producer's script field (which is run server side) and skip the ajax call. 

 

I do have a "backup method", but it's not perfect either. I could create a new record producer variable of type "attachment", and put the sys_id of the already-existing attachment in that field with an onload script, and set it to hide if it had no values.  There would then be the attachment paperclip at the bottom of the form which would be empty even if there's an attachment, which may be confusing, but at least they could also see that there will be an attachment added to the incident on submit if they look at the field. I'd need to use the record producer's script field to move that attachment to the incident on submit, but that's doable.

 

But just adding it to the bottom attachment paperclip would be BETTER, so if you've any ideas please let me know!

 

Thanks,

-Rita