How to check Incident State value on client script (service portal widget)

JC S_
Mega Guru

We are developing a widget that we will add to the ticket page on Service Portal and we need to check using client script the value of State for example. We've tried calling gr.state and c.data.state (with matching data.state = gr.state on server side) but it is not working.

Sample part of code on client script:

if (c.data.state == 6 && c.data.active == true){

//code here

}

The counterpart on the server script:

data.active = gr.active;

data.state = gr.state;

1 ACCEPTED SOLUTION

I figured it out finally! I just need to added .toString() on the gr.active and gr.state part of the server script. Then on the client script, you need to enclose the values of the fields with apostrophes since they are now strings. See the working code below:



WORKING CLIENT SCRIPT:


function($uibModal, $scope) {


var c = this;


var state = c.data.state;


var active = c.data.active;



if (state == '6' && active == 'true'){


// Code to open a modal window here


}


if (state != '6' && state != '7' && active == 'true'){


// Code to open a modal window here


}


}



WORKING SERVER SCRIPT:


(function() {


// Get table & sys_id


data.table = input.table || $sp.getParameter("table");


data.sys_id = input.sys_id || $sp.getParameter("sys_id");



// Valid GlideRecord


gr = new GlideRecord(data.table);


if (!gr.isValid()) return;



// Valid sys_id


if (!gr.get(data.sys_id)) return;



// Check


if(data.table == 'incident' && gr.active == true && gr.state == 6 && gr.caller_id == gs.getUserID()){


data.state = gr.state.toString();


data.active = gr.active.toString();


}


})();



View solution in original post

7 REPLIES 7

I figured it out finally! I just need to added .toString() on the gr.active and gr.state part of the server script. Then on the client script, you need to enclose the values of the fields with apostrophes since they are now strings. See the working code below:



WORKING CLIENT SCRIPT:


function($uibModal, $scope) {


var c = this;


var state = c.data.state;


var active = c.data.active;



if (state == '6' && active == 'true'){


// Code to open a modal window here


}


if (state != '6' && state != '7' && active == 'true'){


// Code to open a modal window here


}


}



WORKING SERVER SCRIPT:


(function() {


// Get table & sys_id


data.table = input.table || $sp.getParameter("table");


data.sys_id = input.sys_id || $sp.getParameter("sys_id");



// Valid GlideRecord


gr = new GlideRecord(data.table);


if (!gr.isValid()) return;



// Valid sys_id


if (!gr.get(data.sys_id)) return;



// Check


if(data.table == 'incident' && gr.active == true && gr.state == 6 && gr.caller_id == gs.getUserID()){


data.state = gr.state.toString();


data.active = gr.active.toString();


}


})();



Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

or you could just change the client script from == '7' to == 7


Hi, we already did that as you can see on our original code and it did not work.