Use Case

You have a community and you would like to Alert users of an upcoming event.

Requirements

  1. Be able to show an alert message in a community.
  2. The alert should be time-based.  You should be able to schedule when it will start appearing to customers and when it will stop showing.
  3. There are varying levels of alerts.  Some alerts are informational others are critical.
  4. Support multi-language.
  5. There can be more than one alert at a time.
  6. Different pages can show different sets of alerts.

Approach

Create a custom lightning component that can be used within a community.  The component will have a controller that will query a custom object that holds the alerts, the language, the alert level and the date times the alert should be shown.

So let’s get into the coding.

At the bottom of this post, you will find a link to the Git Repository where you will find a DX Project containing the meta-data and source. You can use the project with a DX enabled org, or copy paste to create your own component.

Custom Object

First, create a custom object “Community Alerts” with the following fields:

Label Type Comments
Component Name Text 100 Used in the component to find which set of alerts to display for a specific instance of the Alert Component
Details Text Area 1000 The alert text
Expiration Date Date The date you want the message to show until
Start Date Date The date you want it to start showing
Type Picklist What type of alert.  Values are Default, Success, Error, Warning.  Controls the background color of the alert.
Language Picklist Allows for support of multiple languages.  If a single language is being used then just leave this value blank.  If multilanguage, the language will need to be in the URL so the component can pick it up and pass it to the server side controller.

If you are building this you will want to create a Tab and probably an App so that you can access and create alerts. For more details on how to use Community Alerts visit this post.

Server side controller

The lightning component relies on a very straightforward server side controller so let’s get that out of the way.  Notice that the component name and language are passed from the component to the controller can retrieve the appropriate alerts that a particular instance of the component is asking for.

Lightning Component Bundle

Now onto the Lightning component itself.  There are much better places to learn about how to build lighting components so let’s just give the code here and talk about some highlights.

Component highlights:

The componentName is set in the community where the component is being used.
The alerts attribute is a list so it can show more than one alert.

Controller.  Not much to talk about here.

Helper.  Things to note here.

  1. The language is retrieved from the URL and passed to the server side controller. See the notes included in the object definition.
  2. setLanguage is parsing the url looking for the language parameter.

The design code exposes the component name so the user of the component can set the component name to match the data.

And finally, here is the styling.  This is how the background colors are set up.

Here is a link to the Git Repository where you will find a DX Project containing the meta-data and source. You can use the project with a DX enabled org, or copy paste to create your own component [ConsultingME / Alerts].

Categories: Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *