Use Quick Actions, Custom Buttons, or Apex
Learning Objectives
- Create quick actions (instead of using JavaScript) to validate fields, create records with prepopulated values, and redirect to a Visualforce page.
- Describe how to use custom URL buttons to fulfill the same navigation and redirect functionality as JavaScript buttons.
- Use a Visualforce custom button instead of a JavaScript button to work with multiple records in lists.
You Have Buttons? We Have Alternatives
This table maps use cases for JavaScript buttons to alternate—and in most cases, better—solutions in Lightning.
JavaScript Button Top Use Cases | Lightning Alternatives | Declarative/Programmatic |
---|---|---|
Validate fields (presave) | Quick actions (using default values and/or formulas) | D |
Apex triggers | P | |
Create records with prepopulated values | Quick actions (using default values and/or formulas) | D |
Redirect to a record page | Custom URL buttons | D |
Redirect to a Visualforce page | Visualforce quick actions | P |
Lightning actions | P | |
Prefill values based on inputs | Lightning actions | P |
Confirmation pop-up screens | Lightning actions | P |
API calls (Salesforce and third-party) | Lightning actions | P |
Feedback pop-up screens | Lightning actions | P |
Third-party integration | Lightning actions | P |
Mass actions on list view records | Custom Visualforce buttons on list views | P |
As you can see, Salesforce offers several declarative tools for converting the functionality of your JavaScript custom buttons.
Quick Actions
Validate Field Values
Sometimes you want to make sure that certain fields are filled in or populated with specific criteria when your users create or update records.
Let’s say you want to create an action for closing a task without requiring users to go to a full edit page. But you also want to make sure that the task has a due date before it can be closed.
- From the Object Manager in Setup, click Task, then click Buttons, Links, and Actions.
- Click New Action.
- For Action Type, select Update a Record.
- For Label, type in Close Task.
- Click Save.
Now we select the fields that we want to appear on the Close Task quick action. You can easily make a field required or read-only through its field properties.
After you set the fields on the action layout, you can add predefined field values for any of the fields on the task record. In this example, we marked the Due Date field as required. We also added a predefined value for the Status field to be changed to Completed.
Now that we’re done configuring the action, we add it to the Task page layout. Then users can access it from a task record page in both Lightning Experience and the Salesforce mobile app. Here’s an example of a Close Task action on the Tasks page.
Clicking Close Task brings up the action, which the user can quickly act upon and save.
Here’s what it looks like in the Salesforce mobile app.
Prepopulate Fields with Values
A more advanced use case is when you want to let users create a record, but you also want one or more of the fields to be populated automatically based on values in a dependent field.
For example, let’s say your inside sales team’s typical quarterly quota is one fourth of their customers’ revenues from last year, increased by 10%. Because that simple formula doesn’t always apply, you want to prefill the opportunity amount with the value, but also let the user modify it. Create an action so your users can modify the field quickly and efficiently without going to the full opportunity record page.
- From the Object Manager in Setup, click Account, then Buttons, Links, and Actions.
- Click New Action.
- For Action Type, select Create a Record.
- For Target Object, select Opportunity.
- Pick the appropriate Record Type.
- For Label, enter New Oppty.
- Click Save.
Account.Last_Year_Revenue_Generated__c * 1.10 / 4
Add this action to the Account page layout. When users invoke it, they see a value prepopulated in the field that they can accept or override.
Account.Parent.Last_Year_Revenue_Generated__c
Don’t worry about remembering the formula formats; quick actions are declarative and use the Salesforce formula builder.
Redirect to a Visualforce Page Based on Input Values
You can create Visualforce pages to enhance your business processes. Users can navigate to these Visualforce pages in various ways, such as with custom buttons, action overrides, and tabs.
One benefit of Visualforce pages is that by using the standard controller, you can create customized record pages and add prevalidation, prepopulated fields, formulas, and much more.
JavaScript buttons are commonly used in Salesforce Classic to read and pass values from a record into a URL that then redirects users to a Visualforce page. You can also give your users access to Visualforce pages via quick actions. Creating Visualforce quick actions is easy, and a similar process to what we’ve already covered. The only difference is that you select Custom Visualforce as the action type.
For object-specific Visualforce quick actions, you must include the standardController for the object in your Visualforce page to gain access to the record data and have the Visualforce page appear in the quick action picklist.
Custom URL Buttons and Links
Here’s a list of different URL buttons and links, and their redirect behavior in Lightning Experience.
Custom URL Button or Link | Lightning Experience Behavior |
---|---|
External URL
www.google.com |
URL opens in new tab |
Relative Salesforce URL, View
/{!Account.Id} |
Record home page opens in existing tab |
Relative Salesforce URL, Edit
/{!Account.Id}/e |
Edit overlay pops up on the existing page |
Relative Salesforce URL, List
/001/o |
Object home page opens in existing tab |
$Action URL, View
{!URLFOR($Action.Account.View, Account.Id)} |
Record home page opens in existing tab |
$Action URL, Edit
{!URLFOR($Action.Account.Edit, Account.Id)} |
Edit overlay pops up on the existing page |
Apex Triggers
When you need presave validation, calculation, and population of fields, consider using Apex triggers. They’re especially useful for third-party integration, because the rules are enforced through the Salesforce API across Salesforce Classic, Lightning Experience, and the Salesforce mobile app.
For more information on Apex triggers, check out the Apex Developer Guide or earn the Apex Triggers badge.
Custom Visualforce Buttons
- Create your Visualforce page. Here is sample code for editing the Stage and Close dates for multiple opportunities:
<apex:page standardController="Opportunity" recordSetVar="opportunities" extensions="tenPageSizeExt"> <apex:form> <apex:pageBlock title="Edit Stage and Close Date" mode="edit"> <apex:pageMessages /> <apex:pageBlockButtons location="top"> <apex:commandButton value="Save" action="{!save}"/> <apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons> <apex:pageBlockTable value="{!selected}" var="opp"> <apex:column value="{!opp.name}"/> <apex:column headerValue="Stage"> <apex:inputField value="{!opp.stageName}"/> </apex:column> <apex:column headerValue="Close Date"> <apex:inputField value="{!opp.closeDate}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>
- Create a custom button that references your Visualforce page.
- Add the action to your list view.
Mass actions aren’t supported on the Recently Viewed records list. They are only available on list views.
Example
Here’s how that action shows up in Lightning Experience.
As you can see from our examples, Salesforce has a great set of features that allow you to migrate your JavaScript button functionality and move to Lightning Experience.
Now, you may have a lot of JavaScript buttons that have accumulated in your org over the years. You might expect that the migration or conversion process will take a long time. But the job might not be as hard as you think. We did an analysis of JavaScript buttons in the internal Salesforce org that all our employees use. We found that many buttons were obsolete or rarely invoked by users. Others were simply duplicates—the same button but on different objects. After going through the list, we discovered that many of the JavaScript buttons could be converted to the solutions we’ve looked at so far. For the remainder, we could address quite a few with the new Lightning Actions.
You probably noticed Lightning actions in the list of programmatic solutions as a common replacement for many of the JavaScript button use cases. Lightning actions are easy to build, because they’re based on the quick action framework. You set them up similar to Visualforce quick actions. We’ll cover Lightning actions more in depth next.