Copy of attachment from variable to record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi,
I have a variable type attachment on my catalog form. Post submission of request I want to attach that attachment to the requested item so that it shows on the top of the ritm, user doesn't need to scroll down and see for variable attachment
This is what I have tried and it is not working as expected it does copy but every time it copied to ZZ_YYsc_req_item instead sc_req_item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi @Tanya10 ,
Please create a business rule on Requested Item table.
When = async
On Insert
Script:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "ZZ_YY" + current.getTableName());
gr.addQuery("table_sys_id", current.sys_id);
gr.query();
while (gr.next()) {
gr.table_name = current.getTableName();
gr.update();
new global.VariableUtil().copy(gr.sys_id, current.getTableName(), current.sys_id);
}
})(current, previous);
Please test and let me know if you run into any issues
If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
54m ago
Hi @Tanya10 ,
i have updated few lines please check if this is working -
var ritmSysId = '72e1e6fbc30d3e5026b5652599013189'; // RITM sys_id
var attachmentVariableSysId = 'ca5cbd8cc3e3a21026b56525990131c6'; // Attachment variable sys_id
// Get the variable mapping for this RITM
var itemVars = new GlideRecord('sc_item_option_mtom');
itemVars.addQuery('request_item', ritmSysId);
itemVars.query();
while (itemVars.next()) {
var itemOption = itemVars.sc_item_option; // sc_item_option record
var variableDef = itemOption.item_option_new; // question definition sys_id
// Check if this is the attachment variable
if (variableDef == attachmentVariableSysId) {
gs.print('Found attachment variable: ' + variableDef);
// Get attachments linked to the variable (table = ZZ_YYsc_item_option)
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'ZZ_YYsc_item_option'); // Correct table for variable attachments
attachmentGR.addQuery('table_sys_id', itemOption); // sys_id of sc_item_option record
attachmentGR.query();
while (attachmentGR.next()) {
gs.print('Copying attachment: ' + attachmentGR.file_name);
// Copy attachment to RITM (sc_req_item)
var newSysId = new GlideSysAttachment().copy(
attachmentGR.getTableName(),
attachmentGR.getValue('table_sys_id'),
'sc_req_item',
ritmSysId
);
gs.print('Copied attachment to RITM: ' + newSysId);
}
}
}
NOTE -
Source Table for Attachments
Attachments for variable fields are stored under ZZ_YYsc_item_option (not ZZ_YYsc_req_item).Source Sys ID
Use itemOption (sys_id of sc_item_option) for table_sys_id when querying attachments.Copy Logic
Use attachmentGR.getTableName() and attachmentGR.getValue('table_sys_id') to copy from the actual attachment record.
Please mark as correct and close the thread if this is helpfull.
Thanks,
Rithika.ch
