Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JavaScript Buttons, Lightning, and You (Part 1)

DZone's Guide to

JavaScript Buttons, Lightning, and You (Part 1)

Salesforce is moving away from the untrusted JavaScript. See how you can keep that functionality when migrating to Lightning while developing more securely.

· Web Dev Zone
Free Resource

Never build auth again! Okta makes it simple to implement authentication, authorization, MFA and more in minutes. Try the free developer API today! 

JavaScript

We know you love JavaScript buttons and have been using them in Salesforce Classic for many years. As a matter of fact, we’ve heard that many customers are reluctant to migrate to Lightning Experience because of the missing JavaScript button functionality. But Lightning Experience offers so much more than our Salesforce Classic, and it is the future. We realize that you expect and rely on us to always migrate existing functionality to new features and UI, but in the case of JavaScript buttons, we believe that the future is brighter in Lightning Experience, even without JavaScript button support.

JavaScript buttons and links are types of actions in the Salesforce Classic UI that allow you to create inline JavaScript code that can be invoked via a button or link embedded on a record or list page. For example, we have customers who pre-populate new records with data upon creation, update values in fields based on other logic, and partners who use custom buttons to integrate with their platforms.

If JavaScript buttons are so useful, why not support them in Lightning Experience? The issue is that combining untrusted JavaScript from multiple sources and authors together with the application source code, while maintaining trust of the overall application, has significant security challenges.

In this blog series, we’ll cover those security and functional challenges, and share with you the alternatives to JavaScript buttons that are mobile and Lightning-friendly. We’ll cover existing and upcoming features in Salesforce that you can use to migrate the functionality that you’ve built using custom buttons.

We’re committed to solving the problem of client-side customization and integration. We intend to show you a new approach to thinking about JavaScript button functionality in Lightning Experience that is better for everyone.

Overview of the Series

You’re reading Part One, which introduces the blog series, covers the reasons for the decision, highlights the common uses cases for JavaScript buttons, and offers insight into why the alternatives can be better.

Part Two: Take the First Steps: Ways You Can Replace JS Buttons (coming soon)
Highlighting the most common use cases for JavaScript buttons, this article covers the available features in Salesforce that can be used to address that functionality in Salesforce1 and Lightning Experience. Utilizing Quick Actions and Apex, customers and partners have already started migrating to Lightning Experience.

Part Three: Your New Life With Lightning: Plan for the Future with the Actions Roadmap (coming soon)
In the final article of our blog series, we’ll discuss new features coming to Salesforce1 and Lightning Experience — such as Lightning Actions — that will provide you with an easier migration path.

Why It’s Time to Break Up with JavaScript Buttons

One of the coolest benefits in Lightning Experience is that customers and partners can add their Lightning components on the record, home, and other pages in the Salesforce application. For example, a customer might choose to add a map component to their Account record pages, or a partner might provide a component for their AppExchange app that can be added to the Home page or an Opportunity record.

The use cases are quite vast, which is why customers and partners are very excited about Lightning Experience. However, without some safeguards, the components would have access to each other’s data, shared access to the window and event structures, and access to any client side API. This could allow a partner’s component for HIPAA compliance or financial information to be accessed by another component from a different source, when they are both on the same page. I think we can agree that this is problematic, and could lead to many security and regulatory issues.

What’s Up With In-line JavaScript

JavaScriptBefore we discuss the safeguards that Salesforce has put in place for Lightning component security, let’s highlight some of the issues with in-line JavaScript. JavaScript is a loosely-typed programming language, supported by all modern web browsers without a plugin. It can persist data and state through cookies and storage APIs, and it can access events, URLs, and cookies through the browser. What makes JavaScript both useful and dangerous is that it has full access to the Document Object Model (DOM) and Browser Object Model (BOM).

With access to the DOM, a programmer can add, change, or delete almost anything found in an HTML or XML document. This is extremely useful in the right hands because JavaScript provides an API for working with text, dates, and regular expressions, so it is easy to add client-side functionality with JavaScript snippets that enhance the base user interface. However, this is also a big vulnerability because with Cross Site Scripting (XSS), malicious actors can gain access to the DOM or BOM via JavaScript and wreak havoc.

When a website allows dynamic content, hackers can use XSS to inject their malicious client-side code into the web pages that are viewed by normal users. The hackers can then leverage the session and cookies from normal users to run scripts to extract data, log keystrokes, manipulate form entries, and even access APIs.

LockerService: Making Lightning Components More Secure

The good news is that Salesforce is already working on a solution to make Lightning components more secure, and restrict JavaScript’s unfettered access. The solution is LockerService, which uses various technologies and techniques that are intended to:

Prevent:

  • XSS and similar security issues
  • Unrestricted DOM access
  • Calling undocumented/private APIs

Enable:

  • Cool new features like client-side API versioning
  • Faster security review (AppExchange)
  • Better JS development practices
  • Easily updating security features and policies

So now that we know Lightning components are built to be more secure, how will you benefit from using them, and how can you recreate your JavaScript button functionality within Lightning Experience? Let’s look at how customers and partners use JavaScript buttons in Salesforce Classic.

What People Are Doing With JavaScript Buttons

We heard from many customers, some with hundreds of JavaScript buttons in their orgs; and we talked to partners about their JavaScript button use cases as well. We collated their various custom use cases into a broader set of operations. Here are the most common use cases for JavaScript buttons:

  • Use or manipulate values on a record BEFORE the save
    • Validate fields — ensure values are populated and/or meet criteria
    • Prefill values based on inputs in other fields
    • Redirect to a Visualforce page based on input values
    • Confirmation pop-up screens
  • Create records w/ pre-populated values
  • Trigger flows from Visual Workflow
  • Callouts to Salesforce or external API
  • 3rd party integration
  • Mass actions on records in a list
  • Feedback pop-up screens for users, directing methods and procedures

There are more scenarios, and some use cases that are so specific to an org that they’re impossible to categorize. In this blog series, we’ll cover existing and upcoming roadmap items that you can use to migrate JavaScript button functionality to Salesforce1 and Lightning Experience. We hope that as part of doing that, you’ll see the benefits of the alternative solutions, and ways to adapt them to your specific needs.

In our next post, we’ll cover existing solutions you can use to address some of the use cases we covered above. Specifically, we’ll look at utilizing quick actions and Apex, and examine how customers and partners have already started migrating to Lightning Experience. In addition, we’ll give you a peek into some new technology that’s going to make Javascript buttons look like day-old bread. Stay tuned!

Launch your application faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
lightning ,components ,buttons ,security ,ui ,series ,javascript ,salesforce

Published at DZone with permission of Kamyar Seradjfar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}