Skip to content

Commit 986ee44

Browse files
Bulk update catalog item images (#974)
1 parent 9069f34 commit 986ee44

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Bulk Catalog Item Image Change
2+
3+
Updates the image(s) associated with catalog items or record producers
4+
5+
## Description
6+
7+
This script can perform bulk updates of the picture and/or icon fields used in catalog items and record producers. The script will update each item that is returned in the query with the images set
8+
in the iconSysId and pictureSysId variables. A business use for this script would be the need to update the picture and icon fields of all Apple related catalog items with an new Apple logo image.
9+
10+
## Getting Started
11+
12+
### Dependencies
13+
14+
* Must be in the Global scope.
15+
* The image(s) being used must already exist in the db_image table.
16+
17+
### Execution
18+
19+
1. Copy the script from bulk-update-cat-item-images.js to either a fix script or background script in your ServiceNow instance.
20+
2. Copy the sys_id(s) from the image that you will using from the sys_attachments table. You can use the same image for both fields or different images, it's up to you. If you have added this image using the db_image table, you can filter the sys_attachments table name column to ZZ_YYdb_image to make it easier to find.
21+
3. Update the script variables as follows:
22+
* **table**: the table you want to change the icons for the catalog item(s) - commonly sc_cat_item or sc_cat_item_producer
23+
* **iconSysId**: the sys_id of the image to be used for the icon (NOTE - you can comment this line out if you don't want to update the icon)
24+
* **pictureSysId**: the sys_id of the image to be used for the picture (NOTE - you can comment this line out if you don't want to update the picture)
25+
* **query**: the encoded query you will use to identify the items to be updated
26+
4. Run the script and the picture and/or icon fields will be updated
27+
28+
## Authors
29+
30+
Brad Warman
31+
32+
https://www.servicenow.com/community/user/viewprofilepage/user-id/80167
33+
34+
## Version History
35+
36+
* 0.1
37+
* Initial Release

0 commit comments

Comments
 (0)