The CreatorCon Call for Content is officially open! Get started here.

How to Sync sys_user image to Live_profile image

krishnakumar2
Tera Guru

HI Team,

I have a requirement to sync photo from Custom User Profile table to sys_user table and live_profile table. I tried scripting it but it's not working. I am just not able to update the image in live_profile using script. It's working perfectly fine for user table but not getting inserted in live_profile table.

Here is the before business rule script I have added.

var image_sysid = current.u_photo;
var user_sysid = current.u_parent.u_user;

 

//update user table image
var usr = new GlideRecord('sys_user');
usr.addQuery('sys_id',user_sysid);
usr.query();
while(usr.next()){
usr.photo = image_sysid;
usr.update();
}

// update live profile pic
var liveprofile = new GlideRecord('live_profile');
liveprofile.addQuery('document',user_sysid);
liveprofile.query();
while(usr.next()){
liveprofile.photo = image_sysid;
liveprofile.update();
}

 

I am going clueless about how to fix this. Any inputs/guidance you give will be of great help.

9 REPLIES 9

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Try below for live_profile table update as the While Loop had usr.next() instead of liveprofile.next()

var liveprofile = new GlideRecord('live_profile');
liveprofile.addQuery('document',user_sysid);
liveprofile.query();
while(liveprofile.next()){
liveprofile.photo = image_sysid;
liveprofile.update();
}

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.

krishnakumar2
Tera Guru

Thanks for the reply. That was the typo. I had corrected that, but for some reason I am not able use uploaded image as  avatar image. I am not sure how avatar image gets computed. I found below code in Calculated Value for "Avatar" field in sys_user table.

 

(function calculatedFieldValue(current) { return GlideAvatarFinder.getAvatarID(current.getUniqueValue()) })(current);

Do you know what does it mean? What is the source of the data for this field? I tried updating this field, but have not had any success.

krishnakumar2
Tera Guru

I tried with below script to copy attachment as well. But so far no luck.

var userId = '50332850db4ffb4001ba594e5e961928';
var attachmentTablesysId ='c5a681b4db077f4001ba594e5e9619f6';
var liveProfileTableSysId = 'a926e014db4ffb4001ba594e5e9619b7'; // Sys id of Live Profile user record sys id.

// see if user photo already exists, if so, delete it
var existingPhoto = new GlideRecord('sys_attachment');
existingPhoto.addQuery('table_name', 'ZZ_YYsys_user');
existingPhoto.addQuery('table_sys_id', attachmentTablesysId);
//existingPhoto.addQuery('file_name', 'u_photo');
existingPhoto.query();
while (existingPhoto.next())
existingPhoto.deleteRecord();

// see if user photo already exists, if so, delete it
var existingPhoto = new GlideRecord('sys_attachment');
existingPhoto.addQuery('table_name', 'ZZ_YYlive_profile');
existingPhoto.addQuery('table_sys_id', liveProfileTableSysId );
existingPhoto.addQuery('file_name', 'photo');
existingPhoto.query();
while (existingPhoto.next())
existingPhoto.deleteRecord();

// copy attachment then rename it
new GlideSysAttachment().copy('ZZ_YYu_upload_id_photo_task', attachmentTablesysId , 'sys_user', userId);

new GlideSysAttachment().copy('ZZ_YYu_upload_id_photo_task', attachmentTablesysId , 'live_profile', liveProfileTableSysId );

// now find that attachment and rename it to photo
var newPhoto = new GlideRecord('sys_attachment');
newPhoto.addQuery('table_name', 'ZZ_YYsys_user');
newPhoto.addQuery('table_sys_id', userId);
// grab the latest incase there are other attachments on this user for some obscure reason
newPhoto.orderByDesc('sys_created_on');
newPhoto.query();
if (newPhoto.next()) {
newPhoto.file_name = 'photo';
newPhoto.update();
}
// now find that attachment and rename it to photo
var newPhoto = new GlideRecord('sys_attachment');
newPhoto.addQuery('table_name', 'ZZ_YYlive_profile');
newPhoto.addQuery('table_sys_id', liveProfileTableSysId );
// grab the latest incase there are other attachments on this user for some obscure reason
newPhoto.orderByDesc('sys_created_on');
newPhoto.query();
if (newPhoto.next()) {
newPhoto.file_name = 'photo';
newPhoto.update();
}

 

 

Reference: https://community.servicenow.com/community?id=community_question&sys_id=311d89afdb5a5704b61ff3231f96...

krishnakumar2
Tera Guru

Hey guys, any help/input you provide will be of great help.