Service portal Cancel Button change approval state to Cancelled

BenjaminY
Tera Contributor

Hello,

I am tasked with creating a Cancel button on the service portal for my custom table. 
I have the button but I am having a hard time getting the button to change the approval state to "Cancelled"
Please advise:

HTML:
<h4 class="panel-title" ng-if="c.data.state == 'cancelled'">${Request has been cancelled} <sn-time-ago timestamp="::c.data.sys_updated_on" /></h4>
<div>
<button class="btn btn-danger" ng-click="c.openModalcancel()">Cancel Request</button>

<!-- Cancellation Modal Template -->
<script type="text/ng-template" id="modalTemplateCancel">
<div class="panel panel-default">
<div class="panel-body">
<div class="form-group">
<label> Justification for Cancelling Request </label>
<textarea class="form-control" rows="4" ng-model="c.data.comments" required></textarea>
</div>
<div class="form-group">
<button ng-disabled="c.data.comments == ''" class="btn btn-primary btn-lg" ng-click="c.action('cancelled');c.closeModal();">${Submit}</button>
</div>
<div class="panel-footer text-right">
<button class="btn btn-primary" ng-click="c.closeModal()">Cancel</button>
</div>
</div>
</div>
</script>

Client Script:

function ($scope, $uibModal) {
  var c = this;
 
  c.data.comments = "";
c.action = function(state) {
    console.log("State update requested:", state);
    c.data.op = state;
    c.data.state = state;
    c.server.update().then(function() {
    c.closeModal();
    c.server.get();
  });
};
//Opens the pop-up for comments. JWD
  c.openModal = function() {
    c.modalInstance = $uibModal.open({
      templateUrl: 'modalTemplate',
      scope: $scope
    });
  };
 
  c.openModalcancel = function() {
    c.modalInstance = $uibModal.open({
      templateUrl: 'modalTemplateCancel',
      scope: $scope
    });
  };
 
  c.closeModal = function() {
     c.modalInstance.close(); {
      
    }
  };
}


Server Script:

var gr = $sp.getRecord();
 
if (input && input.op && gr) { 
console.log(input.op)
gr.comments = input.comments;
//gr.document_id.comments = input.comments
//gr.document_id.work_notes = input.comments
gr.state = input.op;
gr.update();
var gr_ccht = new GlideRecord('x_g_dh5_ccht_ccht_task');
gr_ccht.get(gr.document_id);
gr_ccht.comments = input.comments;
gr_ccht.work_notes = input.comments;
gr_ccht.update();
 
  var gr = new GlideRecord('x_g_dh5_ccht_ccht_task');
  if (gr.get(input.sys_id)) {
    data.state = gr.state;
    // Any other fields
}
}
 
var fields = $sp.getFields(gr, 'state,sys_created_on');
 
if (gr) {
if (gr.sys_mod_count > 0)
fields.push($sp.getField(gr, 'sys_updated_on'));
 
data.fields = fields;
data.state = gr.state.toString();
data.sys_updated_on = gr.sys_updated_on.toString();
data.sys_id = gr.getUniqueValue();
data.table = gr.getTableName();
data.label = getRecordBeingApproved(gr).getLabel();
}
 
function getRecordBeingApproved(gr) {
if (!gr.sysapproval.nil())
return gr.sysapproval.getRefRecord();
 
return gr.document_id.getRefRecord();
}
if (input && input.op == 'cancelled' && gr) {
    var userName = gs.getUserDisplayName(); // e.g., "John Doe"
    var justification = input.comments;
 
    // Add a work note for the Program Manager
    var note = userName + " has requested to cancel the request with the following justification: " + justification;
    gr.work_notes = note;
 
    // Set status and update record
    gr.state = 'cancelled';
    gr.update();
 
 
}

BenjaminY_0-1747268630420.png

 

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@BenjaminY 

you want this to be shown on form widget in portal?

If yes then you can refer this

Need to modify Cancel Button on Service Portal and make it to work on RITM Stage Field 

Confirmation box on click of Cancel button in Service Portal 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

The widget button is showing as it should. My request was for the state field to be put into "Cancelled" status