How to add an image to a record via a background script?

Jared Wason
Tera Guru

Hi,

I have a custom table with office equipment records. I want to update the records with an image. How can I do that via a script since it is way to many records to do manually? Below is the scripting I have tried but with no success.

var count = 0;

	var pic = new GlideRecord('db_image');
	var gr = new GlideRecord("u_office_equipment");
	gr.addQuery('u_display_name=AIR CONDITIONER, PORTABLE');
	gr.query();
	while(gr.next()){ 
		if(pic.get('fe3880b5c3022100910900251eba8f72')){
			//gr.u_image = '';
			//gr.u_image = 'fe3880b5c3022100910900251eba8f72'; //sys_id of db_image record 
			gr.u_image = pic.getDisplayValue('image');
			count++;
			gr.update();
		}
		
	}

gs.log('Done. Updating ' + count + ' records.');

 

1 ACCEPTED SOLUTION

Jared in my first response I tried to use your script and perform the job which you were trying to do.

The link which I provided has the solution of OOB functionality of copying the attachment from one record to another.

So, with the help of the provided link I came upto a workaround for you, You need to attach the image to one record manually and then you can run the below script and all the attachments of the manually updated record will be copied to the others.

var count = 0;
var gr = new GlideRecord("u_office_equipment");
gr.addQuery('u_display_name=AIR CONDITIONER', 'PORTABLE');
gr.addQuery('sys_id', '!=', '551cc707db891010996e6a4948961997');
gr.query();
while (gr.next()) {
    GlideSysAttachment.copy("u_office_equipment", "551cc707db891010996e6a4948961997", "u_office_equipment", gr.sys_id.toString());
    count++;
}

gs.log('Done. Updating ' + count + ' records.');

Replace 551cc707db891010996e6a4948961997 with the sys id of the record which is manually updated.

https://hi.service-now.com/kb_view.do?sysparm_article=KB0720062

 

Note:- Including the field image other attachments will also be copied with the above script (GlideSysAttachment.copy("ZZ_YYincident", "<Table sys ID>", "ZZ_YYincident", current.sys_id);).

Kindly mark my answer as Correct and helpful based on the Impact.

Regards,

Alok

View solution in original post

9 REPLIES 9

sachin_namjoshi
Kilo Patron
Kilo Patron

Use below code

var count = 0;

	var pic = new GlideRecord('db_image');
        pic.addQuery('sys_id',"fe3880b5c3022100910900251eba8f72");
        pic.query();
        while(pic.next()){
	var gr = new GlideRecord("u_office_equipment");
	gr.addQuery('u_display_name=AIR CONDITIONER, PORTABLE');
	gr.query();
	while(gr.next()){ 
		
			gr.u_image = pic.sys_id;
			gr.update();
		}
		count++;
	}

}
gs.log('Done. Updating ' + count + ' records.');

Regards,

Sachin

Thanks for the response Sachin. I tried your script but that is only putting the sys_id value in the image field, it is not displaying the actual picture. 

You will reference of sys_attachment to update image.

Below article will give you sample script

 

https://hi.service-now.com/kb_view.do?sysparm_article=KB0597756

 

Regards,

Sachin

 

 

Alok Das
Tera Guru

Hi Jared,

Could you please give a try with below code:

var count = 0;

var pic = new GlideRecord('db_image');
if (pic.get('fe3880b5c3022100910900251eba8f72')) {
    var gr = new GlideRecord("u_office_equipment");
    gr.addQuery('u_display_name=AIR CONDITIONER','PORTABLE');
    gr.query();
    while (gr.next()) {
        gr.u_image = pic.getDisplayValue('image');
        count++;
        gr.update();
    }

}

gs.log('Done. Updating ' + count + ' records.');

 

Regards,

Alok