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.

Hide widget on service portal based on RITM stage

Snow consultan1
Tera Contributor

Hi All,

 

I have a requirement to hide widget based on RITM stage in service portal. 

I have server script as below

Server script

(function() {

// Get table & sys_id
data.table = input.table || $sp.getParameter("table");
data.sys_id = input.sys_id || $sp.getParameter("sys_id");

// Valid GlideRecord
gr = new GlideRecord(data.table);
if (!gr.isValid())
return;

// Valid sys_id
if (!gr.get(data.sys_id))
return;

if (input && input.action) {
var action = input.action;
// If request item table table
if (data.table == 'sc_req_item') {
data.stage = gr.getValue('stage');

if (action == 'cancel') {
// Cancel Request
var req = new GlideRecord('sc_request');
req.get('sys_id',gr.request);
req.setValue('request_state', 'closed_cancelled');
req.setValue('stage', 'closed_incomplete');
req.setValue('approval', 'rejected');
req.update();
}
}
}
})();

 

and HTML Tag as below

<div ng-if="data.stage==waiting_for_approval">
<button type="button" name="reject" class="btn btn-danger btn-lg btn-acceptRejectSolution" ng-click="c.uiAction('cancel')">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
${Cancel Request}</button>
</div>

If I click on cancel request button, 

Request is getting cancelled and button is invisible. If I refresh the page, again button is visible, in all stages.

Please suggest.

2 REPLIES 2

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

I think you problem is that the data.stage isn't set before something happens (since it's within the IF-statement of the code) Move this "data.stage = gr.getValue('stage');" to above the "if (input && input.action) {" and it will be hide it from the start. Then I guess you need to perhaps trim it and move some more stuff.

I would do the check in the server side instead and just have a variable like data.showWidget and use that to hide and show the button.

Take a look here. I made a simple video how to do the stuff you are looking for: 

 

jjones4773
Tera Guru

Great video, but what if you want to show/hide certain widgets based on user profile attributes such as usertype.  Type a has these widgets loaded into a portal, but type b has these other ones.