Need a script fix to Error messages ,Attachment restriction after Error

suneelkumar
Tera Contributor

Hi Buddies,

 

If you have any solution for this please fix this code, My issue was while submitting an Attachment if Attachment is having morethan 2 MB it should through error messsage , 

I have created  a Business rule to following functionalities.

1.Two Error Messages Needs to be populated , and count is showing wrong.

2.If Error Messages are populating , It should restrict the attachment adding.

3.If No Error Messages are Populating , It should Allow the attachment.

This is the code , Can anyone of you can fix it , If you are able to analyse it.

 

 

If possible please provide solution ASAP

 

----------------------------------------------------------------------------------------------------------

(function executeRule(current, previous /*null when async*/ ) {

var errornote = gs.getMessage("The picture format must be .jpg or .png and only one photo should be uploaded.");
var errornote1 = gs.getMessage("Profile photo cannot be more than 2 MB in size.");

var grTask = new GlideRecord("sn_hr_core_task");
grTask.addQuery("sys_id", current.table_sys_id);
grTask.addEncodedQuery("hr_task_type=upload_documents^template=" + gs.getProperty('upload_a_photo_hr_task_template'));
grTask.setLimit(1);
grTask.query();

if (grTask.next()) {
var contentType = current.getValue('content_type');
var errorCount = 0;

if (!(contentType.toString().indexOf('png') > -1 || contentType.toString().indexOf('jpg') > -1)) {
gs.addErrorMessage(errornote);
errorCount++;
}

if (parseInt(current.size_bytes) > 2097152) {
gs.addErrorMessage(errornote1);
errorCount++;
}

// Abort the action if errors are triggered
if (errorCount > 0) {
current.sys_attachment = false;
current.canCreate = false;
current.setAbortAction(true);
} else {
// Allow attachment creation when no errors
current.sys_attachment = true;
current.canCreate = true;
}

if (GlideMobile.isMobile()) {
if (errorCount === 1) {
gs.info("1 Error message");
} else if (errorCount > 1) {
gs.info(errorCount + " Error messages");
}
}
}

})(current, previous);

 

-------------------------------------------------------------------------------------------------------------------------

 

 

1 REPLY 1

Sandeep Rajput
Tera Patron
Tera Patron

@suneelkumar I am assuming, you have created an on Before Insert Business rule on sys_attachment table if this assumption is correct, I am not sure if you need to set the following fields 

 

current.sys_attachment = true;
current.canCreate = true;

As these fields don't exist on sys_attachment table, the script might be crashing due to it.

 

Here is the OOTB fields on sys_attachment table.

Screenshot 2023-11-17 at 9.40.34 AM.png