Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to restrict the image file to upload in employee profile page when fill exceeds 300 kb

Aditya02
Tera Guru

I've been assigned a task to restrict the upload of a profile cover image on the Employee Profile page in the Employee Center if the file size exceeds 300 KB. I'm using the Employee Profile Header widget to implement this functionality. If the file size exceeds the limit, an error message should appear indicating that the file size has been exceeded.

 

Writing a Business Rule on the table is not allowed for this requirement.

Could anyone suggest the best practices or approaches to validate the file size and meet this requirement?

11 REPLIES 11

@Aditya02 

you need to customize OOB widget or clone the OOB one and handle your logic

are you referring to this one?

AnkurBawiskar_0-1734349561830.png

if yes then you will have to clone the User Profile widget and make the changes

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

Is there any way to do this instead of cloning the widget.?

@Aditya02 

you can try to use before insert BR on sys_attachment but you told BR is not allowed

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Mark Manders
Mega Patron

Why not use a BR for this? Check the size and abort action, with a message. It's an easy thing to do.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi @Mark Manders ,

 

I tried implementing this with a Business Rule, but it's not working as expected. While I can see the message on the platform side, it doesn't appear in the portal view. Additionally, I attempted to abort the action, but it doesn't stop the image from being uploaded.

 

Below is the script I used:

 

(function executeRule(current, previous /*null when async*/ ) {
    if (current.background_banner) {
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_sys_id', current.sys_id);
        attachmentGR.orderByDesc('sys_created_on'); // Get the most recent attachment
        attachmentGR.query();
        gs.addInfoMessage('curr: '+current.sys_id);
 
        if (attachmentGR.next() && attachmentGR.size_bytes > 200 * 1024) { // Check if size exceeds 200 KB
            gs.addErrorMessage('Image size exceeds the limit of 200 KB. Try a different one.');
            current.setAbortAction();
        }
    }

})(current, previous);