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.

how to restrict cancel button on service portal for a particular catalog items

Arjun Reddy Yer
Mega Sage

Can you please help me @Ahmmed Ali @Vasantharajan N 

 

As I had created Cancel Request Button Widget on Service Portal based on RITM (sc_req_item table) with below mentioned Server Script.

Now I need to restrict the Cancel Button for a particular catalog items.

 

Cancel Request Button:

ArjunReddyYer_0-1681461865308.png

 

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 {

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 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;
}
}

//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;
}

})();

 

9 REPLIES 9

Community Alums
Not applicable

@Arjun Reddy Yer First store sys_id of your catalog form loading into data object which you want to hide.

And you can use ng-hide='c.data.variable_store_sys_id==sys_id_of_form_hidding' on button attributes into html tag.

There are also other way but i can think of this a one of easy way.

Hope this will help you.

As I had tried with the below change in the HTML Code as per your suggestion. But it's not working.

As for the menioned Catalog item sys id only the Cancel Button should be visible @Ahmmed Ali @Vasantharajan N 

 

HTML Code:

<div >
<button class="btn btn-danger full-width" ng-click="c.doCancel()" ng-show="c.data.variable_store_sys_id == '4f90e891db25595053fad4bdd3961957'" && 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>

 

Ahmmed Ali
Giga Sage
Giga Sage

Hello @Arjun Reddy Yer 

 

Add below statement in Server side script after 

var gr = $sp.getRecord();

 data.cancellable_items = ["SYS_ID1", "SYS_ID2"]; //here you need to add sys_ids of catalog items for which you cant to show cancel button

data.show_cancel_button = data.cancellable_items.indexOf(gr.getValue("cat_item") != -1);

 

 

Then in html, you can add ng-if="c.data.show_cancel_button"

 

Thank you,

Ali

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

If I want to retrict for a particular Category which contains Catalog items

For example under Hardware Category there are mutliple (15) Catalog items for those Catalog items Cancel Button should be visible.

 

For Software Category which contains multiple (20) Catalog items Cancel Button should be hide.

 

For IT Support Category which contains multiple (30) Cancel Button must be visible.

 

Can you help me as I had tried with below script but it's not working as expected

 

data.cancellable_items = ["0674da48dbc41110f1795843f396195d", "5ca6e64ddb79151053fad4bdd3961939"]; //sys_ids of Category for which cancel button should be hide

data.show_cancel_button = data.cancellable_items.indexOf(gr.getValue("sc_category") != -1);