Script Action not executing when triggered by custom event (mass update on custom table)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hey guys!
u_restricted_read_access = true) on historical tasks stored in a custom base table (x_vgll_rail_task).“Historical” simply means all existing records, regardless of state.
- A custom event:
x_vgll_rail.add_restricted_access - A Script Action triggered by the event
- Updated logic in the Record Producer to:
- Count matching records
- Send the event if there are records to update
It seems the event is queued, but the Script Action does not run — I get no logs from inside it, and no records are updated.
I already have this old example script in the Record Producer which does what I will do, but the other way around:
else if(action == 'remove_restricted_access') {
var taskGR = new GlideRecord('x_vgll_rail_task');
taskGR.addQuery('assignment_group', producer.assignment_group);
taskGR.addQuery('u_restricted_read_access', true);
taskGR.query();
while(taskGR.next()){
taskGR.u_restricted_read_access = false;
taskGR.setWorkflow(false);
taskGR.update();
}
gs.addInfoMessage('Begränsad läsåtkomst för historiska ärenden med utförargrupp ' + producer.name + ' är borttagen.');
current.setAbortAction(true);
producer.redirect = "x_vgll_rail_user_organisation_list.do";
}
This is the script in the record producer that I've added below the the example above:
else if (action == 'add_restricted_access') {
// 1. Hitta alla ärenden som ska påverkas
var taskGR2 = new GlideRecord('x_vgll_rail_task');
taskGR2.addQuery('assignment_group', producer.assignment_group);
taskGR2.addQuery('u_restricted_read_access', false); // inte redan begränsade
taskGR2.query();
var count = taskGR2.getRowCount();
if (count == 0) {
gs.addInfoMessage(
'Det finns inga historiska ärenden för utförargruppen ' +
producer.name +
' som kan få begränsad läsåtkomst.'
);
current.setAbortAction(true);
producer.redirect = 'x_vgll_rail_user_organisation_list.do';
} else {
// 2. Skicka ett event som Script Action tar hand om i bakgrunden
gs.eventQueue(
'x_vgll_rail.add_restricted_access', // event name
current, // current = record producer
producer.assignment_group, // parm1: sys_id på utförargruppen
producer.name // parm2: gruppens namn
);
// 3. Ge feedback till användaren
gs.addInfoMessage(
'Begränsad läsåtkomst kommer att läggas till för ' +
count +
' historiska ärenden med utförargrupp ' +
producer.name +
'. Processen körs i bakgrunden.'
);
current.setAbortAction(true);
producer.redirect = 'x_vgll_rail_user_organisation_list.do';
}
}
Here is the Script Action script:
(function executeAction(current, event) {
// 1. Hämta parametrar från eventet
var assignmentGroupSysId = event.parm1; // parm1: sys_id för assignment_group
var assignmentGroupName = event.parm2; // parm2: gruppens namn (för loggning)
// 2. Säkerhetskoll – om vi saknar assignment group, avbryt
if (!assignmentGroupSysId) {
gs.log(
'x_vgll_rail.add_restricted_access: Saknar assignment_group (parm1). Avbryter.',
'x_vgll_rail'
);
return;
}
// 3. Bygg GlideRecord mot bastabellen x_vgll_rail_task
var taskGR = new GlideRecord('x_vgll_rail_task');
// Filtrera på:
// - Assignment group (från eventet)
// - Bara de som INTE redan har begränsad läsåtkomst
taskGR.addQuery('assignment_group', assignmentGroupSysId);
taskGR.addQuery('u_restricted_read_access', false);
// 4. Kör query för att hitta matchande records
taskGR.query();
// 5. Räkna hur många ärenden vi hittade
var count = taskGR.getRowCount();
if (count === 0) {
// Inga records att uppdatera – logga bara
gs.log(
'x_vgll_rail.add_restricted_access: Inga ärenden att uppdatera för grupp: ' +
assignmentGroupName + ' (' + assignmentGroupSysId + ').',
'x_vgll_rail'
);
return;
}
// 6. Förbered massuppdatering
// Vi stänger av workflow och sys_fields för att spara prestanda
taskGR.setWorkflow(false);
taskGR.autoSysFields(false);
// 7. Sätt det fält som ska uppdateras
taskGR.setValue('u_restricted_read_access', true);
// 8. Kör massuppdatering i en enda DB-operation
taskGR.updateMultiple();
// 9. Logga resultatet
gs.log(
'x_vgll_rail.add_restricted_access: Satt u_restricted_read_access = true för ' +
count + ' ärenden med utförargrupp ' + assignmentGroupName +
' (' + assignmentGroupSysId + ').',
'x_vgll_rail'
);
})(current, event);Here comes a screenshot that shows the configuration for the event:
Here comes a screenshot for the configuration of the script action:
So what is could be the issue for why the code in the Script Action is not running? Can you see something I cannot?
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
if event gets processed then only script action will run.
Try to use after insert BR on your table and trigger the event
OR
Why not use Async Insert and let it run in background asynchronously?
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Ankur Bawiskar hey! 🙂 What do you mean by "Why not use Async Insert and let it run in background asynchronously?"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I am referring to use async insert business rule
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Ankur Bawiskar even though this record producer doesn't create any records? It just updates historical/already created records.
