Join us Dec. 16 for The Community Hour, our first global virtual User Group event!

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

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

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

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

Mark Manders
Tera Patron

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.

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

 

Not applicable

easy and simple solution. Thank you.