- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-01-2025 03:21 AM
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 :
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-01-2025 03:57 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-01-2025 03:38 AM
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
---------------------------------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-01-2025 03:43 AM
I dont want business rule, is there any way to run the script include in background (async) ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-01-2025 03:57 AM
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
---------------------------------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-01-2025 03:51 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader