- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 08:13 AM
Hi there,
I need to copy the picture field image from catalog item onto the content item using background script. The name of catalog item & content item onto which it is to be copied have same name. So i have to perform this copy picture on 91 content items.
means copy the picture from 91 catalog items onto the 91 content items where the name is same.
eg: catalog1 name: AAAA content item 1 name: AAAA
Tested with below 2 scripts but it dint work as i need.
var records = [ 'BMWAgile Toolchain'];
var catalogItemCount=0;
var contentItemCount=0;
var CI_catalog, CI_category, CI_available_for;
for (var i = 0; i < records.length; i++)
{
var gr_item = new GlideRecord('sc_cat_item');
gr_item.addQuery("name",records[i]);
gr_item.addQuery("sys_class_name","sc_cat_item");
gr_item.query();
if(gr_item.next())
{
gs.log("two");
var picture = gr_item.picture;
//Get the target catalog item
var gr_content = new GlideRecord('sc_cat_item');
gr_content.addQuery("name",records[i]);
gr_content.addQuery("sys_class_name","sc_cat_item_content");
gr_content.query();
if(gr_content.next())
{
gs.print("three");
// Set the picture in the target catalog item
gr_content.picture = picture;
gr_content.update();
}
}
}
with above code, no record gets created in attachment table. but just image is copied which later cannot be modified manually also.
// Define the sys_ids of the source and target catalog items
var sourceItemSysID = 'b443d928975935101f8af0f6f053af18'; // Replace with actual sys_id
var targetItemSysID = '448fe8189795b1101f8af0f6f053af5f'; // Replace with actual sys_id
// Get the source catalog item
var sourceItem = new GlideRecord('sc_cat_item');
if (sourceItem.get(sourceItemSysID)) {
// Get the picture from the source catalog item
var picture = sourceItem.picture;
// Get the target catalog item
var targetItem = new GlideRecord('sc_cat_item');
if (targetItem.get(targetItemSysID)) {
// Set the picture in the target catalog item
targetItem.picture = picture;
targetItem.update();
gs.info('Picture copied successfully.');
} else {
gs.error('Target catalog item not found.');
}
}
by above code attachment record is created but on content item, it shows broken image.
Anyone help with background script please.
Regards,
Snehal
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 08:35 AM
please use this; this script worked for me.
I just did for 1; you can enhance it for your requirement
// Define the sys_ids of the source and target catalog items
var sourceItemSysID = 'b443d928975935101f8af0f6f053af18'; // Replace with actual sys_id
var targetItemSysID = '448fe8189795b1101f8af0f6f053af5f'; // Replace with actual sys_id
// Get the source catalog item
var sourceItem = new GlideRecord('sc_cat_item');
if (sourceItem.get(sourceItemSysID)) {
// Get the target catalog item
var targetItem = new GlideRecord('sc_cat_item');
if (targetItem.get(targetItemSysID)) {
GlideSysAttachment.copy('sc_cat_item', sourceItemSysID, 'sc_cat_item', targetItemSysID);
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", targetItemSysID);
gr.query();
if (gr.next()) {
gr.table_name = 'ZZ_YYsc_cat_item';
var sysId = gr.update();
targetItem.picture = sysId;
targetItem.update();
gs.info('Picture copied successfully.');
}
} else {
gs.error('Target catalog item not found.');
}
}
Output: Working fine
If my response helped please mark it correct and close the thread so that it benefits future readers.
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-18-2023 08:25 AM
Refer this link to copy image from one table to another table field
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 08:35 AM
please use this; this script worked for me.
I just did for 1; you can enhance it for your requirement
// Define the sys_ids of the source and target catalog items
var sourceItemSysID = 'b443d928975935101f8af0f6f053af18'; // Replace with actual sys_id
var targetItemSysID = '448fe8189795b1101f8af0f6f053af5f'; // Replace with actual sys_id
// Get the source catalog item
var sourceItem = new GlideRecord('sc_cat_item');
if (sourceItem.get(sourceItemSysID)) {
// Get the target catalog item
var targetItem = new GlideRecord('sc_cat_item');
if (targetItem.get(targetItemSysID)) {
GlideSysAttachment.copy('sc_cat_item', sourceItemSysID, 'sc_cat_item', targetItemSysID);
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", targetItemSysID);
gr.query();
if (gr.next()) {
gr.table_name = 'ZZ_YYsc_cat_item';
var sysId = gr.update();
targetItem.picture = sysId;
targetItem.update();
gs.info('Picture copied successfully.');
}
} else {
gs.error('Target catalog item not found.');
}
}
Output: Working fine
If my response helped please mark it correct and close the thread so that it benefits future readers.
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-19-2023 06:50 AM
It worked as expected. Thank you ankur.
Regards,
Snehal