- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 11:52 PM
Hello Team
When attachment is added to sc task we need to copy it to ritm.
but only currently inserted file/files,not those inserted yesterday and so on
any advice on script in BR on attachment table.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 11:30 PM
Hello @Petr Pastuszek1 ,
Please try with the modified code below,
(function executeRule(current, previous /*null when async*/) {
// Get the current task record
var grTask = new GlideRecord('sc_task');
if (grTask.get(current.table_sys_id)) {
// Create a new sys_attachment record
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.initialize();
// Set values for the new attachment record
grAttachment.setValue('file_name', current.file_name);
grAttachment.setValue('content_type', current.content_type);
grAttachment.setValue('size_bytes', current.size_bytes);
grAttachment.setValue('table_name', 'sc_req_item');
grAttachment.setValue('table_sys_id', grTask.request_item);
// Insert the new attachment record
var newAttachmentSysId = grAttachment.insert();
// Copy the content stream from the original attachment to the new attachment
var gsa = new GlideSysAttachment();
gsa.writeContentStream(
grAttachment,
current.file_name,
current.content_type,
gsa.getContentStream(current.sys_id)
);
// Optionally, log the new attachment sys_id for debugging
gs.info('New Attachment Sys ID: ' + newAttachmentSysId);
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 12:10 AM
Hi @Petr Pastuszek1 ,
As already suggested by our colleagues to use GlideSysAttachment ,you can try that but if you have already tried something we would request you to paste the code you are trying to execute.
Please mark correct/helpful if my response helped you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 07:27 AM
Hi @Petr Pastuszek1 ,
Can you try this in a BR:
Table - Catalog Task
When to Run - Before Update
Scripts -
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var copy = new GlideRecord('sc_req_item');
copy.get(<field name of RITM on sctask table>);
if (copy.next()) {
GlideSysAttachment.copy('sc_task', current.getUniqueValue(), 'sc_req_item', copy.getUniqueValue());
}
})(current, previous);
Please mark helpful/correct if my response helped you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 07:42 AM
Hello, the code doesnt work, I modified 'copy.get' part and doesnt work.
I was able today to figure out solution this way, could anyone confirm this is ok technically?
It is working really good and as expected, doesnt create duplicates ...
var grTask = new GlideRecord('sc_task');
grTask.get(current.table_sys_id);
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.initialize();
grAttachment.setValue('file_name',current.file_name);
grAttachment.setValue('content_type',current.content_type);
grAttachment.setValue('size_bytes',current.size_bytes);
grAttachment.setValue('table_name','sc_req_item');
grAttachment.setValue('table_sys_id', grTask.request_item);
grAttachment.insert();/Petr
