Copy Attachment

NAIDILE S
Tera Contributor

Hi,

I have a table where i want to copy the attachment to Security incident from Security request.

In Security request we have a UI action called " convert to security incident" once it is converted Security incident will be created and the attachment to be copied over.

I tried using After -insert BR.  but its not working . ANywhere im goin wrong? can anyone suggest on this 

Here is the code which i tried

var sourceTable = "sn_si_request";
var targetTable = "sn_si_incident";

var attachment = new GlideSysAttachment();
 attachment.copy(sourceTable,current.sys_id, targetTable,current.sys_id);

 

 

1 ACCEPTED SOLUTION

Pooja2998
Mega Sage

Hello @NAIDILE S ,
Please try This After insert Business Rule:

(function executeRule(current, previous /*null when async*/) {
    if (current.parent) {
        var parentSysid = current.parent.toString();
        gs.addInfoMessage("Parent sys_id: " + parentSysid);
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_sys_id', parentSysid);
        attachmentGR.query();
        var attachmentCount = attachmentGR.getRowCount();
        gs.addInfoMessage("Number of attachments" + attachmentCount);

        if (attachmentCount > 0) {
            while (attachmentGR.next()) {
                GlideSysAttachment.copy('sn_si_scan_request', parentSysid, 'sn_si_incident', current.sys_id);
            }
        }
    }
})(current, previous);

If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Pooja.

View solution in original post

8 REPLIES 8

Brad Bowman
Kilo Patron
Kilo Patron

Since your Business Rule is running on the sn_si_incident table, 'current.sys_id' will be the sys_id of the incident record, so it is correct to use it as the 4th parameter in the copy method.  The second parameter needs to be the sys_id of the request record.  Luckily, when the UI Action is used, the parent field on the incident is populated with a reference to the request, so you can use

 

 attachment.copy(sourceTable, current.parent, targetTable, current.sys_id);

I wouldn't recommend altering the out of box UI Action as you will miss out on any updates that are made to the script or condition in future releases.  You especially don't want to add code that creates another incident when the out of box script already does that nicely, linking the two records, assigning the incident, and displaying an ifo message with the link to the new incident.

 

Juhi Poddar
Kilo Patron

Hello @NAIDILE S 

  • There is no need to create separate BR as this will run even when new record is inserted without click of UI button.
  • Try to copy the attachment when record is created.

Please refer to the script below:

// Ensure this script is added to the "Convert to Security Incident" UI Action

// Create the Security Incident record
var incidentGr = new GlideRecord("sn_si_incident");
incidentGr.initialize();
incidentGr.short_description = current.short_description; // Copy fields as needed
incidentGr.description = current.description;
incidentGr.some_field = current.some_field; // Copy other fields if needed
incidentGr.insert(); // Insert the new Security Incident record

// Copy attachments from the Security Request to the new Security Incident
var attachment = new GlideSysAttachment();
try {
    attachment.copy("sn_si_request", current.sys_id, "sn_si_incident", incidentGr.sys_id);
    gs.addInfoMessage("Attachments copied successfully to the new Security Incident.");
} catch (e) {
    gs.addErrorMessage("Error copying attachments: " + e.message);
}

// Redirect to the new Security Incident (optional)
action.setRedirectURL(incidentGr);

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

 

Thanks for the solution, using BR it worked for me

Pooja2998
Mega Sage

Hello @NAIDILE S ,
Please try This After insert Business Rule:

(function executeRule(current, previous /*null when async*/) {
    if (current.parent) {
        var parentSysid = current.parent.toString();
        gs.addInfoMessage("Parent sys_id: " + parentSysid);
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_sys_id', parentSysid);
        attachmentGR.query();
        var attachmentCount = attachmentGR.getRowCount();
        gs.addInfoMessage("Number of attachments" + attachmentCount);

        if (attachmentCount > 0) {
            while (attachmentGR.next()) {
                GlideSysAttachment.copy('sn_si_scan_request', parentSysid, 'sn_si_incident', current.sys_id);
            }
        }
    }
})(current, previous);

If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Pooja.