The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Accenture Interview Question Asked !!

SandeepKSingh
Kilo Sage

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?

2 ACCEPTED SOLUTIONS

Ravi Gaurav
Giga Sage
Giga Sage

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/

View solution in original post

Runjay Patel
Giga Sage

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);

 

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Hi @SandeepKSingh ,

For resolving an incident, it's important to ensure that the Resolution Notes or Work Notes field is filled in, as it is a mandatory field in most configurations. Here's how you can create a UI Action button to automatically change the incident's state to "Resolved" and set the "Resolved by" field to the current user:

and try this ui action script

// UI Action Script

function resolveIncident() {
    // Check if the form is valid (especially mandatory fields)
    if (!g_form.isValid()) {
        return; // Stop execution if the form is invalid
    }

    // Get the current record's sys_id
    var sysId = g_form.getUniqueValue();

    // Add a work note (resolution note)
    var resolutionNote = g_form.getValue('close_notes'); // Get the value from the close notes field
        if(!resolutionNote){
                g_form.showErrorBox('close_notes', 'Resolution Note is mandatory.');
                return;
        }

    // GlideRecord to update the incident
    var grIncident = new GlideRecord('incident');
    if (grIncident.get(sysId)) {
        grIncident.setValue('state', 6); // Resolved state (check your instance for the correct value)
        grIncident.setValue('resolved_by', g_form.getUserId()); // Set resolved by to current user
        grIncident.work_notes = resolutionNote; // Add the resolution note
        grIncident.update();

        // Refresh the form to reflect the changes
        g_form.submit(); // Use submit for a full page refresh
        // OR g_form.save(); // Use save for an AJAX save (might not refresh related lists properly)
    }
}

Community Alums
Not applicable

Hi @SandeepKSingh ,

For resolving an incident, it's important to ensure that the Resolution Notes or Work Notes field is filled in, as it is a mandatory field in most configurations. Here's how you can create a UI Action button to automatically change the incident's state to "Resolved" and set the "Resolved by" field to the current user:

and try this ui action script

 

// UI Action Script

function resolveIncident() {
    // Check if the form is valid (especially mandatory fields)
    if (!g_form.isValid()) {
        return; // Stop execution if the form is invalid
    }

    // Get the current record's sys_id
    var sysId = g_form.getUniqueValue();

    // Add a work note (resolution note)
    var resolutionNote = g_form.getValue('close_notes'); // Get the value from the close notes field
        if(!resolutionNote){
                g_form.showErrorBox('close_notes', 'Resolution Note is mandatory.');
                return;
        }

    // GlideRecord to update the incident
    var grIncident = new GlideRecord('incident');
    if (grIncident.get(sysId)) {
        grIncident.setValue('state', 6); // Resolved state (check your instance for the correct value)
        grIncident.setValue('resolved_by', g_form.getUserId()); // Set resolved by to current user
        grIncident.work_notes = resolutionNote; // Add the resolution note
        grIncident.update();

        // Refresh the form to reflect the changes
        g_form.submit(); // Use submit for a full page refresh
        // OR g_form.save(); // Use save for an AJAX save (might not refresh related lists properly)
    }
}