- 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 04:26 AM
Hello and thank you,
tested code and problem is that it control data created on today.
If I insert attachment 'A' now and 'B' 1 min later, end result is that RITM will have these attachments: A + B + B and this is not wanted.
is there really no way to copy only those which are currently inserted (current object) without need to query table?
/Petr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 07:43 AM
This code which I have done is working well, could you confirm it is ok technically?
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 07:56 AM
Taking this back.
working well, but when its copied, its not usable. Its not real e.g. image object, when I download it to local disc, it doesnt open as it missing something from original file
Still trying to figure out next step.
- 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-06-2024 04:27 AM
Thank you, I used your code and updated below way which is after many tests working really as expected.
It will copy always only currently uploaded attachment or attachments from ritm to sc_task. Thank you
/Petr
(function executeRule(current, previous /*null when async*/) {
/******************************************************
* Access task into which attachment has been uploaded
*****************************************************/
var grTask = new GlideRecord('sc_task');
if (grTask.get(current.table_sys_id)) {
/******************************************************************
* Access RITM to which attachment will be copied from above task
******************************************************************/
var ritm = new GlideRecord('sc_req_item');
ritm.get(grTask.request_item);
/*******************************************************************************************
* Method to copy attachment from sc_task to RITM
* Do not use other method because it will copy all attachments and not currently uploded
*******************************************************************************************/
var targetGlideRecord = ritm;
var fileName = current.file_name;
var contentType = current.content_type;
var sourceAttachmentSysId = current.sys_id;
var gsa = new GlideSysAttachment();
gsa.writeContentStream(
targetGlideRecord,
fileName,
contentType,
gsa.getContentStream(current.sys_id)
);
}
})(current, previous);
