Validating the attachment type and size

satya30
Tera Contributor

I had written before insert business rule on attachment table. In attachment table sys_id of the case that is going to be created. Business rule is working correctly as how I expected but the major problem is that this is running for all the forms in my custom portal. But this should run only for one form in portal. I tried using client script but did not work.

Below is my business rule:

(function executeRule(current, previous) {
    var MAX_SIZE = 10490314;

    var allowedTypes = [
        // Word
        'application/msword',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',

        // Excel
        'application/vnd.ms-excel',
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',

        // PowerPoint
        'application/vnd.ms-powerpoint',
        'application/vnd.openxmlformats-officedocument.presentationml.presentation',

        // PDF
        'application/pdf',

        // Images
        'image/jpeg',
        'image/png',
        'image/gif',
        'image/bmp',
        'image/webp',
        'image/svg+xml',

        // Video
        'video/mp4',
        'video/quicktime',
        'video/x-msvideo',
        'video/x-ms-wmv',

        // Audio
        'audio/mpeg',
        'audio/wav',
        'audio/ogg'
    ];


    var val = allowedTypes.indexOf(current.content_type.toString());
    gs.addErrorMessage(current.content_type);
    gs.addErrorMessage(val);

// Hidden field on the record producer form "Reward Night Program Form"
var recordProducerSysId = g_form.getValue('current_record'); 

    // Apply validation only for this specific Record Producer
    if (recordProducerSysId == 'c5af10261bd56210923a1132b24bcb89') {

                if (val === -1) {
                    gs.addErrorMessage(val);

                    // Validate file type
                    gs.addErrorMessage(
                        "Allowed File Types:\n\n" +
                        "- Word Documents: .doc, .docx\n" +
                        "- Excel Spreadsheets: .xls, .xlsx\n" +
                        "- PowerPoint Presentations: .ppt, .pptx\n" +
                        "- PDF Files: .pdf\n" +
                        "- Images: .jpg, .jpeg, .png, .gif, .svg, .bmp, .webp\n" +
                        "- Videos: .mp4, .mov, .avi, .wmv\n" +
                        "- Audio Files: .mp3, .wav, .ogg\n\n" +
                        "Maximum file size: 10 MB\n" +
                        "Only 1 file may be uploaded."
                    );
                    current.setAbortAction(true);
                }

                // Validate file size
                if (current.size_bytes > MAX_SIZE) {
                    gs.addErrorMessage('File size exceeds the maximum allowed size of 10MB.');
                    current.setAbortAction(true);
                }
            }
        
})(current, previous);

 

Please help me. Thank you

2 REPLIES 2

anand-bhosle
Tera Guru

Hello @satya30 ,

On a server-side Business Rule, you won’t have access to g_form (that’s a client-side API), So your check on the “current_record” hidden field will never work. Instead, every sys_attachment record carries the fields table_name – & table_sys_id – 

Use those to scope your rule to only fire when someone uploads to your one Record Producer .

 

You can use the same BR but add below condition 

// only run when attached to that one Record Producer
(current.table_name == 'sc_cat_item_producer' &&
current.table_sys_id == 'c5af10261bd56210923a1132b24bcb89');

 

Try and see. hope this helps.

 

Let me know if my response help you anyway so mark accepted /helpful

 

Thanks

Anand

But the main problem is in the attachment table table sys if has the value of the new case that is going to be created.

 

That is only main issue for me to validate

 

Can not record producer sysid