We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Server Javascript error Cannot read property “action” from undefined

PaKe
Kilo Sage

Hey,

I created a little widget where I have a Select Box and a button. If I click the button the selected record should be updated.

Unfortunately I always get four error messages when the page is loading. 

find_real_file.png

I'm working in my own Scoped Application but the table is also in this application.

 

HTML Template:

<div class="panel-body">
	<div>
		<b>${Ticket}: </b>
      <select style="width:300px" id='ticket' type='String' class="form-control" ng-model="c.data.tickets">
        <option ng-repeat="ticket in c.data.tickets" value="{{ticket}}">{{ticket}}</option>
      </select>
  		<button style="margin-top:10px" class="btn btn-primary rounded m-l-lg padder-xl" ng-click="c.uiAction('close')">
        	${Close Ticket}
      </button>
  </div>
</div>

 

Client Script:

function() {
  var c = this;
  c.uiAction = function(action){	
		c.data.action = action;
 		c.data.training = document.getElementById('ticket').value;
 		c.server.update().then(function() {
 		c.data.action = undefined;
 		})
 }
}

Server Script:

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
	data.tickets = [];
	
	var gr = new GlideRecord('x_ntt47_human_care_medical_advice');
		gr.addQuery('state', '1');
		var grOr = gr.addQuery('patient', gs.getUserID());
		grOr.addOrCondition('attending_doctor', gs.getUserID());
		gr.query();
		while(gr.next()){
			data.tickets.push(gr.number.getDisplayValue());
		}
	
	if (input.action == 'close') {
		var gr2 = new GlideRecord('x_ntt47_human_care_medical_advice');
		gr2.addQuery('number', input.ticket);
		gr2.query();
		while(gr2.next()){
			gr2.state = '2';
			gr2.update();
			gs.addInfoMessage('Closed');
		}
	}
})();
10 REPLIES 10

Let me try the same code in my PDI and get back to you.

I tried something on incident table, you can try

HTML

<div class="panel-body">
	<div>
		<b>${Ticket}: </b>
      <select style="width:300px" id='ticket' type='String' class="form-control">
        <option ng-repeat="ticket in data.tickets" value="{{ticket}}">{{ticket.number}}</option>
      </select>
  		<button style="margin-top:10px" class="btn btn-primary rounded m-l-lg padder-xl" ng-click="c.uiAction()">
        	${Close Ticket}
      </button>
  </div>
</div>

 

Client Script

api.controller=function() {
	/* widget controller */
	var c = this;
	c.uiAction = function(){	
		c.data.action = 'close';
		//c.data.training = document.getElementById('ticket').value;
		c.server.update().then(function() {
			c.data.action = '';
		})
	};

};

 

 

Server

(function() {
	/* populate the 'data' object */
	/* e.g., data.table = $sp.getValue('table'); */
	data.tickets = [];

	var gr = new GlideRecord('incident');
	//gr.addQuery('state', '1');
	///var grOr = gr.addQuery('patient', gs.getUserID());
	//grOr.addOrCondition('attending_doctor', gs.getUserID());
	gr.query();
	while(gr.next()){
		var a ={}
		
		a.number = gr.number;
		data.tickets.push(a);
	}
	if(input){
		if (input.action == 'close') {
			var gr2 = new GlideRecord('incident');
			//gr2.addQuery('number', input.ticket);
			gr2.query();
			if(gr2.next()){
				gr2.state = '2';
				gr2.update();
				gs.addInfoMessage('Closed');
			}
		}
	}
})();

Thank you @Pranav Bhagat, but I still get the errors.

Could it be that I get these errors because the widget and the table is in an own scoped application?

How did you solved this issue ? I am getting same . 

Vijayalakshmi P
Kilo Sage

Hi @PaKe - Were you able to resolve this issue pls?