- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2020 07:35 AM
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
Solved! Go to Solution.
- Labels:
-
Communities

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2020 08:48 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2020 10:13 AM
Hi Sachin -
On further investigation it looks like the avatar for community is set on sn_communities_profile table. I have tried to add additional code to this script to set the sn_communities_profile photo to be the same as the live_profile photo, but it is not working. Any advice?
Warmest,
Katie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2020 10:44 AM
Hi Sachin -
I have modified this code for the sn_communities_profile table but it is not working as it did for the live_profile table. I have replaced queries on the live_profile table with queries on the sn_communities_profile table and replaced the ZZ_YYlive_profile table_name on the attachment queries to point to the ZZ_YYsn_communities_profile table instead (which I have verified is the correct table_name on sys_attachment). Is there something I am missing?
Warmest,
Katie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2022 03:05 AM
Hi All,
Why the live_profile photos are getting deleted automatically?
Thanks!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2022 07:07 PM
Hi All,
After much effort and back and forth with ServiceNow via HI I have a solution here that is far simpler and cleaner (no need for creating thumbnail attachments or attachment documents). Taking an excerpt from the original script above - once you have your attachment record created (by whatever means you choose - REST, scripted, Table API - just wherever your record exists as 'trgtAttchSysId'):
var attachRec = new GlideRecord('sys_attachment');
attachRec.get(trgtAttchSysId);
attachRec.setValue('table_name','ZZ_YYlive_profile');
attachRec.update();
GlideCacheManager.flush("user_avatar_cache");
The line of code 'GlideCacheManager.flush("user_avatar_cache")' gets ServiceNow (under the hood) to create all of the necessary records and linkages for the new attachment to appear as your avatar everywhere across the platform. Ive tested it extensively and so have ServiceNow themselves... It works. If you use the GlideCacheManager whilst logged into your own session all you need to do is refresh the screen and the avatar will update. If you are using GlideCacheManager within a script on behalf of others, then the next time they log in it will refresh.
Good luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 10:34 PM
Hello,
This seems like the easiest option however unfortuntely, this didn't work for me. The record is updated in sys_attachment table but still avatar and Portal etc are not working. Did you do anything else after this post?
thanks