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

sb1186
Kilo Guru

Hi,



Try placing the values into '' as below:



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



Also, what exactly are c.data.state and c.data.active? Are these the field names?



Regards


Supriya Bisht


Hi,



We tried adding the '' on the values but it still did not work. We added the counterpart of those fields from the server script. So basically, we query it using server script then on the client script we want to check the values of those fields.


Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

On the server script.. add console.log(data.state) to see in the webbrowser what value the data has...


Since you ain't sharing the whole script it's kind of hard to see whats wrong


We are still unable to make this work. We added the console.log as you said and we can see it in the browser console, but we need these values to be parsed by the client script as shown below.



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


}


}



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;


console.log(data.state);


data.active = gr.active;


console.log(data.active);


}


})();