Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Redirect from widget post server side operations.

srivatsat
Mega Sage

Hi Team,

I am trying to enable a cancel request option from Service portal. Have created a widget for this and introduced a button. I would like to redirect to requests page once user click (Button)/cancels the request.

Below are my code snippet.

HTML

<div>
    <div class="panel panel-primary">
    <div class="panel-heading">Actions</div>
    <div class="panel-body">
      <button type="button" class="btn btn-default btn-block" ng-click="c.uiAction('cancel')">Cancel</button>
    </div>
  </div>
</div>
  

Client Side

function($scope, spUtil) {
  var c = this;
	
	spUtil.recordWatch($scope, $scope.data.table, "sys_id=" + $scope.data.sys_id);
	
	c.uiAction = function(action) {
		c.data.action = action;
		c.server.update().then(function() {
			c.data.action = undefined;
		});
	}
	
}

 

 

Server side

(function() {
 
	// Get table & sys_id
	data.table = input.table || $sp.getParameter("table");
	data.sys_id = input.sys_id || $sp.getParameter("sys_id");
	//gs.addInfoMessage(data.table);
 
	// Valid GlideRecord
	gr = new GlideRecord(data.table);
	if (!gr.isValid())
		return;
 
	// Valid sys_id
	if (!gr.get(data.sys_id))
		return;
	
	data.request_state = gr.getValue('request_state');
	data.ShowCancel = (data.request_state == "requested") ? true : false;
	
	if (input && input.action) {
		var action = input.action;		
 
		// If Incident table
		if (data.table == 'sc_request') {
			if (action == 'cancel') {
				
				gr.request_state="closed_cancelled";
				gr.state="Closed Incomplete";
				gr.stage="closed_incomplete";
				gr.update();
				
				 gs.addInfoMessage('Request was cancelled.');
				 //gs.setRedirect('/sp?id=requests');
			}
		}
	}	
 
})();

 

Please let me know how can i achieve this?

Thanks

Sri.

 

1 ACCEPTED SOLUTION

Bhagya Lakshmi
Mega Guru

Hi,

You will write like this in client-side script.

function($scope, spUtil) {
 var c = this; spUtil.recordWatch($scope, $scope.data.table, "sys_id=" + $scope.data.sys_id); 
c.uiAction = function(action) { 
c.data.action = action; 
c.server.update().then(function() { 
c.data.action = undefined;
location.href="id=<page_id>"
 });
 } 
}

 

View solution in original post

3 REPLIES 3

Bhagya Lakshmi
Mega Guru

Hi,

You will write like this in client-side script.

function($scope, spUtil) {
 var c = this; spUtil.recordWatch($scope, $scope.data.table, "sys_id=" + $scope.data.sys_id); 
c.uiAction = function(action) { 
c.data.action = action; 
c.server.update().then(function() { 
c.data.action = undefined;
location.href="id=<page_id>"
 });
 } 
}

 

Thank you it worked, i had tried this before, was not sure what i missed it.

is their is any way to achieve same thing using server script