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.

Cancel button widget change active approvers into not required status

BenjaminY
Tera Contributor

Hello,

I am tasked with creating a cancel button that puts the current record into cancelled status and also putting active approvals on that record into not required status once the button is selected. I am not the most expierienced in portal/widget scripting.

Please advise:

HTML script:
<!-- Cancellation Modal Template -->
<div class="panel-heading bg-primary" ng-if="data.showCancel">Actions</div>
<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()"
ng-if="data.showCancel"
>Cancel Request
</button>
</div>
<!-- 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>
<div class="panel-footer text-right">
<button class="btn btn-primary" ng-click="c.closeModal()">Cancel</button>
</div>
</div>
</script>
Client Script:

api.controller = function($uibModal, $scope, spUtil, spModal) {
var c = this;
 
$scope.$on("record.updated", function(name, data) {
if (c.data.action == "cancelled") {
c.data.action = undefined;
}
spUtil.update($scope);
});
//Opens the pop-up for comments. JWD
c.openModalcancel = function() {
 
c.modalInstance = $uibModal.open({
templateUrl: 'modalTemplateCancel',
scope: $scope
})
 
/*
spModal.confirm("Are you sure you want to cancel this request?").then(function(confirmed) {
if(confirmed){
c.server.update({ action: 'cancelled', comments: c.data.comments || '' }).then(function(response) {
c.data.action = undefined;
 
if(response.withdrawalRequested){
spUtil.addInfoMessage("Cancellation was successful.", 3000);
} else {
spUtil.addErrorMessage("Could not cancel request, please try again later", 3000);
}
 
c.modalInstance.close();
});
}
}); */
};
//c.data.action = action;
//};
 
c.action = function(action) {
if(action == 'cancelled'){
if($scope.data.comments){
c.data.action = 'cancelled';
c.data.comments = $scope.data.comments;
c.server.update().then(function(response){
c.data.action = '';
});
} else {
spUtil.addErrorMessage('You must submit a justification for cancelling request.');
}
}
}
 
c.closeModal = function() {
c.modalInstance.close(); {
 
}
};
}

 


Server Script:

(function () {
    // Get table & sys_id
    data.table = input.table || $sp.getParameter("table");
    data.sys_id = input.sys_id || $sp.getParameter("sys_id");
 
    var grCCHTTask = new GlideRecord(data.table);
    if (!grCCHTTask.isValid()) return;
    if (!grCCHTTask.get(data.sys_id)) return;
 
    // Logic to determine whether to show cancel button
    if (
        data.table == "x_g_dh5_ccht_ccht_task" &&
        grCCHTTask.active == true &&
        (
            grCCHTTask.record_status == 'Supervisor Approval' ||
            grCCHTTask.record_status == 'SAC Approval' ||
            grCCHTTask.record_status == 'Program Manager Approval'
        ) &&
        grCCHTTask.requested_by == gs.getUserID()
    ) {
        data.showCancel = true;
    } else {
        data.showCancel = false;
    }
 
    // Cancel logic
    if (input && input.action === "cancelled") {
        var strCurrentUserName = gs.getUser().getDisplayName();
        var gdtCurrentTime = new GlideDateTime();
 
        grCCHTTask.comments = "Cancelled by " + strCurrentUserName + " on: " + gdtCurrentTime.getDisplayValueInternal();
        grCCHTTask.setWorkflow(false);
        grCCHTTask.setValue('record_status', 'Cancelled');
        grCCHTTask.update();
 
data.withdrawalRequested = true;
 
        // Log justification in work notes if provided
        if (input.comments) {
            grCCHTTask.work_notes = strCurrentUserName + " has requested to cancel the request with the following justification: " + input.comments;
            grCCHTTask.update();
        }
    }
 
    // Get record metadata for client-side rendering
    var gr = $sp.getRecord();
    if (gr) {
        var fields = $sp.getFields(gr, 'state,sys_created_on');
 
        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();
 
        var record = getRecordBeingApproved(gr);
        if (record) data.label = record.getLabel();
    }
 
    // Helper to get related approval record
    function getRecordBeingApproved(gr) {
        if (!gr.sysapproval.nil())
            return gr.sysapproval.getRefRecord();
 
        return gr.document_id.getRefRecord();
    }
})();
0 REPLIES 0