🎓 Scenario-Based Question
Q1: Write a background script to resolve all low-priority incidents that are in the In Progress state.
Answer:
Below is the background script to identify and resolve all incidents with:
- 🟡 State = In Progress (
state=2
) - 🔽 Priority = Low (
priority=4
)
var gr = new GlideRecord('incident');
gr.addEncodedQuery('state=2^priority=4');
gr.query();
while (gr.next()) {
gr.state = 6; // Resolved
gr.resolution_code = 'Resolved by Background Script';
gr.update();
gs.print('Resolved incident: ' + gr.number);
}
gs.print('All low-priority In Progress incidents have been resolved.');
🧠 Explanation:
- 🔍
addEncodedQuery('state=2^priority=4')
filters incidents with In Progress state and Low priority. - ✅ Updates the
state
to Resolved (typically value 6) and adds a resolution note. - 🖨️ Prints each incident number that was resolved in the background.
💡 "Automate the routine, so you can focus on the impactful." ⚙️✨
Q2: In a workflow, we have a Run Script activity that takes around 10 minutes to execute, but it throws an error: Transaction Cancelled: Max execution time exceeded
. Why does this occur, and how can we resolve it?
Answer:
This issue occurs because ServiceNow enforces a transaction quota limit on how long a single server-side transaction can run.
- 🧭 By default, this is usually set between 2 to 5 minutes.
- 🛑 If any script takes longer than that, the platform automatically cancels it to prevent performance issues.
Resolution:
⚠️ While it is possible to increase the transaction timeout limit, it is not recommended as it can negatively impact instance performance and stability.
✅ Best Practice: Offload long-running scripts to run asynchronously in the background.
- Write heavy logic inside a Script Action (which runs in the background).
- From the workflow's Run Script activity, trigger this Script Action using an
event
. - This ensures the workflow continues without waiting and avoids hitting the transaction quota.
💡 Tip: Use asynchronous execution for background processing when the script logic exceeds a few minutes.