Uploading Profile/Avatar Images via REST API (GET Method)

Kamal17
Kilo Sage

Hi,

I'm working on a requirement to upload profile pictures for user accounts and make them visible in portal. Profile images are stored in a different application and i'm using a REST API url to fetch those images and store them in "Photo" field in "live_profile" table (background script given below).

Challenge:

Even after uploading the images to "photo" field in "live_profile" table, users are unable to see their profile/avatar images in service portal. Tried clearing cache, logout and login again after sometime but nothing works. I also tried creating one more entry in attachment table with name 'thumb_sysid of attachment in live_profile table' but no luck.

Instead of using REST API, if i manually upload the image in "photo" field in "live_profile" table it becomes visible in service portal. Not sure what i'm missing. Any helpful inputs will be appreciated.

 

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();

}

 

Thanks,

Udhay

1 ACCEPTED SOLUTION

Kamal17
Kilo Sage

Contacted HI support and they confirmed it's an adhoc issue and advised me to rebuild cache after loading all the images by entering the command cache.do in the application navigator search bar. Once, that is done refresh the service portal/home page to see the user profile/avatar images.

View solution in original post

2 REPLIES 2

Kamal17
Kilo Sage

Contacted HI support and they confirmed it's an adhoc issue and advised me to rebuild cache after loading all the images by entering the command cache.do in the application navigator search bar. Once, that is done refresh the service portal/home page to see the user profile/avatar images.

kevinanderson
Giga Guru

additional info from HI on the process that need to happen when loading an image via integration for users

 

https://hi.service-now.com/kb_view.do?sysparm_article=KB0693478

        Here is the Procedure how system decides which picture to be displayes as Avatar. 

  1. If user updates Profile picture form User profile. It creates two entries in sys_attachment table. 
    1. One entry with following values. 
      • File Name - Photo
      • Table Name - ZZ_YYsys_user
    2. Second entry with following values. 
      • File Name - thumb_sysid [ Sys id of record created in "a" entry. 
      • Table Name - sys_attachment
      • Table Sys id  - Sys id of record created in "a" entry. 
  2. Thumb file appear as Avatar icon for user. 
  3. If user updates photo from Live Profile. Same two entries gets created as mentioned in 1st. 
  4. If User's profile photo and Live Profile photo both are updated then live profile picture takes precedence and live profile's thumb file will be shown as User's Avatar icon. 
  5. If none of the photos available for user, then Avatar will be shown as Initials like BA for Beth Anglin.  

 

a cleanup effort will need occur when doing an update that involves finding and deleting the existing attachment entries