Not getting sys id of the attachment when using g_form.getValue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2022 08:11 PM
Hi. I need to grab the sys id of the submitted Attachment variable so that I can push the same image ( submitted by the user) to sc_cat item table.
I am not getting the sys_id of the attachment variable. It is coming blank when I am using getValue method. pls suggest.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2022 10:19 PM
Hi,
so user submits catalog item with 2 files 1 for picture and other for icon
these 2 should be copied to Catalog item's picture and icon field
Then you can use GlideRecord in after insert BR on RITM table and copy the file
check this and enhance
How to copy attachment from one File attachment field to another table
regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2022 10:27 PM
Ankur,
what I am not understanding is, why are we inserting the already inserted picture in sys_attachment Table again?
function copySingleAttachment(targetSysId, sourceAttSysId) {
var attRec;
var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', sourceAttSysId);
gr.query();
if (gr.next()) {
var gr1 = new GlideRecord('sys_attachment');
gr1.initialize();
gr1.file_name = gr.file_name;
gr1.content_type = gr.content_type;
gr1.compressed = gr.compressed;
gr1.table_name = 'sc_task';
gr1.size_bytes = gr.size_bytes;
gr1.size_compressed = gr.size_compressed;
gr1.table_sys_id = targetSysId;
attRec = gr1.insert();
}
var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', gr.sys_id);
attDoc.query();
while (attDoc.next()) {
var attDocCopy = new GlideRecord('sys_attachment_doc');
attDocCopy.initialize();
attDocCopy.sys_attachment = attRec;
attDocCopy.position = attDoc.position;
attDocCopy.length = attDoc.length;
attDocCopy.data = attDoc.data;
attDocCopy.insert();
}
}
var tableName = '<table name to from from>';
var fromSysId = '<sys_id of record>';
var fieldName = '<name of field>';
var targetSysId = 'f745ef5e974c111086d3b4b3f153af4a'; // sys_id of record to copy attachment
var grTable = new GlideRecord(tableName);
grTable.addQuery('sys_id', fromSysId);
grTable.query();
if (grTable.next()) {
var sysId = grTable.getValue(fieldName);
copySingleAttachment(targetSysId, sysId);
}
When the user submits the RITM, as Task is generated and I see same time picture is inserted in sys_attachment Table as well..
Please correct me if I am wrong.. what I am supposed to do is to grab the sys_id of the pictures from sys_attachment and push into sc_cat_item Table.??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2022 10:38 PM
Hi,
you need to copy the file from attachment variable to field of type attachment on sc_cat_item record
Did you check the link I shared above?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2022 10:41 PM
Yes that's my ask.. to copy the attachment to sc_cat_item record. Yes I checked the link you shared and the code I pasted above is from that link only.
My question is why we are again inserting picture into sys_attachment and sys_attachment_doc table ( In my previous reply to you, I have highlighted that code)?? The attachment is already present in both the Tables, why are we writing to insert there? while all we need is to insert into sc_cat_item Table?
Also, I am not even getting sys_id of the attachment variable from submitted catalog Item task ( is it because my business rule is on Task level while sys id is available at RITM Level??) I am just getting name of the attachment variable by using getDisplayValue .. getValue is returning nothing..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2022 10:55 PM
Hi,
the file is present on the RITM variable and hence it's linked with RITM sysId in sys_attachment.
In order to copy them to sc_cat_item you need to write script
what's your exact requirement? to copy files to sc_cat_item right?
then why not use after insert BR on RITM table?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader