Set Avatar for Users in Script

katie penner
Kilo Contributor

Hi All - 

 

Is there a way to set the Communities User Profile Avatar for all users from a given company?  I have a requirement that all users from company X need to use a single image as an avatar.  I was looking for a way to script this and wanted a bit of guidance from the community.  It is my understanding that the Communities avatar comes from the live_profile table rather than the sys_user table.  However, when I look at my own live_profile, the avatar I have in the Community is not there, so maybe I am mistaken.  Is there a way to set the  Communities avatar for each user for them in a script?  Any help is much appreciated!

 

Warmest,

Katie

1 ACCEPTED SOLUTION

You can also use below code and run this as fix script to update avatar for users.

You will have to update sys_id records as per your instance.

 

var liveprofileRecSysId = 'e3949a5cs0a0a3c6d01e089a708d3ebef';
var userId = 'abc';
var trgtAttchSysId = '';
var userSysId = '6816f79cc0a8016401c5sa33be04be441';

try{

var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
request.setEndpoint('https://www.w3schools.com/html/pic_trulli.jpg');
request.saveResponseBodyAsAttachment('live_profile', liveprofileRecSysId, 'photo');
var response = request.execute();

trgtAttchSysId = response.getResponseAttachmentSysid();
gs.print('trgtAttchSysId = '+trgtAttchSysId);

var httpResponseStatus = response.getStatusCode();

//GlideSysAttachment.copy('live_profile',liveprofileRecSysId,'ZZ_YYlive_profile',liveprofileRecSysId);
//GlideSysAttachment.copy('ZZ_YYlive_profile',liveprofileRecSysId,'sys_attachment',trgtAttchSysId);
//GlideSysAttachment.copy('live_profile',liveprofileRecSysId,'sys_attachment',trgtAttchSysId);

var attachRec = new GlideRecord('sys_attachment');
attachRec.get(trgtAttchSysId);
attachRec.setValue('table_name','ZZ_YYlive_profile');
attachRec.update();

}
catch(ex){
var message = ex.getMessage();
gs.print(message);
}

var attRec = new GlideRecord('sys_attachment');
attRec.addQuery('table_name','ZZ_YYlive_profile');
attRec.addQuery('table_sys_id',liveprofileRecSysId);
attRec.query();

gs.print('count = '+attRec.getRowCount());

if(attRec.next()){

//Create a new record for thumb image
var gr1 = new GlideRecord('sys_attachment');
gr1.initialize();

//gr1.file_name = attRec.file_name;
gr1.file_name = 'thumb_'+attRec.sys_id;
gr1.content_type = attRec.content_type;
gr1.compressed = attRec.compressed;
gr1.table_name = 'sys_attachment';
gr1.size_bytes = attRec.size_bytes;
gr1.size_compressed = attRec.size_compressed;
gr1.table_sys_id = attRec.sys_id;
var attRec2 = gr1.insert();

}

//Copy attachment contents from live_profile image to thumb image
var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', attRec.sys_id);
attDoc.query();

while(attDoc.next()){
var attDocCopy = new GlideRecord('sys_attachment_doc');
attDocCopy.initialize();
attDocCopy.sys_attachment = attRec2;
attDocCopy.position = attDoc.position;
attDocCopy.length = attDoc.length;
attDocCopy.data = attDoc.data;
attDocCopy.insert();

}

 

Regards,

Sachin

View solution in original post

9 REPLIES 9

sachin_namjoshi
Kilo Patron
Kilo Patron

You can use script include code from below share which has code to generate avatar for users.

 

https://developer.servicenow.com/connect.do#!/share/contents/1655773_unique_avatars_for_users?t=PROD...

 

Regards,

Sachin

Hi Sachin -

 

Thanks for this!  I took a look at the script include - can you tell me how I am supposed to use this for my requirement?  I'm not sure I understand what the code is doing so I am not sure how to modify it for my needs...

 

Warmest,

Katie

You can also use below code and run this as fix script to update avatar for users.

You will have to update sys_id records as per your instance.

 

var liveprofileRecSysId = 'e3949a5cs0a0a3c6d01e089a708d3ebef';
var userId = 'abc';
var trgtAttchSysId = '';
var userSysId = '6816f79cc0a8016401c5sa33be04be441';

try{

var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
request.setEndpoint('https://www.w3schools.com/html/pic_trulli.jpg');
request.saveResponseBodyAsAttachment('live_profile', liveprofileRecSysId, 'photo');
var response = request.execute();

trgtAttchSysId = response.getResponseAttachmentSysid();
gs.print('trgtAttchSysId = '+trgtAttchSysId);

var httpResponseStatus = response.getStatusCode();

//GlideSysAttachment.copy('live_profile',liveprofileRecSysId,'ZZ_YYlive_profile',liveprofileRecSysId);
//GlideSysAttachment.copy('ZZ_YYlive_profile',liveprofileRecSysId,'sys_attachment',trgtAttchSysId);
//GlideSysAttachment.copy('live_profile',liveprofileRecSysId,'sys_attachment',trgtAttchSysId);

var attachRec = new GlideRecord('sys_attachment');
attachRec.get(trgtAttchSysId);
attachRec.setValue('table_name','ZZ_YYlive_profile');
attachRec.update();

}
catch(ex){
var message = ex.getMessage();
gs.print(message);
}

var attRec = new GlideRecord('sys_attachment');
attRec.addQuery('table_name','ZZ_YYlive_profile');
attRec.addQuery('table_sys_id',liveprofileRecSysId);
attRec.query();

gs.print('count = '+attRec.getRowCount());

if(attRec.next()){

//Create a new record for thumb image
var gr1 = new GlideRecord('sys_attachment');
gr1.initialize();

//gr1.file_name = attRec.file_name;
gr1.file_name = 'thumb_'+attRec.sys_id;
gr1.content_type = attRec.content_type;
gr1.compressed = attRec.compressed;
gr1.table_name = 'sys_attachment';
gr1.size_bytes = attRec.size_bytes;
gr1.size_compressed = attRec.size_compressed;
gr1.table_sys_id = attRec.sys_id;
var attRec2 = gr1.insert();

}

//Copy attachment contents from live_profile image to thumb image
var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', attRec.sys_id);
attDoc.query();

while(attDoc.next()){
var attDocCopy = new GlideRecord('sys_attachment_doc');
attDocCopy.initialize();
attDocCopy.sys_attachment = attRec2;
attDocCopy.position = attDoc.position;
attDocCopy.length = attDoc.length;
attDocCopy.data = attDoc.data;
attDocCopy.insert();

}

 

Regards,

Sachin

Hi Sachin -

This is awesome and works to set the photo in the live_profile, so thank you!  The only problem is this does not appear to be the photo that is associated with the Community profile, so does not fulfill my requirement.  Do you know where SNow is pulling this photo from?  

 

Warmest,

Katie