- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I want to perform different server actions like approve, reject, or close from the same widget.
Question: What is the best practice for using input.action in Service Portal widgets
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Best Practice: Using input.action for Multiple Server Actions in Service Portal Widgets
When a Service Portal widget needs to perform multiple server-side actions (such as approve, reject, or close) from the same UI, the recommended best practice is to use input.action to distinguish the requested operation.
This approach keeps the client script simple and centralizes business logic on the server, improving maintainability and scalability.
Example Implementation
Client Script
The client sets the desired action and invokes the server update.
api.controller = function() {
var c = this;
c.approve = function() {
c.data.action = 'approve';
c.server.update();
};
};
HTML Template
The UI triggers the client action and displays server-returned values.
<div>
<h3>Incident Actions</h3>
<div ng-if="c.data.state">
<p>Current State: {{c.data.state}}</p>
<p ng-if="c.data.message">{{c.data.message}}</p>
<button class="btn btn-success" ng-click="c.approve()">
Approve
</button>
</div>
</div>
Server Script
The server script evaluates input.action and performs the appropriate business logic.
(function() {
data.message = '';
data.state = '';
var sysId = input.sys_id || $sp.getParameter('sys_id');
var gr = new GlideRecord('incident');
if (!sysId || !gr.get(sysId)) {
data.error = 'Invalid incident';
return;
}
if (input && input.action === 'approve') {
gr.setValue('approval', 'approved');
gr.update();
data.message = 'Incident approved successfully';
}
data.state = gr.getDisplayValue('state');
})();
How It Works
- The client sets c.data.action = 'approve' and calls c.server.update()
- The server script reads input.action and executes the corresponding logic
- The server updates the record and returns updated values in data
- The UI automatically refreshes to display the new state and message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Best Practice: Using input.action for Multiple Server Actions in Service Portal Widgets
When a Service Portal widget needs to perform multiple server-side actions (such as approve, reject, or close) from the same UI, the recommended best practice is to use input.action to distinguish the requested operation.
This approach keeps the client script simple and centralizes business logic on the server, improving maintainability and scalability.
Example Implementation
Client Script
The client sets the desired action and invokes the server update.
api.controller = function() {
var c = this;
c.approve = function() {
c.data.action = 'approve';
c.server.update();
};
};
HTML Template
The UI triggers the client action and displays server-returned values.
<div>
<h3>Incident Actions</h3>
<div ng-if="c.data.state">
<p>Current State: {{c.data.state}}</p>
<p ng-if="c.data.message">{{c.data.message}}</p>
<button class="btn btn-success" ng-click="c.approve()">
Approve
</button>
</div>
</div>
Server Script
The server script evaluates input.action and performs the appropriate business logic.
(function() {
data.message = '';
data.state = '';
var sysId = input.sys_id || $sp.getParameter('sys_id');
var gr = new GlideRecord('incident');
if (!sysId || !gr.get(sysId)) {
data.error = 'Invalid incident';
return;
}
if (input && input.action === 'approve') {
gr.setValue('approval', 'approved');
gr.update();
data.message = 'Incident approved successfully';
}
data.state = gr.getDisplayValue('state');
})();
How It Works
- The client sets c.data.action = 'approve' and calls c.server.update()
- The server script reads input.action and executes the corresponding logic
- The server updates the record and returns updated values in data
- The UI automatically refreshes to display the new state and message
