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

siva_
Giga Guru

Check every current state using g_form.getValue() and using addOption and removeOption of g_form have these states as the dropdown choices at respective states 

That should work if not with state flows 

 

Thanks,

Siva

Could you write it for one of the conditions, just so I can understand and write properly?

Here a sample of code to be used:

if(g_form.getValue('state') == '40'){

   g_form.removeOption('state', '10');
   g_form.addOption('state', '50', 'Execution');
}

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

Cheers
Alberto

I have added the script I have written in the Question, Can you check what is the error because it is not working.