- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2024 12:22 AM
Q :- 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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2024 12:35 AM
Hello @SandeepKSingh
You can write the below script in UI Action.
Fill out the necessary fields:
- Name: XXX
- Table: Incident
- Action name: resolve_incident
- Form Button: Checked
- Onclick: Leave blank (to use the server-side script).
- Condition: current.state != 6 (to display the button only if the incident is not already resolved).
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);
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2024 12:43 AM
Hi @SandeepKSingh ,
create new ui action and use below script to achieve this.
(function executeAction() {
var incident = current; // The current incident record
// Set the state to 'Resolved' (make sure the value is correct for 'Resolved')
incident.state = 6; // Typically, 6 corresponds to 'Resolved' in the Incident State field (check your specific values in your system).
// Set the "Resolved by" field to the current user
incident.resolved_by = gs.getUserID(); // Gets the current user's Sys ID
// Optionally, set the resolved date (if you want to track when the incident was resolved)
incident.resolved_at = new GlideDateTime(); // Set the resolved date/time to the current timestamp
// Update the record
incident.update(); // Save the changes to the incident record
// Optionally, you can display a message to the user confirming the action
gs.addInfoMessage('Incident has been marked as Resolved.');
})(current);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2024 12:35 AM
Hello @SandeepKSingh
You can write the below script in UI Action.
Fill out the necessary fields:
- Name: XXX
- Table: Incident
- Action name: resolve_incident
- Form Button: Checked
- Onclick: Leave blank (to use the server-side script).
- Condition: current.state != 6 (to display the button only if the incident is not already resolved).
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);
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2024 12:36 AM
Looking at the questions you are asking, maybe it would be better to first dig into learning ServiceNow yourself instead of asking the community to do your work for you. If you can't even manage something as simple as this, what are you even doing in an interview for this job?
Start learning the platform first, or be honest with the employer that you are coming in without any knowledge but would love to learn it.
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2024 12:43 AM
Hi @SandeepKSingh ,
create new ui action and use below script to achieve this.
(function executeAction() {
var incident = current; // The current incident record
// Set the state to 'Resolved' (make sure the value is correct for 'Resolved')
incident.state = 6; // Typically, 6 corresponds to 'Resolved' in the Incident State field (check your specific values in your system).
// Set the "Resolved by" field to the current user
incident.resolved_by = gs.getUserID(); // Gets the current user's Sys ID
// Optionally, set the resolved date (if you want to track when the incident was resolved)
incident.resolved_at = new GlideDateTime(); // Set the resolved date/time to the current timestamp
// Update the record
incident.update(); // Save the changes to the incident record
// Optionally, you can display a message to the user confirming the action
gs.addInfoMessage('Incident has been marked as Resolved.');
})(current);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2025 12:19 AM
easy and simple solution. Thank you.