๐ Scenario-Based Question
Q1: You need to create a UI Action button that, when clicked, will automatically change the state of an incident to "Resolved" and also set the "Resolved by" field to the current user. How would you write the script for this UI Action?
Answer:
You can write the below script in a UI Action on the incident
table.
- ๐ Name:
Resolve Incident
- ๐ Table:
Incident
- ๐ท๏ธ Action name:
resolve_incident
- ๐ Form button: โ Checked
- ๐ฑ๏ธ OnClick: Leave blank (uses server-side script)
- โ๏ธ Condition:
current.state != 6
(only show if not already resolved)
๐ง Server-side Script:
if (!gs.hasRole('itil')) {
gs.addErrorMessage("You do not have permission to resolve incidents.");
return;
}
current.state = 6;
current.resolved_by = gs.getUserID();
current.resolved_at = new GlideDateTime();
current.update();
action.setRedirectURL(current);
โ Result: When clicked, the button resolves the incident, sets the current user as the resolver, and updates the resolution timestamp.
ย
Q2: Write a script to print the list of incidents grouped by assignment group.
Answer:
Below is a script using GlideAggregate
to group active incidents by assignment_group
and display their count:
var gr = new GlideAggregate("incident");
gr.addQuery("active", true);
gr.addAggregate("COUNT");
gr.groupBy("assignment_group");
gr.query();
while (gr.next()) {
var countOf = gr.getAggregate("COUNT");
var groupName = gr.getDisplayValue("assignment_group");
gs.print(countOf);
gs.print(groupName);
}
๐ง Explanation:
- ๐ Filters only active incidents.
- ๐ Groups results by
assignment_group
. - ๐งฎ Uses
GlideAggregate
for performance and count aggregation. - ๐ค Prints the count and name of each group to the log using
gs.print()
.