Abort UI action using script include

Aasrita
Tera Contributor

We have requirement of throwing an error on transform map when the column field names are wrong in Uploaded Excel sheet. We have the following script include.

 

var datasourceerror = Class.create();
datasourceerror.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    datasourcefunction: function() {
        // var test = 0;
        var columns = [];
        var inc = new GlideRecord('sys_attachment');
        inc.addEncodedQuery('table_name=sys_data_source^table_sys_id=dadb0b1447110210279ceb02d16d434a');
        inc.orderByDesc('sys_created_on');
        inc.setLimit(1);
        inc.query();

        if (inc.next()) {
            var sysId = inc.getValue('sys_id');
            // gs.print(sysId);
        }
        var parser = new sn_impex.GlideExcelParser();
        var attachment = new GlideSysAttachment();
        var attachmentStream = attachment.getContentStream(sysId);
        parser.setSource(attachmentStream);

        var list_sheet_name = parser.getSheetNames();
        var requiredColumns = ['Number', 'Opened', 'Short description', 'Caller', 'Category', 'Assigned to', 'Assignment group', 'state(u_state)'];
        var missingColumns = [];

        for (i = 0; i < list_sheet_name.length; i++) {
            // gs.info('sheet name :' + list_sheet_name[i]);

            if (parser.parse()) {

                var headers = parser.getColumnHeaders();
                for (i = 0; i < headers.length; i++) {
                    if (headers.indexOf(requiredColumns[i]) === -1) {
                        missingColumns.push(requiredColumns[i]);
                        //if (headers[i] == 'Number' || headers[i] == 'Opened' || headers[i] == 'Short description' || headers[i] == 'Caller' || headers[i] == 'Category' || headers[i] == 'Assigned to'|| headers[i] == 'Assignment group' || headers[i]=='state(u_state)') {
                        count++;
                        // gs.print(headers[i]);

                    }

                }
            }
        }
        if (missingColumns.length > 0) {
            for (i = 0; i < missingColumns.length; i++) {
                columns.push(missingColumns[i]);
            }
            return JSON.stringify(columns);
        }
    },

    type: 'datasourceerror'
});
 
And the returned array is being checked on the Ui action 'Load All records'.
 
ar col = [];
col = new datasourceerror();
gs.log('hello123');
col.datasourcefunction();
gs.log(col);
if (col.length > 0) {

    //for (i = 0; i < col.length; i++) {
        gs.log('hello456');
        current.setAbortAction(true);
        gs.addErrorMessage('One/few of the column name is not matching with the data upload template. Please refer KB article  KB99999999 to fetch the right template. Missing/wrong columns:   ' + col.join(', '));
        gs.addErrorMessage('Test');
        action.setRedirectURL(current);
    //}
 
But the script in the UI action is not validating. Instead we keep the condition in UI policy it is working as expected.
0 REPLIES 0