How to run scheduled script execution on ui action

Anna_Servicenow
Tera Guru

I have the below UI action-client(scoped) and script include(scoped), When I test with high load, the script is getting transaction cancelled with "maximum execution time exceeded". How can I get this script executed so that its not timed out. Is it possible to run a run scheduled script on click of ui action and how? Please help with script.
script include:

var AttachmentParserScoped = Class.create();

AttachmentParserScoped.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getExcelInfo: function() {

        var currentSysId = this.getParameter('sysparm_sys_id');

        var currentParent = this.getParameter('sysparm_parent');

        var attachment = new GlideSysAttachment();

        var getdata = new GlideRecord('sys_attachment');

        getdata.addQuery('table_sys_id', currentSysId);

        getdata.addQuery('table_name', "x_fdrbl_task");

        getdata.query();

        if (getdata.next()) {

            //An attachment is found

       

            var attachmentSysID = getdata.sys_id.toString();

            var attachmentStream = attachment.getContentStream(attachmentSysID);

            var demodata = new sn_impex.GlideExcelParser();

            demodata.parse(attachmentStream);

            var caseID = [];

            var resolvedBy = [];

            var headers = demodata.getColumnHeaders();

            var header1 = headers[0];

            var header2 = headers[1];

            while (demodata.next()) {

                var row = demodata.getRow();

                caseID.push(row[header1]);

                resolvedBy.push(row[header2]);

            }

            var finalArr = [];

            finalArr[0] = caseID.toString();

            finalArr[1] = resolvedBy.toString();

 

            // ***** Create entries in x_fdrbl_quality_control_list table******//

            for (var i = 0; i < caseID.length; i++) {

                var qualityControlList = new GlideRecord('x_fdrbl_quality_control');

                qualityControlList.initialize();

                qualityControlList.parent_id = currentSysId;

                // Check if the username exists in sys_user table

                var userName = resolvedBy[i];

                var userDetail = new GlideRecord('sys_user');

                userDetail.addQuery('name', userName);

                userDetail.addQuery('email', 'CONTAINS', '@uion.com');

                userDetail.query();

                if (userDetail.next()) {

                    qualityControlList.resolved_by = userDetail.sys_id.toString();

                    qualityControlList.subteam = userDetail.u_sub_team;

                    qualityControlList.parentteam = userDetail.u_parent_team;

                } else {

                    gs.addErrorMessage("Resolved by user not found: " + userName);

                }

                qualityControlList.case_id = caseID[i];

                qualityControlList.insert();

            //  var attachmentDlt = new GlideSysAttachment();

            //  attachmentDlt.deleteAttachment(attachmentSysID);

               

            }

        } else {

            gs.addErrorMessage("Please attach a file to import the data");

        }

    },

    type: 'AttachmentParserScoped'

});

 

/// ui action:

 

function showDetails() {
g_form.setVisible('import_state', true);
g_form.setValue('import_state', 'In progress');
   
    alert("Please wait import is in progress" );

    var systemId = g_form.getUniqueValue();
    var ga = new GlideAjax('AttachmentParserScoped');
    ga.addParam('sysparm_name', 'getExcelInfo');
    ga.addParam('sysparm_sys_id', systemId);
    ga.getXMLAnswer(showDetails);

    function showDetails(answer) {
        alert("Import is complete, Please reload the form");
        g_form.setValue('import_state', 'Complete');
        location.reload(true);
      //  g_form.setValue('import_in_progress', 'false');
    }
}
g_form.setVisible('import_state', false);
1 ACCEPTED SOLUTION

@Anna_Servicenow 

No UI action required.

Simply do this

1) use async insert/update BR and call that script include inside it

BR: Condition: After update (With correct import_state condition)

 

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

	// Add your code here
	new AttachmentParserScoped().getExcelInfo(current.getUniqueValue());

})(current, previous);

 

Script Include:

 

var AttachmentParserScoped = Class.create();

AttachmentParserScoped.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getExcelInfo: function(sysId) {

        var currentSysId = sysId;

        var attachment = new GlideSysAttachment();

        var getdata = new GlideRecord('sys_attachment');

        getdata.addQuery('table_sys_id', currentSysId);

        getdata.addQuery('table_name', "x_fdrbl_task");

        getdata.query();

        if (getdata.next()) {

            //An attachment is found



            var attachmentSysID = getdata.sys_id.toString();

            var attachmentStream = attachment.getContentStream(attachmentSysID);

            var demodata = new sn_impex.GlideExcelParser();

            demodata.parse(attachmentStream);

            var caseID = [];

            var resolvedBy = [];

            var headers = demodata.getColumnHeaders();

            var header1 = headers[0];

            var header2 = headers[1];

            while (demodata.next()) {

                var row = demodata.getRow();

                caseID.push(row[header1]);

                resolvedBy.push(row[header2]);

            }

            var finalArr = [];

            finalArr[0] = caseID.toString();

            finalArr[1] = resolvedBy.toString();



            // ***** Create entries in x_fdrbl_quality_control_list table******//

            for (var i = 0; i < caseID.length; i++) {

                var qualityControlList = new GlideRecord('x_fdrbl_quality_control');

                qualityControlList.initialize();

                qualityControlList.parent_id = currentSysId;

                // Check if the username exists in sys_user table

                var userName = resolvedBy[i];

                var userDetail = new GlideRecord('sys_user');

                userDetail.addQuery('name', userName);

                userDetail.addQuery('email', 'CONTAINS', '@uion.com');

                userDetail.query();

                if (userDetail.next()) {

                    qualityControlList.resolved_by = userDetail.sys_id.toString();

                    qualityControlList.subteam = userDetail.u_sub_team;

                    qualityControlList.parentteam = userDetail.u_parent_team;

                } else {

                    gs.addErrorMessage("Resolved by user not found: " + userName);

                }

                qualityControlList.case_id = caseID[i];

                qualityControlList.insert();

                //  var attachmentDlt = new GlideSysAttachment();

                //  attachmentDlt.deleteAttachment(attachmentSysID);


                var gr = new GlideRecord("x_fdrbl_task");
                gr.addQuery("sys_id", currentSysId);
                gr.query();
                if (gr.next()) {
                    gr.import_state = 'Complete';
                    gr.update();
                }



            }

        } else {

            gs.addErrorMessage("Please attach a file to import the data");

        }

    },

    type: 'AttachmentParserScoped'

});

 

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

View solution in original post

14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

@Anna_Servicenow 

the script is taking long time and hence getting timed out.

what's your business requirement which you are trying to achieve?

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

@Ankur Bawiskar As the user import an excel with two column, case id and resolved by, and click on import button, the data in the excel should be displayed in the embedded list in the same form.

There will be multiple users performing this activity at same time and each excel will have upto 500 rows.
NB: Trying to achieve this without transform map

@Anna_Servicenow 

I will suggest to use transform map as it's asynchronous process

Check my blog and enhance the logic. user can submit excel and submit the request and it will start the processing in background

Data load and transform via Catalog Item 

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

@Ankur Bawiskar Is it possible to achieve this with schedule script execution or any other script asynchronously