I want my 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

@eyal abu hamad 

something like this

Note: Please enhance it as per your requirement

1) Create an Event:

Navigate to System Policy > Events > Registry.
Click on New to create a new event.
Name the event (e.g., approve_records_event).

2) Create a Script Action:

Navigate to System Policy > Script Actions.
Click on New to create a new script action.
Name the script action (e.g., Approve Records Action).
Select the event you created (approve_records_event).
Script Action Code:

var recordIds = event.parm1.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);
    }
});

3) Trigger the Event from your Ajax function:

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

    approve: function() {
        var recordIds = this.getParameter('sysparm_record_id').toString(); // Splitting the record IDs
        gs.eventQueue('approve_records_event', current, recordIds);
    },

    type: 'ApproveScriptInclude'
});

I hope this much information will be helpful and you can achieve your requirement.

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

10 REPLIES 10

Nilesh Wahule
Tera Guru

Hi @eyal abu hamad ,

 

Please have a look at Run async script include from UI Action

 

---------------------------------------------------------------------------------------------------

Please mark my answer as helpful/correct if it resolves your query.

Thanks,
Nilesh Wahule

---------------------------------------------------------------------------------------------------

 

I dont want business rule, is there any way to run the script include in background (async) ?

Hi @eyal abu hamad ,

 

When you use GlideAjax with getXMLAnswer, its by default async, so i think its not waiting for any operation its returning the control to user side immediately after making the call. 

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

This code is executed post the operation has processed. I would recommend remove the callback function and check once.

---------------------------------------------------------------------------------------------------

Thanks
Nilesh Wahule

---------------------------------------------------------------------------------------------------

 

Ankur Bawiskar
Tera Patron
Tera Patron

@eyal abu hamad 

you can use asynchronous GlideAjax and the update will happen in backend

OR

you can use script action and event to run them in background

Do this

1) create an event in event registry

2) then create script action and associate it with the above event

3) from your GlideAjax function use gs.eventQueue() and trigger that event and pass the sysIds

4) when the event is triggered the script action will trigger and get the values using event.parm1 or event.parm2 and it will update the records asynchronously

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