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

Thanks for your response Alok. I tried your code and now the output in my image field is '

878880b5c3022100910900251eba8f76.iix'. Do I need to follow the link you shared before to actually associate this .iix file with an image or?

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

Thank you Alok! That script and the HI article you linked solved it for me and helped me to understand what was happening. 

RAJU MANGA
Tera Contributor

Hi.
Instead of taking the image from "db_image" to insert in any record, can we insert the image directly from PC through scripting. Is it possible?

Regards,
RAJU MANGA