- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 07:01 AM
Can you please help me @Ahmmed Ali
As I had created Cancel Request Button Widget on Service Portal based on RITM (sc_req_item table) with below mentioned HTML Code, Server Script & Client Script.
Now it's working on RITM State field but need to modify that and make it work on RITM Stage field like when the RITM Stage is as highlighted in the below screen shot then the Cancel Request Button should appear otherwise it's should hide.
Tried with changing the Server Script with Stage but it's not executing.
Cancel Request Button:
RITM Stage Values:
Server Script:
(function() {
if (!input) {
data.show = false;
data.sys_id = $sp.getParameter('sys_id');
data.type = $sp.getParameter('table');
data.cancelled_msg = '';
var gr = $sp.getRecord();
if (gr.u_cancelled == 1 || gr.u_cancelled == 2) {
data.cancelled_msg = gs.getMessage('A request to cancel this ticket has been submitted.');
if (gr.state == 4) {
data.cancelled_msg = gs.getMessage('This ticket has been closed according to your cancellation request.');
}
}
if (data.type == 'sc_req_item') {
data.btn_text = gs.getMessage('Cancel Request');
data.show = canCancelReq(data.sys_id);
}
} else if (input.action == 'cancel') {
if (input.type == 'sc_req_item') {
cancelReq(input.sys_id);
}
}
function cancelReq(id) {
var req = new GlideRecord('sc_req_item');
if (req.get(id)) {
var sc_task = new GlideRecord('sc_task');
sc_task.addQuery('request_item', id);
sc_task.addActiveQuery();
sc_task.addNotNullQuery('assigned_to');
sc_task.query();
if (!sc_task.hasNext()) {
req.state = 4;
req.u_cancelled = 2;
req.approval = 'cancelled';
req.stage = "Request Cancelled";
req.comments = input.reason;
req.update();
var gr_approve = new GlideRecord('sysapproval_approver');
gr_approve.addQuery('sysapproval', id);
gr_approve.query();
while (gr_approve.next()) {
gr_approve.state = 'not_required';
gr_approve.comments = "Request has been cancelled by Requester";
gr_approve.update();
}
data.msg = gs.getMessage("This ticket has been successfully cancelled.");
} else {
// Exception for IDM requests (as tasks are generated before approval by design)
if (gs.getProperty("mars.idm.cancel.exceptions", "").indexOf(req.cat_item) >= 0 && req.approval != "approved") {
req.state = 4;
req.u_cancelled = 2;
req.stage = "Request Cancelled";
req.comments = input.reason;
req.update();
data.msg = gs.getMessage("This ticket has been successfully cancelled.");
}
}
}
}
function canCancelReq(id) {
var invalid_states = ['7', '3', '4'];
var userId = gs.getUserID();
var req = $sp.getRecord();
//if we have a record
if (req) {
var state = req.getValue('state');
var cancelled = req.getValue('u_cancelled');
//and it belongs to the user
if (req.request.requested_for == userId || req.opened_by == userId) {
//and it isn't already cancelled or being worked
if (cancelled != 1 && cancelled != 2 && invalid_states.indexOf(state) == -1) {
return true;
}
}
//check delegates
if (gs.getUserID() != req.request.requested_for && gs.getUserID() != req.opened_by) {
var delUtil = new global.MarsDelegateUtil();
var check = delUtil.checkIfActiveDelegate(req.request.requested_for, gs.getUserID());
if (!check && req.request.requested_for != req.opened_by) {
check = delUtil.checkIfActiveDelegate(req.opened_by, gs.getUserID());
}
if (check) {
//isn't already cancelled or being worked
if (cancelled != 1 && cancelled != 2 && invalid_states.indexOf(state) == -1) {
return true;
}
}
}
}
return false;
}
})();
Client Script:
function($scope, $uibModal, $location, spModal) {
/* widget controller */
var c = this;
function doCancel() {
//Second popup asking for Reason for Cancellation
spModal.open({
title: "Reason for Cancellation",
text: "\n",
size: "sl",
input: true,
value: c.reason,
buttons: [
{label:'Cancel', cancel: true},
{label:'Confirm', primary: true}
]
}).then(function(reason) {
c.reason = reason;
c.server.get({
action: 'cancel',
type: c.data.type,
sys_id: c.data.sys_id,
reason: c.reason
}).then(function(r) {
var searchParms = $location.search();
searchParms.refresh = true;
$location.search(searchParms);
});
});
}
// First Popup Yes or No
c.doCancel = function() {
c.modalInstance = $uibModal.open({
backdrop: "static",
templateUrl: 'popupTemplate',
size: 'md',
scope: $scope
});
};
//Action triggered after you click yes or no from first popup
c.closeModal = function(cancel) {
c.modalInstance.close();
if (cancel == true) {
doCancel();
}
};
};
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 07:27 AM
Hello @Arjun Reddy Yer
Update the server script as below:
var valid_stages = ['fulfillment', 'delivery', 'not_started']; //add all stages with their back end values for whom you want to show the cancel button
var stage = req.getValue('stage');
if (cancelled != 1 && cancelled != 2 && valid_stages.indexOf(stage) != -1)
Thank you,
Ali
Thank you,
Ali

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 07:27 AM
Hello @Arjun Reddy Yer
Update the server script as below:
var valid_stages = ['fulfillment', 'delivery', 'not_started']; //add all stages with their back end values for whom you want to show the cancel button
var stage = req.getValue('stage');
if (cancelled != 1 && cancelled != 2 && valid_stages.indexOf(stage) != -1)
Thank you,
Ali
Thank you,
Ali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 07:48 AM
🙏🙏🙏🙏Thanks thanks thanks thanks a lot for your help it's working now as expected thanks once again for help 🙏🙏🙏🙏
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2023 04:45 AM
Hello Arjun,
Can you also please share your HTML code? We have a similar requirement and your code is of great help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2023 12:04 AM
HTML Code:
<div >
<button class="btn btn-danger full-width" ng-click="c.doCancel()" ng-if="c.data.show == true && data.cancelled_msg == '' ">
{{c.data.btn_text}}
</button>
<div ng-if="data.cancelled_msg != '' " class="panel b">
<div class="panel-heading bg-danger mars-danger-heading">
<i class="fa fa-exclamation cancel-icon"></i><span >{{c.data.cancelled_msg}}</span>
</div>
</div>
<div class="cancelled-msg">
</div>
</div>
<script type="text/ng-template" id="popupTemplate" data-backdrop="static" data-keyboard="false">
<div class="panel panel-default" >
<div class="panel-body text-center">
${Are you sure you would like this Ticket Cancelled?}
</div>
<div class="panel-footer text-center">
<button class="btn btn-primary" ng-click="c.closeModal(true)">${Yes}</button>
<button class="btn btn-default" ng-click="c.closeModal(false)">${No}</button>
</div>
</div>
</script>
Server script:
(function() {
if (!input) {
data.show = false;
data.sys_id = $sp.getParameter('sys_id');
data.type = $sp.getParameter('table');
data.cancelled_msg = '';
var gr = $sp.getRecord();
if (gr.u_cancelled == 1 || gr.u_cancelled == 2) {
data.cancelled_msg = gs.getMessage('A request to cancel this ticket has been submitted.');
if (gr.state == 4) {
data.cancelled_msg = gs.getMessage('This ticket has been closed according to your cancellation request.');
}
}
if (data.type == 'sc_req_item') {
data.btn_text = gs.getMessage('Cancel Request');
data.show = canCancelReq(data.sys_id);
}
} else if (input.action == 'cancel') {
if (input.type == 'sc_req_item') {
cancelReq(input.sys_id);
}
}
function cancelReq(id) {
var req = new GlideRecord('sc_req_item');
if (req.get(id)) {
var sc_task = new GlideRecord('sc_task');
sc_task.addQuery('request_item', id);
sc_task.addActiveQuery();
sc_task.addNotNullQuery('assigned_to');
sc_task.query();
if (!sc_task.hasNext()) {
req.state = 4;
req.u_cancelled = 2;
req.approval = 'cancelled';
req.stage = "Request Cancelled";
req.u_request_cancelled_by_requester = "yes";
//req.work_notes = "Request has been cancelled by Requester.";
req.comments = input.reason;
req.update();
var gr_approve = new GlideRecord('sysapproval_approver');
gr_approve.addQuery('sysapproval', id);
gr_approve.query();
while (gr_approve.next()) {
gr_approve.state = 'not_required';
gr_approve.comments = "Request has been cancelled by Requester";
gr_approve.update();
}
data.msg = gs.getMessage("This ticket has been successfully cancelled.");
} else {
// Exception for IDM requests (as tasks are generated before approval by design)
if (gs.getProperty("mars.idm.cancel.exceptions", "").indexOf(req.cat_item) >= 0 && req.approval != "approved") {
req.state = 4;
req.u_cancelled = 2;
req.stage = "Request Cancelled";
// req.work_notes = "Request has been cancelled by Requester.";
// req.comments = "Request has been cancelled by Requester.";
req.u_request_cancelled_by_requester = "yes";
req.comments = input.reason;
req.update();
data.msg = gs.getMessage("This ticket has been successfully cancelled.");
}
}
}
}
function canCancelReq(id) {
var invalid_states = ['7', '3', '4'];
var valid_stages = ['waiting_for_approval', 'waiting_for_manager_approve', 'waiting_for_service_manager_approve', 'waiting_for_team_lead_approve', 'waiting_for_it_security_approve', 'approval', 'CIO Approval', 'Dept. Head Approval'];
var userId = gs.getUserID();
var req = $sp.getRecord();
//if we have a record
if (req) {
var state = req.getValue('state');
var cancelled = req.getValue('u_cancelled');
var stage = req.getValue('stage');
//and it belongs to the user
if (req.request.requested_for == userId || req.opened_by == userId) {
//and it isn't already cancelled or being worked
//if (cancelled != 1 && cancelled != 2 && invalid_states.indexOf(state) == -1)
if (cancelled != 1 && cancelled != 2 && valid_stages.indexOf(stage) != -1){
return true;
}
}
}
return false;
}
})();
Client controller:
function($scope, $uibModal, $location, spModal) {
/* widget controller */
var c = this;
function doCancel() {
//Second popup asking for Reason for Cancellation
spModal.open({
title: "Reason for Cancellation",
text: "\n",
size: "sl",
input: true,
value: c.reason,
buttons: [
{label:'Cancel', cancel: true},
{label:'Confirm', primary: true}
]
}).then(function(reason) {
c.reason = reason;
c.server.get({
action: 'cancel',
type: c.data.type,
sys_id: c.data.sys_id,
reason: c.reason
}).then(function(r) {
var searchParms = $location.search();
searchParms.refresh = true;
$location.search(searchParms);
});
});
}
// First Popup Yes or No
c.doCancel = function() {
c.modalInstance = $uibModal.open({
backdrop: "static",
templateUrl: 'popupTemplate',
size: 'md',
scope: $scope
});
};
//Action triggered after you click yes or no from first popup
c.closeModal = function(cancel) {
c.modalInstance.close();
if (cancel == true) {
doCancel();
}
};
};