UI action script to run in background

eyal abu hamad
Mega Sage

I want my UI action script to run in background.
hey, I want to check record from list and then use the ui action to approver them (time cards)
the problem that im having that when I check couple of time cards and use the ui action it well take for ever to do the script.
can I run the process in the background ?
I will give the scripts
UI action : 

Client - checked
form button - checked
list choice - checked
onClick - approveRecord();
script - 

 

function approveRecord() {
    var ga = new GlideAjax('global.ApproveScriptInclude');
    ga.addParam('sysparm_name', 'approve');
	var temp = g_list.getChecked();
    ga.addParam('sysparm_record_id',temp);
    ga.getXMLAnswer(handleResponse);
}

function handleResponse(response) {
        //g_form.refresh();
		
}

and the script include

var ApproveScriptInclude = Class.create();
ApproveScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    approve: function() {
        var recordIds = this.getParameter('sysparm_record_id').split(','); // Splitting the record IDs
        var results = [];

        recordIds.forEach(function(recordId) {
            var gr = new GlideRecord('time_card'); // Specify the table name
            if (gr.get(recordId.trim())) { // Trim spaces if any
                try {
                    var isDataSeparationSkippedFromNowOnwards = typeof DataSeparatorGlobalUtil !== "undefined" ? DataSeparatorGlobalUtil.skipDataSeparation() : false;
                    gr.setValue('state', 'Approved');
                    var updateResult = gr.update();
                    if (updateResult) {
                        if (isDataSeparationSkippedFromNowOnwards) {
                            DataSeparatorGlobalUtil.honourDataSeparation();
                        }
                        results.push("success: " + recordId);
                    } else {
                        results.push("failure: could not update " + recordId);
                    }
                } catch (e) {
                    results.push("failure: " + e + " in " + recordId);
                }
            } else {
                results.push("failure: record not found " + recordId);
            }
        });

        return JSON.stringify(results); // Returning JSON string of results for each record ID
    },

    type: 'ApproveScriptInclude'
});

I want the script to run in the background and not slow down my system

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@eyal abu hamad 

Seems a duplicate question.

I already answered your other question which talks about the same requirement.

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@eyal abu hamad 

Seems a duplicate question.

I already answered your other question which talks about the same requirement.

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Sarthak Kashyap
Tera Expert

Hi @eyal abu hamad ,

Please try below code in background script 

new global.ApproveScriptInclude().approve()

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards

Sarthak