- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2019 02:27 AM
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.
- When an incident ticket is in "New" state, next states shown are: Active, Canceled
- When an incident ticket is in "Active" state, next states shown are: Awaiting User Info, Awaiting Problem, Awaiting Evidence, Awaiting Vendor, Resolved and Canceled
- When an incident ticket is in "Awaiting User Info" state, next states shown are: Active, Canceled
- When an incident ticket is in "Awaiting Problem" state, next states shown are: Active, Canceled
- When an incident ticket is in "Awaiting Vendor" state, next states shown are: Active, Canceled
- 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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2019 05:48 AM
Hi prabhmeet,
for state transition write onload client script as below-

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2019 06:57 AM
Hello Prabhmeet
Initially check for the state dropdown values if existing if not try to create new choices for the state once done
You can use on load script to initially calculate the state value
if(g_form.getValue('state')==1){
initially clear all the options and add only the required options as per your requirement
}
Try inserting log statements in between to check if its coming into correct if conditions for different values of state
by using info statements like g_form.addInfoMessage("entered into "+g_form.getValue("state")+"block");
Its not that complex try doing as stated above
There is an Out of the box property which you can change based on your requirement like after how many days your incident being resolved it should automatically moved to closed state
Else
You can write a scheduled job which checks daily comparing the difference between the resolved data and the current date Based on that you can have the incidents filtered and make them closed from the background
Hope this helps
Thanks,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2019 11:17 AM
thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2019 05:48 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2019 11:17 AM
thanks it worked, I was using addOption() instead of removeOption(). It worked now.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2019 06:14 PM
removeOption() might increase the number of lines in the code , you can use clearOptions() instead that will make future enhancements easy as well
Thanks,
Siva
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!