The CreatorCon Call for Content is officially open! Get started here.

Copy attachment from incident to problem

nehasingh98
Tera Contributor

Hi Expert

 

Can someone help me with script to copy attachment from incident to problem while creating. I am creating problem via UI action.

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @nehasingh98 ,

 

You can use below script in your after insert BR.

(function executeRule(current, previous /*null when async*/) {

    var SysId = current.incident; 
    if (SysId ) {
      
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_sys_id', SysId); 
        attachmentGR.addQuery('table_name', 'incident'); // RITM's table name
        attachmentGR.query();
        while (attachmentGR.next()) {
           
            var attachmentSysId = new GlideSysAttachment().copy(
                attachmentGR.getValue('table_sys_id'),
                'incident', 
                current.getValue('sys_id'),
                'problem' 
            );
            
        }
    }
})(current, previous);

 

For details explanation you can also visit here: https://servicenowwithrunjay.com/copy-attachment-from-one-table-to-another/

 

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

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

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

 

View solution in original post

4 REPLIES 4

Runjay Patel
Giga Sage

Hi @nehasingh98 ,

 

You can use below script in your after insert BR.

(function executeRule(current, previous /*null when async*/) {

    var SysId = current.incident; 
    if (SysId ) {
      
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_sys_id', SysId); 
        attachmentGR.addQuery('table_name', 'incident'); // RITM's table name
        attachmentGR.query();
        while (attachmentGR.next()) {
           
            var attachmentSysId = new GlideSysAttachment().copy(
                attachmentGR.getValue('table_sys_id'),
                'incident', 
                current.getValue('sys_id'),
                'problem' 
            );
            
        }
    }
})(current, previous);

 

For details explanation you can also visit here: https://servicenowwithrunjay.com/copy-attachment-from-one-table-to-another/

 

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

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

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

 

Valmik Patil1
Kilo Sage

Hello @nehasingh98 ,

 

Try creating business rule with below configurations

Business rule: Copy Incident attachment into Problem

 

after

 

table : sys_attachment

 

insert, update

 

Script:

var atta = new GlideRecord('problem');
atta.addQuery('parent', current.table_sys_id);
atta.query();
if(atta.next()){
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', atta.sys_id);
gr.deleteMultiple();
GlideSysAttachment.copy('incident', current.table_sys_id, 'problem', atta.sys_id);
current.update();
}

Thanks,

Valmik Patil

Harish Bainsla
Kilo Patron
Kilo Patron

yuvarajkate
Giga Guru

Try using this script. See if it works for your use case.

var problem = new GlideRecord('problem');
problem.initialize();
problem.short_description = 'Problem created from Incident: ' + current.number;
problem.description = current.description; 
problem.incident_ref = current.sys_id; //Store a reference to the Incident
var problemSysId = problem.insert(); 

if (problemSysId) {
    // Copy attachments from Incident to Problem
    var attachment = new GlideRecord('sys_attachment');
    attachment.addQuery('table_name', 'incident'); 
    attachment.addQuery('table_sys_id', current.sys_id); 
    attachment.query();

    while (attachment.next()) {
        var newAttachment = new GlideSysAttachment();
        newAttachment.copy(attachment.sys_id, 'problem', problemSysId);
    }
    action.setRedirectURL(problem);
} else {
    gs.addErrorMessage('Failed to create Problem record.');
}