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

how can I user the asynchronous glideajax ????

@eyal abu hamad 

getXMLAnswer() is already asynchronous.

If using that is still giving user a feeling that the session is being taken up then check the other approach I shared.

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

@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

FANTASTIC !!!! one more qustion please, can I add infoMessage that inform the user that the action is in background ? tried addInfoMessage with no luck

@eyal abu hamad 

it's a list UI action so I don't think you can show message

 

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