We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Validating Attachment extension based on type of Attachment in Catalog Request

jreddy7
Giga Contributor

In My catalog item There is a variable Called "file_type" it is a select box type in that there is 3 options like txt,xlsx,csv. If file type is csv, then In Catalog item there is a variable called "Data Attachment" variable it is a attachment type. It will allow to user to attach only csv file. Like that remaing also If user selects txt type then it will allow to attach the txt file attachment only.

 

For this I have created a On change client script on data_attachment variable.

 

Client script:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

    g_form.hideFieldMsg('data_attachment ');

    var selectedFileType = g_form.getValue('file_type');
    alert('Selected Type: ' + selectedFileType);

 

    if (!selectedFileType) {

        g_form.clearValue('data_attachment');

        g_form.showFieldMsg('data_attachment', 'Please select a file type before uploading.', 'error');

        return;

    }

    var ga = new GlideAjax('AttachmentValidator');

    ga.addParam('sysparm_name', 'validateType');

    ga.addParam('sysparm_attachment_id', newValue);

    ga.addParam('sysparm_selected_type', selectedFileType);

   

    ga.getXMLAnswer(function(answer) {

        if (answer === 'invalid') {

            g_form.showFieldMsg('data_attachment', 'Invalid file type. Please upload a .' + selectedFileType + ' file.', 'error');

           // g_form.clearValue('data_attachment');

        }

    });

}
 
Script Include:
var AttachmentValidator = Class.create();

AttachmentValidator.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    validateType: function() {

        var attachmentSysId = this.getParameter('sysparm_attachment_id');

        var selectedType = this.getParameter('sysparm_selected_type'); // e.g., 'csv', 'xlsx', 'txt'

                if (!attachmentSysId || !selectedType) {

            return 'valid';

        }

 

        var gr = new GlideRecord('sys_attachment');

        if (gr.get(attachmentSysId)) {

            var fileName = gr.getValue('file_name').toLowerCase();

            var fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);

            if (fileExtension !== selectedType.toLowerCase()) {

                return 'invalid';

            }

        }

        return 'valid';

    },

 

    type: 'AttachmentValidator'

});
 
 
Attached the error screenshot ; It is allowing to attach any txt,csv and xlsx types files  but not allowing other than these.
2 REPLIES 2

Chaitanya ILCR
Giga Patron

Hi @jreddy7 ,

 

Client script

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || !newValue) {
        return;
    }

    g_form.hideFieldMsg('data_attachment');

    var selectedFileType = g_form.getValue('file_type');

    if (!selectedFileType) {
        g_form.clearValue('data_attachment');
        g_form.showFieldMsg(
            'data_attachment',
            'Please select a file type before uploading.',
            'error'
        );
        return;
    }

    var ga = new GlideAjax('AttachmentValidator');
    ga.addParam('sysparm_name', 'validateType');
    ga.addParam('sysparm_attachment_id', newValue);
    ga.addParam('sysparm_selected_type', selectedFileType);

    ga.getXMLAnswer(function(answer) {

        if (answer === 'invalid') {

            g_form.showFieldMsg(
                'data_attachment',
                'Invalid file type. Please upload only .' +
                selectedFileType + ' files.',
                'error'
            );

            // Clear the attachment variable
            g_form.clearValue('data_attachment');

        }
    });
}

 

script include

 

var AttachmentValidator = Class.create();
AttachmentValidator.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateType: function() {

        var attachmentSysId = this.getParameter('sysparm_attachment_id');
        var selectedType = this.getParameter('sysparm_selected_type');

        if (!attachmentSysId || !selectedType) {
            return 'valid';
        }

        var gr = new GlideRecord('sys_attachment');

        if (gr.get(attachmentSysId)) {

            var fileName = gr.getValue('file_name').toLowerCase();
            var fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);

            if (fileExtension !== selectedType.toLowerCase()) {

                return 'invalid';
            }
        }

        return 'valid';
    },

    type: 'AttachmentValidator'
});

Tanushree Maiti
Tera Patron

Hi @jreddy7 

 

  • Navigate to your Catalog Item and open the data_attachment variable.
  • Switch to the Type Specifications tab.
  • In the Variable attributes field, you have mentioned:
    allowed_extensions=csv;xlsx;txt

mention all type file extensions here, which you want to allow at form level attachment variable.

 

 

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