- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2024 09:10 PM
Hi Expert
Can someone help me with script to copy attachment from incident to problem while creating. I am creating problem via UI action.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2024 09:15 PM
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
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2024 09:15 PM
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
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2024 09:19 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2024 09:21 PM
Hi @nehasingh98 you can use flow designer for it check below link
or you can refer below link
if my answer helps you mark helpful and accept solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2024 10:12 PM
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.');
}