attachement record procedure

sakila
Tera Contributor

Hello

 

i tried to attach the attachment in record procedure using incident form. not able to getting attached the attachment incident ticket

sakila_0-1725525247656.png

but not added in top of the incident 

sakila_1-1725525651829.png

i used this script in inside script record procedure

sakila_2-1725525703390.png

 

 

2 REPLIES 2

jaswalvishal106
Tera Expert

Hello @sakila  I would request you to update the spellings like attachment, record producer so that it will be clear as what you are looking for. Please use below script:

var incGR = new GlideRecord('incident');
incGR.initialize();

//Please add if there are more fields as per your requirement
incGR.short_description = current.short_description; 
incGR.caller_id = gs.getUserID(); 

// Insert the new incident record
var incSysId = incGR.insert();
if (incSysId) {
   // Check if there are any attachments uploaded with the record producer
   var attachmentGR = new GlideRecord('sys_attachment');
   attachmentGR.addQuery('table_name', current.getTableName());
   attachmentGR.addQuery('table_sys_id', current.sys_id); // The sys_id of the record producer
   attachmentGR.query();
   // Loop through each attachment and attach it to the incident
   while (attachmentGR.next()) {
       var attachment = new GlideSysAttachment();
       attachment.copy(attachmentGR, 'incident', incSysId); // Attach to the incident
   }
   // Showing confirmation message and recdirecting to incident created
   gs.addInfoMessage('Incident created successfully: ' + incGR.number);
   current.setRedirect(true);
} else {
   gs.addErrorMessage('Failed to create incident.');
}

Adrian Ubeda
Mega Sage
Mega Sage

Hello, 

Try to use the GlideSysAttachment API for handling attachments, here's a example about how to copy an attachment from an incident: 

var attachment = new GlideSysAttachment();
var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
var incGR = new GlideRecord('incident');
incGR.get(incidentSysID);

var copiedAttachments = attachment.copy('incident', incidentSysID, 'problem', incGR.getValue('problem_id'));
gs.info('Copied attachments: ' + copiedAttachments);

Check this: https://docs.servicenow.com/bundle/vancouver-api-reference/page/app-store/dev_portal/API_reference/G...

 

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆