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

Runjay Patel
Giga Sage

Hi @NAIDILE S ,

 

In UI Action you can try below code.

 var grIncident = new GlideRecord('sn_si_incident'); // Table name for Security Incident
    grIncident.initialize();
    grIncident.short_description = current.short_description;
    grIncident.description = current.description;
    grIncident.insert();

    // Copy attachments
    var attachment = new GlideSysAttachment();
    attachment.copy('security_request', current.sys_id, 'sn_si_incident', grIncident.sys_id);

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Thanks for the solution,using BR it worked for me

Hi @NAIDILE S ,

 

JFI, Writing BR is not the best solution, BR will run on every new insert. 

 

 

Hi, 
UI action which is present is OOTB and we cannot customize it. so went with the BR

and for every insert of SIR(security incident) from Security Request(from UI action "convert to security incident) a new SIR will be created