Help with background script to copy picture field from catalog item onto content item picture field

Snehal Madakatt
Mega Sage

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

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Snehal Madakatt 

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

image copied.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

palanikumar
Mega Sage

Refer this link to copy image from one table to another table field

https://www.servicenow.com/community/developer-forum/copy-image-field-from-one-form-field-to-order-r... 

Thank you,
Palani

Ankur Bawiskar
Tera Patron
Tera Patron

@Snehal Madakatt 

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

image copied.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

It worked as expected. Thank you ankur.

 

Regards,

Snehal