|
| 1 | +var table = 'sc_cat_item'; |
| 2 | +var iconSysId = 'f1dd1def1ba9fd1086a098e7b04bcb81'; // sys_id of icon file |
| 3 | +var pictureSysId = 'f1dd1def1ba9fd1086a098e7b04bcb81'; // sys_id of picture file |
| 4 | +var query = 'category=6a6ab100dbd5cc10ee115d87f49619fb^active!=true'; // query used to identify items |
| 5 | + |
| 6 | +var grItem = new GlideRecord(table); |
| 7 | +grItem.addEncodedQuery(query); |
| 8 | +grItem.query(); |
| 9 | +while (grItem.next()) { |
| 10 | + createImage(pictureSysId, 'picture', table, grItem.sys_id); |
| 11 | + createImage(iconSysId, 'icon', table, grItem.sys_id); |
| 12 | +} |
| 13 | + |
| 14 | +function createImage(attachmentID, fieldName, tableName, tableID) { |
| 15 | + |
| 16 | + var attachmentGR = new GlideRecord('sys_attachment'); |
| 17 | + attachmentGR.get(attachmentID); |
| 18 | + var fields = attachmentGR.getFields(); |
| 19 | + var imageGR = new GlideRecord('sys_attachment'); |
| 20 | + imageGR.initialize(); |
| 21 | + imageGR.compressed = attachmentGR.compressed; |
| 22 | + imageGR.content_type = 'image/png'; |
| 23 | + imageGR.size_bites = attachmentGR.size_bites; |
| 24 | + imageGR.size_compressed = attachmentGR.size_compressed; |
| 25 | + imageGR.file_name = fieldName; |
| 26 | + imageGR.table_name = 'ZZ_YY' + tableName; |
| 27 | + imageGR.table_sys_id = tableID; |
| 28 | + imageGR.state = '2'; |
| 29 | + var imageID = imageGR.insert(); |
| 30 | + |
| 31 | + copyAttachmentContent(attachmentID, imageID); |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +/* |
| 36 | + oldID: sys_id of existing attachment |
| 37 | + newID: sys_id of newly created attachment |
| 38 | + */ |
| 39 | +function copyAttachmentContent(oldID, newID) { |
| 40 | + var oldGR = new GlideRecord('sys_attachment_doc'); |
| 41 | + oldGR.addQuery('sys_attachment', oldID); |
| 42 | + oldGR.query(); |
| 43 | + while (oldGR.next()) { |
| 44 | + var newGR = new GlideRecord('sys_attachment_doc'); |
| 45 | + newGR.initialize(); |
| 46 | + newGR.data = oldGR.data; |
| 47 | + newGR.length = oldGR.length; |
| 48 | + newGR.position = oldGR.position; |
| 49 | + newGR.sys_attachment = newID; |
| 50 | + newGR.insert(); |
| 51 | + } |
| 52 | +} |
0 commit comments