Importing .jpg pictures with a data source

teroruuskanen
Giga Contributor

Greetings!

I would like to import .jpg pictures to ServiceNow from a network location or MID server. Is this possible with a data source? We have a network location with profiles pictures for our employees, and I'm investigating if I'm able to start importing them into SN instead of using LDAP as we would like to use high resolution profile pictures .

Thank you

3 REPLIES 3

Chuck Tomasi
Tera Patron

I recommend taking a look at the inbound REST API "Attachment API" available. 

Image fields are using the sys_attachment table, however the table_name field is not the name of the table (e.g. sys_user) for image fields - it is ZZ_YYsys_user to tell the system this attachment goes with a specific image field indicated in the field name. E.g. "photo" for sys_user images placed in the photo field.

G11
ServiceNow Employee
ServiceNow Employee
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
	
	//gs.log('User Photo Script: Check for Existing Attachment', 'Active Directory');
	var grPhotoAttachmentExists = new GlideRecord('sys_attachment');
	grPhotoAttachmentExists.addQuery('table_name','ZZ_YYsys_user');
	grPhotoAttachmentExists.addQuery('table_sys_id',target.sys_id);
	grPhotoAttachmentExists.addQuery('file_name','photo');
	grPhotoAttachmentExists.query();
	if (source.u_thumbnailphoto != '') {
		//gs.log('User Photo Script: LDAP Source Photo Exists', 'Active Directory');
		if (!grPhotoAttachmentExists.next()) {
			//gs.log('User Photo Script: No existing photo attachment, attaching new photo', 'Active Directory');
			attachPhoto();
		} else {
			//gs.log('User Photo Script: Photo Attachment Exists, Compare Attachments', 'Active Directory');
			var sysEncodedAttachment = new GlideSysAttachment();
			var binData =sysEncodedAttachment.getBytes(grPhotoAttachmentExists);
			var EncodedBytes = GlideStringUtil.base64Encode(binData);
			//gs.log('User Photo Script: sys_updated_by is ' + grPhotoAttachmentExists.sys_updated_by, 'Active Directory');
			if (EncodedBytes != source.u_thumbnailphoto && grPhotoAttachmentExists.sys_updated_by == 'admin') {
				//gs.log('User Photo Script: Photo attachment exists, bytes does not match, delete existing attachment and attaching new photo', 'Active Directory');
				grPhotoAttachmentExists.deleteRecord();
				attachPhoto();
			} else {
				//gs.log('User Photo Script: Image has not changed or a personal Profile Photo attachment exists. Not updating from system', 'Active Directory');
			}
		}
	} else {
		//gs.log('User Photo Script: AD Source Photo Does Not Exist', 'Active Directory');
		if (grPhotoAttachmentExists.next()) {
			//gs.log('User Photo Script: Deleting existing photo attachment', 'Active Directory');
			grPhotoAttachmentExists.deleteRecord();
		}
	}
	
	function attachPhoto(){
		//gs.log('User Photo Script: Attach Photo', 'Active Directory');
		var sysDecodedAttachment = new GlideSysAttachment();
		var DecodedBytes = GlideStringUtil.base64DecodeAsBytes(source.u_thumbnailphoto);
		var attID = sysDecodedAttachment.write(target, 'photo', 'image/jpeg', DecodedBytes);
		var newAttachment = new GlideRecord("sys_attachment");
		newAttachment.addQuery("sys_id", attID);
		newAttachment.query();
		if (newAttachment.next()) {
			newAttachment.table_name = "ZZ_YYsys_user";
			newAttachment.table_sys_id = target.sys_id;
			newAttachment.content_type = 'image/jpeg';
			newAttachment.update();
		}
	}
	
})(source, map, log, target);

Is this an on after, on before or etc? Also have you tested it yet to see if it works?