scolladon / server-action-service

Generic and reusable Lightning service component that calls server-side actions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Salesforce Lightning Server Side Actions Service Component

About

This is a generic and reusable Lightning component that calls server-side actions.
This is a service component so it has no user interface of it's own: it is meant to be called by other components.

Features

The component provides the following features:

  • integrated error notifications (developer console + toast notifications in Lightning Experience)
  • support for storable actions (cached)
  • custom success and error callbacks

Installation

Option 1: deploy the component and the sample application with Salesforce DX by clicking on this button:

Deploy

Option 2: install just the service component without the sample app as an unmanaged package either by

Option 2a: running this Salesforce DX command:

sfdx force:package:install --package 04t1t000000VqwD

Option 2b: clicking this link:
https://login.salesforce.com/packaging/installPackage.apexp?p0=04t1t000000Vrbf

Documentation

The component is documented using Aura documentation.
You can access it from this URL (replace the domain):
https://<YOUR_DOMAIN>.lightning.force.com/auradocs/reference.app#reference?descriptor=c:ServerActionService&defType=component

Here is an overview of how the service integrates with a calling component:

architecture

Use the service by adding the component to a parent component's markup:

<!-- Add dependency to server side action service -->
<c:ServerActionService aura:id="server"/>

Then, simply call a server-side action from the parent's component controller like this:

// Get server action service
const server = component.find('server');
// Get server-side action
const action = component.get('c.anAction');
// Call server-side action with no parameters and no callback
server.callServer(action);

Server-side actions can also be called with optional parameters, optional custom success and error handlers:

server.callServer(
    action, // Server-side action
    parameters, // Action parameters
    false, // Disable cache
    false, // Disable background
    false, // not abortable
    false, // Disable built-in error notification
    $A.getCallback(response => { // Custom success callback
        // Handle response
    }),
    $A.getCallback(errors => { // Custom error callback
        // Handle errors
    })
);

Server-side actions can also be called in Promise chain:

server.callServerPromise(
    action, // Server-side action
    parameters, // Action parameters
    false, // Disable cache
    false, // Disable background
    false, // not abortable
    false, // Disable built-in error notification
).then($A.getCallback(res => {console.log(res)}))
.catch($A.getCallback(err=> {console.error(err)}));

Sample application

The default installation (if you do not use the package) installs the component and a sample application available under this URL (replace the domain):
https://<YOUR_DOMAIN>.lightning.force.com/c/SampleServerActionApp.app

If you wish to install manually the project without the sample app, edit sfdx-project.json and remove the src-sample path.

Sample app screenshot

About

Generic and reusable Lightning service component that calls server-side actions

License:Apache License 2.0


Languages

Language:JavaScript 77.3%Language:Shell 15.5%Language:Apex 6.1%Language:CSS 1.2%