Service Portal

DiveshTyagi
Mega Guru

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

1 ACCEPTED SOLUTION

Aditya40
Mega Guru

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

  1. The client sets c.data.action = 'approve' and calls c.server.update()
  2. The server script reads input.action and executes the corresponding logic
  3. The server updates the record and returns updated values in data
  4. The UI automatically refreshes to display the new state and message

 

View solution in original post

1 REPLY 1

Aditya40
Mega Guru

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

  1. The client sets c.data.action = 'approve' and calls c.server.update()
  2. The server script reads input.action and executes the corresponding logic
  3. The server updates the record and returns updated values in data
  4. The UI automatically refreshes to display the new state and message