Call a server function from a button click in a widget

kazidon
Giga Contributor

Hi, not sure how to word this but I am creating a widget and I want a button to call a specific server side function and not run the entire server script. Not sure if I can call that directly from the client script or if I need to pass a variable over with c.data....

here is what I have so far

HTML

<div>
<!-- your widget template -->
  <p id="title">My Dev Tasks </p>
  <table border="1px">
   
    <tr>
    <th><button ng-click="sortBy('number')">Number</button> </th>
      <th>Short Description</th>
      <th>Description</th>
      <th>Update Sets</th>
      <th><button ng-click="sortBy('points')">Points</button> </th>
      <th>Update Record</th>
      <th>Send To QA</th>
    </tr>
    <tr ng-repeat="ticket in data.tickets | orderBy:orderField">
      <td id="number">{{ticket.number}}</td>
      <td>{{ticket.short_description}}</td>
      <td ng-if="modifingRecord.sys_id == ticket.sys_id"><textarea ng-model="ticket.description"></textarea></td>
      <td ng-if="modifingRecord.sys_id != ticket.sys_id">{{ticket.description}}</td>
      <td ng-if="modifingRecord.sys_id == ticket.sys_id"><textarea ng-model="ticket.update_set"></textarea></td>
      <td ng-if="modifingRecord.sys_id != ticket.sys_id">{{ticket.update_set}}</td>
      <td ng-if="modifingRecord.sys_id == ticket.sys_id"><textarea ng-model="ticket.points"></textarea></td>
      <td ng-if="modifingRecord.sys_id != ticket.sys_id">{{ticket.points}}</td>
      <td ng-if="modifingRecord.sys_id != ticket.sys_id"><button class="btn btn-primary" ng-click="modifyRec(ticket)">Update Record</button></td>
      <td ng-if="modifingRecord.sys_id == ticket.sys_id"><button class="btn btn-danger" ng-click="saveRec(ticket)">Save</button></td>
      <td ng-if="modifingRecord.sys_id != ticket.sys_id"><button class="btn btn-primary" ng-click="sendToQA(ticket)">Send To QA</button></td>
    </tr>
    
  </table>
</div>	

 

 

Client Script

function($scope,spUtil) {
  /* widget controller */
  var c = this;
	$scope.sortBy = function(field){
		$scope.orderField = field;
	}
		$scope.modifyRec = function(ticket){
		$scope.modifingRecord = angular.copy(ticket);
	}
	$scope.saveRec = function(ticket){
		$scope.data.modifiedRec = angular.copy(ticket);
	spUtil.update($scope);
		$scope.modifingRecord = null;
	}
	
	$scope.sendToQA = function(ticket){
		$scope.data.modifiedRec = angular.copy(ticket);
	}
	  spUtil.recordWatch($scope, "rm_scrum_task", "active=true", function(name, data) {
		spUtil.update($scope);
	});
}

 

 

Server Script

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
 var parentTable = 'rm_story';
	var tableSys = options.table.toString();
	var tableRec = new GlideRecord('sys_db_object');
	tableRec.get('sys_id',tableSys);
	var tableName = tableRec.name.toString();
	if(tableName == '' || tableName == undefined){
		tableName = 'rm_scrum_task';
	}

	data.tickets = [];
	var ticketRec = new GlideRecord(tableName);
	ticketRec.addActiveQuery();
	ticketRec.addQuery('short_description', 'CONTAINS', 'Development');
	ticketRec.addQuery('assigned_to', gs.getUserID());
	ticketRec.query();
	while(ticketRec.next()){
		var ticket = {};
		ticket.number = ticketRec.number.toString();
		ticket.short_description = ticketRec.short_description.toString();
		ticket.sys_id = ticketRec.sys_id.toString();
		ticket.description = ticketRec.description.toString();
		ticket.update_set = ticketRec.u_update_sets.toString();
		ticket.priority = ticketRec.priority.toString();
		ticket.points = ticketRec.parent.story_points.toString();
		data.tickets.push(ticket);
	}
	
	if(input.qa)
		{
			var gr = new GlideRecord(tableName);
		  gr.addQuery('sys_id',input.modifiedRec.sys_id);
		  gr.query();
			if(gr.next())
				{
					gr.description = "Send to QA";
				}
		}
	
	if(input){
		var modifyTic = new GlideRecord(tableName);
		modifyTic.addQuery('sys_id',input.modifiedRec.sys_id);
		modifyTic.query();
		if(modifyTic.next()){
			modifyTic.description = input.modifiedRec.description;
			modifyTic.u_update_sets = input.modifiedRec.update_set;
			var test = new GlideRecord(parentTable);
			test.addQuery('sys_id', modifyTic.parent);
			test.query();
			while(test.next())
				{
					test.story_points = input.modifiedRec.points;
					test.update();
				}
			modifyTic.update();
		}
	}	
})();

 

so basically I want a function to be run in my server side script when Send to QA is clicked.

2 REPLIES 2

kente
Tera Guru

Below could work.

 

if(!input){

//the code you dont want to run

}

Jeffrey Siegel
Mega Sage

IDK if you still are looking for an answer, or if someone else is looking at this, but i was able to figure this out by simply creating an html button

 

<div>
  <span id="UI Change" role="button" ng-click="changeUI()" title="Change UI" class="btn btn-info">Change UI</button>
</div>

 

Client controller:

function($scope, spUtil) {
    /* widget controller */
    var c = this;
    $scope.changeUI = function() {
		c.data.trigger = true;  //sets the trigger to true for the server side to process special server-side code when button is pressed
        spUtil.update($scope); //tells server side to re-calculate its scripts along with the new c.data.trigger now being set to true.
    }
}

server script:

    if (input.trigger) {
//insert server side code you want to have evaluated only when the button is pressed, it will not be processed upon widget load.  Note all other server-side code thats not within this if statement will also be re-triggered.
}