How to Attach a Profile Photo to a Newly Created User Using Script?

VENKATASAMPATH
Tera Contributor

Hi Team,

I am looking for a script-based approach to attach a profile photo to a newly created user (sys_user).

My instance does not have a user_image table. I would like to know how to associate a profile photo with a user record using a script.

Requirements:

  • Create a new user record in sys_user using a script.

  • Attach or associate a profile photo with the user through the same script.

  • If the photo is stored as an attachment, how can it be copied from an existing record to the newly created user using a script?

  • Is there a supported API or recommended method to set the User Photo field through scripting?

Could someone provide a sample script or guidance on the recommended approach?

Thank you.

3 REPLIES 3

pr8172510
Tera Guru

Hi @VENKATASAMPATH,

Yes, this can be achieved through scripting.

profile photos are stored as attachments (sys_attachment / sys_attachment_doc) associated with the user record. There is no separate user_image table.

Create User and Copy Existing Profile Photo

// Create User
var userGR = new GlideRecord('sys_user');
userGR.initialize();
userGR.first_name = 'John';
userGR.last_name = 'Doe';
userGR.user_name = 'jdoe';

var userSysId = userGR.insert();

// Copy photo from another user
var gsa = new GlideSysAttachment();
gsa.copy(
    'sys_user',
    'SOURCE_USER_SYS_ID',
    'sys_user',
    userSysId
);

 

Attach a New Image

var userGR = new GlideRecord('sys_user');
if (userGR.get(userSysId)) {

    var gsa = new GlideSysAttachment();

    gsa.write(
        userGR,
        'profile.jpg',
        'image/jpeg',
        imageBytes
    );
}

 

Where imageBytes contains the binary image data (for example, decoded from Base64).

 

The supported approach is to use the GlideSysAttachment API for creating or copying attachments. Avoid directly manipulating sys_attachment records unless there is a specific requirement, as ServiceNow manages attachment relationships internally.

vaishali231
Kilo Sage

Hey @VENKATASAMPATH 

Yes, this can be achieved through scripting even if your instance does not have a dedicated user_image table.

In most modern ServiceNow releases, the user's profile photo is stored as an attachment associated with the sys_user record. Therefore, the recommended approach is:

  1. Create the user record.
  2. Attach an image to the newly created sys_user record.
  3. If needed, copy an existing attachment from another record using GlideSysAttachment APIs.

Example 1: Create a User and Copy an Existing Profile Photo

// Create new user

var userGR = new GlideRecord('sys_user');

userGR.initialize();

userGR.first_name = 'John';

userGR.last_name = 'Doe';

userGR.user_name = 'john.doe';

userGR.email = 'john.doe@example.com';



var newUserSysId = userGR.insert();



// Source record containing profile image

var sourceUserSysId = 'SOURCE_USER_SYS_ID';



// Copy attachments from source user to new user

var attachment = new GlideSysAttachment();

attachment.copy(

    'sys_user',

    sourceUserSysId,

    'sys_user',

    newUserSysId

);

gs.info('Profile image copied successfully');

Example 2: Upload a New Image to the User Record

var userGR = new GlideRecord('sys_user');

if (userGR.get('NEW_USER_SYS_ID')) {
    var gsa = new GlideSysAttachment();

    gsa.write(
        userGR,
        'profile_photo.jpg',
        'image/jpeg',
        imageBytes
    );

    gs.info('Profile photo attached successfully');

}

Note:

  • imageBytes must contain the binary content of the image.
  • This approach is commonly used when receiving employee photos from an HR system or an external integration.

Example 3: Verify the Attachment

var attGR = new GlideRecord('sys_attachment');

attGR.addQuery('table_name', 'sys_user');

attGR.addQuery('table_sys_id', newUserSysId);

attGR.query();

while (attGR.next()) {

    gs.info('Attachment: ' + attGR.file_name);

}

 

*************************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb



Tanushree Maiti
Tera Patron

Hi @VENKATASAMPATH 

 

Check this post for the script :

https://www.servicenow.com/community/developer-forum/uploading-profile-avatar-images-via-rest-api-ge...

 

https://www.servicenow.com/community/developer-forum/upload-profile-photo-in-user-table/m-p/1943887/...

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti