You probably already know and love quick actions, but in case you don’t, quick actions are easily accessible custom actions users can do in micro moments throughout the day. Lightning Actions are simply quick actions that call Lightning components.
That’s great news if you’ve already built Lightning Components. Simply add an interface to the component, and zap, you have a Lightning action! If you want an object-specific Lightning action, then just add another interface. Life with Lighting couldn’t be easier!
The rest of this blog post is going to focus in-depth on the development aspects of Lightning Actions. If you haven’t built Lightning components before, you should visit the Lightning Development Center, do the Lightning Component Quick Start Trailhead project, and complete the Lightning Components Basics Trailhead module.
- Populate fields based on user input
- Provide feedback messages to users during data entry
- 3rd Party API integration
Use Cases #1 & 2: Populate Fields Based on User Input & Provide Feedback Messages to Users
A popular use case is to validate or manipulate data when users are working with records. This lets you give your users feedback or instructions for handling more complicated business processes.
We’ll use an example to illustrate how easily you can validate or manipulate data in a Lightning component before your users create or update a record. We’ve built a custom object called Case Studies. Our sample org uses case studies to track different research projects and adds test users to those records.
When we add new test users, we capture the users’ names and email addresses, which we want to make sure are entered correctly, since that’s our primary contact method. We also want to create a unique nickname so that during the research, test users can remain anonymous.
So we’re going to create a Lightning action that has the name and email address fields, validates the email address, and uses the first name and a random number to automatically create the nickname.
Here’s the code for the component used by this Lightning action.
CreateUser.cmp is the lightning component that you see when you open the action – it contains the UI implementation, which includes the text fields, buttons, action title, etc.
CreateUserController.js is the controller that listens to the component and any UI events that take place, such as the initial load, text input, and button clicks. Its function is to delegate these events to the helper class, CreateUserHelper.js.
CreateUserHelper.js deals with all the business logic, such as validating the password and email fields, and communicating with the server-side Apex controller, which saves the data.
SaveTestUser.apxc is a very simple Apex controller that handles the request to create a new test user.
After creating the Lightning component, we assign it to an action. In the object management settings for Case Study, we go to Buttons, Links, and Actions, click New Action, then configure the action like this:
Then we add our new Lightning action to the Case Study page layout. When users invoke it, they see the Lightning action we created.
The great thing about this is that it also works in Salesforce1:
Use Case #3: 3rd-Party API Integration
Let’s say that we’re in the luxury travel business, and we handle celebrities and VIPs. We want our customer service agents to be able to communicate with their clients, but we don’t want to expose the clients’ personal contact information to anyone. We created a Lightning action on the Contact object so that agents can send messages without accessing the contact’s phone number.
To illustrate this, we’ll show you an example of an integration with Twilio for sending SMS messages. As before, we’ll create the Lightning component and helper classes, and then create a quick action to invoke the component.
As with the prior example, after creating the component, we created a new Lightning action, and this time added it to the Contact page layout so agents can access it.
Again, the cool thing is that the action is also available in Salesforce1, so if a car service provider is trying to contact the customer at the airport, the driver can reach them easily.
If you’d like to try Twilio for yourself, you can find an unmanaged package of the Twilio for Salesforce library class used in the 3rd Party integration example in the Salesforce Github library: https://github.com/twilio/twilio-salesforce
Before using Twilio for Salesforce and the Twilio API, you need to create a Twilio account, set up a phone number, and connect your account to Salesforce; see the TwilioApi.cls in the above library for more detailed instructions.