Client script for Incident State transition

prabhmeet
Giga Expert

Hi,

I am new to Servicenow. I have to perform the following task -

Design the below state transition for incident. Create the state if not available.

  1. When an incident ticket is in "New" state, next states shown are: Active, Canceled
  2. When an incident ticket is in "Active" state, next states shown are: Awaiting User Info, Awaiting Problem, Awaiting Evidence, Awaiting Vendor, Resolved and Canceled
  3. When an incident ticket is in "Awaiting User Info" state, next states shown are: Active, Canceled
  4. When an incident ticket is in "Awaiting Problem" state, next states shown are: Active, Canceled
  5. When an incident ticket is in "Awaiting Vendor" state, next states shown are: Active, Canceled
  6. When an incident ticket is in "Resolved" state, next state shown is: Active

When an incident ticket is in "Resolved" state, system automatically closes the ticket after 3 days.

I have created the States which were not present using Choice List. I know this can be done using State Models and state transitions (I have done it using State model module) but I need to do this using Client Script.

The script I have written - 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//Type appropriate comment here, and begin script below

var _state = g_form.getValue('state');

if(_state == '1'){
g_form.clearOptions('state');
g_form.addOption('state','-10','Active');
g_form.addOption('state','8','Cancelled');
}
if(_state == '-10'){
g_form.clearOptions('state');
g_form.addOption('state','-11','Awaiting User Info');
g_form.addOption('state','-12','Awaiting problem');
g_form.addOption('state','-13','Awaiting Evidence');
g_form.addOption('state','-14','Awaiting Vendor');
g_form.addOption('state','8','Cancelled');
g_form.addOption('state','6','Resolved');
}

if(_state == '-11'){
g_form.clearOptions('state');
g_form.addOption('state','-10','Active');
g_form.addOption('state','8','Cancelled');
}

if(_state == '-12'){
g_form.clearOptions('state');
g_form.addOption('state','-10','Active');
g_form.addOption('state','8','Cancelled');
}

if(_state == '-14'){
g_form.clearOptions('state');
g_form.addOption('state','-10','Active');
g_form.addOption('state','8','Cancelled');
}

if(_state == '6'){
g_form.clearOptions('state');
g_form.addOption('state','-10','Active');
}

else{
g_form.clearOptions('state');
}
}

This is not working. Can someone please help with the script?

 

1 ACCEPTED SOLUTION

Vishal Khandve
Kilo Sage

Hi prabhmeet,

for state transition write onload client script as below-

function onLoad() {
//Type appropriate comment here, and begin script below
var gr= g_form.getValue('state');
if(gr==1)                   //new state
{
g_form.removeOption('state', 3);      //in_progress state
g_form.removeOption('state', 6);     // resolved state
g_form.removeOption('state', 7);     //closed state
}
else if(gr==2)                 //assigned_to state
{
g_form.removeOption('state', 1);
g_form.removeOption('state', 6);
g_form.removeOption('state', 7);
}
else if(gr==3)
{
g_form.removeOption('state', 1);
g_form.removeOption('state', 2);
g_form.removeOption('state', 7);
}
else if(gr==6)
{
g_form.removeOption('state', 1);
g_form.removeOption('state', 2);
g_form.removeOption('state', 3);
}
}

 for auto close incident 

write onafter BR

autoCloseIncidents();

function autoCloseIncidents() {
var ps = gs.getProperty('glide.ui.autoclose.time'); //in sys_properties set value as 3
var pn = parseInt(ps);

if (pn > 0) {
var gr = new GlideRecord('incident');
gr.addQuery('incident_state', '6');  
gr.addQuery('sys_updated_on', '<', gs.daysAgoStart(pn));
gr.query();
while(gr.next()) {
gr.incident_state = '7';
gr.close_code = 'No Response';
gr.close_notes = 'There was no response from Caller hence Incident automatically closed after ' + pn + ' days.';
gr.active = false;
gr.update();
}
}
}

 

Thank you!

 Please, remember to mark Correct or Helpful if you find my response useful.

View solution in original post

11 REPLIES 11

But clearOptions() will remove all the options in the dropdown and it will come up as blank right?

I don't want that, as you can see in the question, I want like when state in New, two options Active and Cancelled should be present, So i used removeOptions() to remove all the other options. Similary for all the other states.

rishireddy
Mega Guru

Hi

Just to post alternate OOB solution for state transition without scripts

You can use OOB state transition model and hide/show the states with Enter/Exit conditions on the sys_state_transition form.

This will hide/show the states on form and list layout as per given conditions and it is ServiceNow upgrade proof

 

Please Hit Like/Helpful based on the response, so that it may help others to quickly find a solution for the related issue

Regards,

Rishi