SlideShare a Scribd company logo
JavaScript-heavy
Salesforce Applications
David Meyer
Technical Architect, Appirio
dmeyer@appirio.com
Andrew Davis
Consultant, Appirio
adavis@appirio.com
The role of JavaScript in Salesforce
JS is the ‘last mile’ between Salesforce and users (it already powers much
of the Salesforce UI)
The relevance of JS continues to grow (Angular, node.js, mongoDB, etc.)
Salesforce continues to expand the options for using JavaScript (Lightning
Components, Lightning Experience)
Justifications: When to go JavaScript-heavy?
If you need a highly-customized and responsive UI
If you need/want to use JS Frameworks (Angular, Backbone, etc.)
To achieve tight integration with other JS tools or web services
To bypass Salesforce governor limits
To make more complex asynchronous calls than is possible with Apex
Sample uses of JavaScript in the AOL Support Console
​  Account info derived
from back-end systems
​  Dynamically-sourced
customer offers
​ Displaying information from backend systems using browser-based REST APIs
​  Entirely custom
JavaScript applications
can be included in SFDC
​  … that draw data from
external systems
​  … and allow purchases
and other actions to be
performed on those
external systems
Sample uses of JavaScript in the AOL Support Console
​ Embedding entirely custom apps inside of Salesforce
Salesforce-JavaScript Interaction
Behind-the-scenes: Visualforce compiles down to HTML and JavaScript
Tools you can use:
• Remote Objects (JS data structures that mirror the SFDC database)
• Remote Actions (Defined in Apex, invoked from JS using JS callbacks)
• Action Methods (Action Functions, Action Support, Action Pollers)
• REST web services (Salesforce provides many and lets you write your own too)
• Lightning Components (learn more elsewhere)
• Tip: exchange complex objects between Apex and JS using JSON.de/serialize()
• Tip: overcome cross-org limitations by using JSONP or CORS-enabled services
Salesforce-JavaScript Interaction
How to pass control and data back and forth
JavaScript Concepts
Some core ideas to help Salesforce developers
JavaScript Concepts
Most developers know some JavaScript
It’s worth getting to know it well!
What we’ll touch on:
• Object-oriented JavaScript (properties, methods, protoypes)
• Asynchronous JavaScript: (callbacks and promises)
• Variable scope in JavaScript (scope, closures, namespacing)
Selecting Elements in Visualforce Pages
The Id that Visualforce gave them when it renders your pageThe Id you gave to your elements
Try this method instead (this also works in CSS)These methods won’t work to select this element
Minimize use of global variables
Having all of your variables in the global scope is poor practice, makes debugging harder, and there are
950 global variables used in Salesforce that your variable names might conflict withIdeally, create a single global “namespace” object and make your code into its methods and properties
Group Related Variables into Objects
JSON.parse and JSON.stringify provide an elegant way to pass complex data
Understanding Variable Scope and Closures
Promises and the jQuery Deferred Object
Deferred objects allow for concise and clear expression of how asynchronous requests should be handled
Debugging JavaScript using Chrome DevTools
Getting to know the capabilities of Chrome DevTools or Firebug is essential
Capabilities of DevTools:
• Inspect and modify HTML, CSS, JS, browser data
• Monitor network traffic and page rendering timeline
• Set breakpoints and step through JS execution
• Emulate devices and network speeds
• Profile and Audit your JavaScript and page loads
DevTools can give you complete visibility into every aspect of your JS
Unit Testing JavaScript on Salesforce
Setting up a unit testing framework and preparing your code
Unit testing JavaScript on Salesforce
Why unit test your JavaScript?
Setting up a unit testing stack: Node-Karma-Mocha-Chai (locally or in CI)
Preparing your code:
• Try to move all of your JS into static resources (this can also speed page load)
• But {!controllerBindings} need to stay on the Visualforce page
• Make sure your code is testable:
• Testable methods need to be globally-accessible
• Emphasize discrete, independent functions that don’t depend on global values
Write unit tests using Mocha and Chai syntax with Sinon for stubs/mocks
Sample Unit Testing Stack
For installing
dependenciesPlatform
Test Runner
(Creates a web server)
Assertion Syntax
assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');
assert.lengthOf(foo, 3);
Mocks & Stubs
var callback = sinon.stub();
callback.withArgs(42).returns(1);
callback.withArgs(1).throws("TypeError");
Testing & Reporting
(The core test framework)
describe('testFn Suite', function () {
it('should return true', function () {
assert.equal(true, testFn() );
});
});
Unit testing stack setup (on your machine)
1.  Install Node.js on your development machine(s).
2.  Grab our code at goo.gl/fMFrm1 (github.com/abd3/DF15-JS-Heavy-Salesforce)
3.  Copy these files from our sample project into yours and customize as needed:
a.  /package.json - Node.js will use this to get all the packages you need
b.  /karma.conf.js - The configuration file for Karma (which files to test, etc.)
c.  /test/* - The actual unit tests used in our sample project
4.  In the root of your development directory (where package.json is installed) run
the following one-time installation command from the command line:
npm install
5.  Run the following code from the command-line every time you want to start the
karma test runner:
./node_modules/karma/bin/karma start karma.conf.js
6.  autowatch = true in our sample configuration file, so Karma will re-run all the unit
tests each time you save a change to one of the files being tested.
Unit Test FilesSalesforce Files
Anatomy of a JavaScript Unit Test
Salesforce Files
Unit Test Files
Similar Unit Tests can be Grouped
Salesforce Files
Setup & Tear-down steps
Actual Unit Tests
Setup & Tear-down steps
Setup & Tear-down steps
Anatomy of a JavaScript Unit Test
Actual Unit Tests
Anatomy of a JavaScript Unit Test
Karma’s unit testing interface
Command-line or web-based
Browser-based test runner
The command-line test runner auto-runs when files change
Karma’s code coverage report (karma-junit-reporter style)
This shows aggregate and line-by-line unit test coverage on the project
Aggregate code-coverage for the project
Line-by-line code coverage
Unit testing stack (for Continuous Integration)
​ General guidelines for including JS unit testing in your CI builds:
•  Set up a separate build step for the JS unit testing
•  Install the nodejs plugin for Jenkins and specify your version of node & npm
•  The build step consists simply of running this shell command:
npm install
node_modules/karma/bin/karma start karma.conf.js --single-run
•  The build step will succeed or fail based on the results of that command
•  This will also output some reports in the /reports folder that can be ingested to
report on code coverage, code quality, etc. if you’re using code quality tools
​  Build step for JS code
quality analysis in
SonarQube
JavaScript Unit Testing as a Continuous Deployment Step
​ JS unit testing can be one step in a CI/CD setup
​  Build step to run
JavaScript unit tests
Jenkins build step to
deploy to a QA org. This
also runs Apex unit tests
Karma outputs, displayed in SonarQube
Karma’s unit testing output can be ingested and displayed in other products
Questions & Answers
Grab the code at goo.gl/fMFrm1
(https://github.com/abd3/DF15-JS-Heavy-Salesforce)
Share Your Feedback, and Win a GoPro!
3
Earn a GoPro prize entry for
each completed survey
Tap the bell to take a
survey2Enroll in a session1
Thank you
Salesforce - JavaScript Integration Methods
Note that you can mix and match any combination of these as needed
Action Methods
JavaScript
Remoting
Remote Objects
Salesforce
REST API
Require
Controller Code
yes yes no no
Require
Visualforce
yes yes yes no
Have their own
Governor Limits
no no no yes
Main Benefit
Call static or
instance methods
while maintaining
controller state
Call static Apex
methods quickly and
efficiently
Create a JS version
of Salesforce object
with CRUD access
Perform Salesforce
actions and modify
data from external
apps
Main
Disadvantage
Submits form data;
slower
Limited to static
methods
Can’t execute
custom Apex from
VF this way

More Related Content

PDF
jsForce in Action
Salesforce Developers
 
PDF
Introduction to Javascript Unit Testing With xUnit.js
Salesforce Developers
 
PPTX
JavaScript Unit Testing
Keir Bowden
 
PDF
JavaScript Patterns and Practices from the Salesforce Experts
Salesforce Developers
 
PDF
NgForce: A JS Library For Quickly Building Salesforce Apps Using AngularJS
Salesforce Developers
 
PDF
Salesforce API Series: Release Management with the Metadata API webinar
Salesforce Developers
 
PPTX
Intro to Apex - Salesforce Force Friday Webinar
Abhinav Gupta
 
PPTX
AngularJS App In Two Weeks
Peter Chittum
 
jsForce in Action
Salesforce Developers
 
Introduction to Javascript Unit Testing With xUnit.js
Salesforce Developers
 
JavaScript Unit Testing
Keir Bowden
 
JavaScript Patterns and Practices from the Salesforce Experts
Salesforce Developers
 
NgForce: A JS Library For Quickly Building Salesforce Apps Using AngularJS
Salesforce Developers
 
Salesforce API Series: Release Management with the Metadata API webinar
Salesforce Developers
 
Intro to Apex - Salesforce Force Friday Webinar
Abhinav Gupta
 
AngularJS App In Two Weeks
Peter Chittum
 

What's hot (20)

PDF
Salesforce.com API Series: Service Cloud Console Deep Dive
Salesforce Developers
 
PPTX
Apex for Admins: Beyond the Basics
Salesforce Developers
 
PPT
Restful services with ColdFusion
ColdFusionConference
 
PDF
Generically Call External Classes from Managed Packages
Salesforce Developers
 
PPTX
Apex for Admins: Get Started with Apex in 30 Minutes! (part 1)
Salesforce Developers
 
DOC
Gowtham_resume
Gowtham Venkateshan
 
PPTX
Salesforce Apex Hours:- Salesforce DX
Amit Chaudhary
 
PDF
Intro to Apex Programmers
Salesforce Developers
 
PDF
AppExchange Tech Enablement June 2017
Salesforce Partners
 
PDF
Continuous integration testing for automation needs and quality of the releases
Zado Technologies
 
PDF
Summer '13 Developer Preview Webinar
Salesforce Developers
 
PPTX
10 Principles of Apex Testing
Salesforce Developers
 
PPTX
Web based automation testing on Node.js environment
Yu-Lin Huang
 
PPTX
Seamless Authentication with Force.com Canvas
Salesforce Developers
 
PDF
Advanced Flow Techniques with Apex and Visualforce
Salesforce Developers
 
PDF
Build Together And Deliver Continuously With Salesforce DX
Lynette Lim
 
PDF
Building Dynamic UI with Visual Workflow Runtime API
Salesforce Developers
 
PPT
How Force.com developers do more in less time
Abhinav Gupta
 
PPTX
Get Started with Salesforce DX!
Salesforce Developers
 
PDF
Lightning Components Explained
Atul Gupta(8X)
 
Salesforce.com API Series: Service Cloud Console Deep Dive
Salesforce Developers
 
Apex for Admins: Beyond the Basics
Salesforce Developers
 
Restful services with ColdFusion
ColdFusionConference
 
Generically Call External Classes from Managed Packages
Salesforce Developers
 
Apex for Admins: Get Started with Apex in 30 Minutes! (part 1)
Salesforce Developers
 
Gowtham_resume
Gowtham Venkateshan
 
Salesforce Apex Hours:- Salesforce DX
Amit Chaudhary
 
Intro to Apex Programmers
Salesforce Developers
 
AppExchange Tech Enablement June 2017
Salesforce Partners
 
Continuous integration testing for automation needs and quality of the releases
Zado Technologies
 
Summer '13 Developer Preview Webinar
Salesforce Developers
 
10 Principles of Apex Testing
Salesforce Developers
 
Web based automation testing on Node.js environment
Yu-Lin Huang
 
Seamless Authentication with Force.com Canvas
Salesforce Developers
 
Advanced Flow Techniques with Apex and Visualforce
Salesforce Developers
 
Build Together And Deliver Continuously With Salesforce DX
Lynette Lim
 
Building Dynamic UI with Visual Workflow Runtime API
Salesforce Developers
 
How Force.com developers do more in less time
Abhinav Gupta
 
Get Started with Salesforce DX!
Salesforce Developers
 
Lightning Components Explained
Atul Gupta(8X)
 
Ad

Similar to Javascript-heavy Salesforce Applications (20)

PPT
Pragmatic Parallels: Java and JavaScript
davejohnson
 
PPT
Netserv Software Testing
sthicks14
 
PDF
Introduction to AngularJS
Yoann Gotthilf
 
PPTX
Whats New In 2010 (Msdn & Visual Studio)
Steve Lange
 
DOCX
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
curwenmichaela
 
PDF
RAHUL_Updated( (2)
Rahul Singh
 
DOCX
RubaDevi_Salesforce
Ruba Devi Thirugnanam
 
ODP
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
PDF
iks auf der ElipseCon 2011: Tickling the shoulders of giants
IKS Gesellschaft für Informations- und Kommunikationssysteme mbH
 
PDF
Quest to the best test automation for low code development platform kherrazi ...
Rachid Kherrazi
 
PDF
Session on Selenium Powertools by Unmesh Gundecha
Agile Testing Alliance
 
PPTX
Coldbox developer training – session 4
Billie Berzinskas
 
PPT
Spring - a framework written by developers
MarcioSoaresPereira1
 
PDF
Microservice Automated Testing on Kubernetes
Shane Galvin
 
PPTX
Azure DevOps for JavaScript Developers
Sarah Dutkiewicz
 
DOCX
Bharath Kesana Selinium Automation Tester
Bharath Kesana
 
ODP
Presentation - Course about JavaFX
Tom Mix Petreca
 
PPT
Behavior Driven Development by Example
Nalin Goonawardana
 
PDF
Ionic framework one day training
Troy Miles
 
PDF
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
 
Pragmatic Parallels: Java and JavaScript
davejohnson
 
Netserv Software Testing
sthicks14
 
Introduction to AngularJS
Yoann Gotthilf
 
Whats New In 2010 (Msdn & Visual Studio)
Steve Lange
 
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
curwenmichaela
 
RAHUL_Updated( (2)
Rahul Singh
 
RubaDevi_Salesforce
Ruba Devi Thirugnanam
 
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
iks auf der ElipseCon 2011: Tickling the shoulders of giants
IKS Gesellschaft für Informations- und Kommunikationssysteme mbH
 
Quest to the best test automation for low code development platform kherrazi ...
Rachid Kherrazi
 
Session on Selenium Powertools by Unmesh Gundecha
Agile Testing Alliance
 
Coldbox developer training – session 4
Billie Berzinskas
 
Spring - a framework written by developers
MarcioSoaresPereira1
 
Microservice Automated Testing on Kubernetes
Shane Galvin
 
Azure DevOps for JavaScript Developers
Sarah Dutkiewicz
 
Bharath Kesana Selinium Automation Tester
Bharath Kesana
 
Presentation - Course about JavaFX
Tom Mix Petreca
 
Behavior Driven Development by Example
Nalin Goonawardana
 
Ionic framework one day training
Troy Miles
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
 
Ad

More from Salesforce Developers (20)

PDF
Sample Gallery: Reference Code and Best Practices for Salesforce Developers
Salesforce Developers
 
PDF
Maximizing Salesforce Lightning Experience and Lightning Component Performance
Salesforce Developers
 
PDF
Local development with Open Source Base Components
Salesforce Developers
 
PPTX
TrailheaDX India : Developer Highlights
Salesforce Developers
 
PDF
Why developers shouldn’t miss TrailheaDX India
Salesforce Developers
 
PPTX
CodeLive: Build Lightning Web Components faster with Local Development
Salesforce Developers
 
PPTX
CodeLive: Converting Aura Components to Lightning Web Components
Salesforce Developers
 
PPTX
Enterprise-grade UI with open source Lightning Web Components
Salesforce Developers
 
PPTX
TrailheaDX and Summer '19: Developer Highlights
Salesforce Developers
 
PDF
Live coding with LWC
Salesforce Developers
 
PDF
Lightning web components - Episode 4 : Security and Testing
Salesforce Developers
 
PDF
LWC Episode 3- Component Communication and Aura Interoperability
Salesforce Developers
 
PDF
Lightning web components episode 2- work with salesforce data
Salesforce Developers
 
PDF
Lightning web components - Episode 1 - An Introduction
Salesforce Developers
 
PDF
Migrating CPQ to Advanced Calculator and JSQCP
Salesforce Developers
 
PDF
Scale with Large Data Volumes and Big Objects in Salesforce
Salesforce Developers
 
PDF
Replicate Salesforce Data in Real Time with Change Data Capture
Salesforce Developers
 
PDF
Modern Development with Salesforce DX
Salesforce Developers
 
PDF
Get Into Lightning Flow Development
Salesforce Developers
 
PDF
Integrate CMS Content Into Lightning Communities with CMS Connect
Salesforce Developers
 
Sample Gallery: Reference Code and Best Practices for Salesforce Developers
Salesforce Developers
 
Maximizing Salesforce Lightning Experience and Lightning Component Performance
Salesforce Developers
 
Local development with Open Source Base Components
Salesforce Developers
 
TrailheaDX India : Developer Highlights
Salesforce Developers
 
Why developers shouldn’t miss TrailheaDX India
Salesforce Developers
 
CodeLive: Build Lightning Web Components faster with Local Development
Salesforce Developers
 
CodeLive: Converting Aura Components to Lightning Web Components
Salesforce Developers
 
Enterprise-grade UI with open source Lightning Web Components
Salesforce Developers
 
TrailheaDX and Summer '19: Developer Highlights
Salesforce Developers
 
Live coding with LWC
Salesforce Developers
 
Lightning web components - Episode 4 : Security and Testing
Salesforce Developers
 
LWC Episode 3- Component Communication and Aura Interoperability
Salesforce Developers
 
Lightning web components episode 2- work with salesforce data
Salesforce Developers
 
Lightning web components - Episode 1 - An Introduction
Salesforce Developers
 
Migrating CPQ to Advanced Calculator and JSQCP
Salesforce Developers
 
Scale with Large Data Volumes and Big Objects in Salesforce
Salesforce Developers
 
Replicate Salesforce Data in Real Time with Change Data Capture
Salesforce Developers
 
Modern Development with Salesforce DX
Salesforce Developers
 
Get Into Lightning Flow Development
Salesforce Developers
 
Integrate CMS Content Into Lightning Communities with CMS Connect
Salesforce Developers
 

Recently uploaded (20)

PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
PDF
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
DOCX
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Doc9.....................................
SofiaCollazos
 
This slide provides an overview Technology
mineshkharadi333
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
Software Development Company | KodekX
KodekX
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 

Javascript-heavy Salesforce Applications

  • 2. The role of JavaScript in Salesforce JS is the ‘last mile’ between Salesforce and users (it already powers much of the Salesforce UI) The relevance of JS continues to grow (Angular, node.js, mongoDB, etc.) Salesforce continues to expand the options for using JavaScript (Lightning Components, Lightning Experience)
  • 3. Justifications: When to go JavaScript-heavy? If you need a highly-customized and responsive UI If you need/want to use JS Frameworks (Angular, Backbone, etc.) To achieve tight integration with other JS tools or web services To bypass Salesforce governor limits To make more complex asynchronous calls than is possible with Apex
  • 4. Sample uses of JavaScript in the AOL Support Console ​  Account info derived from back-end systems ​  Dynamically-sourced customer offers ​ Displaying information from backend systems using browser-based REST APIs
  • 5. ​  Entirely custom JavaScript applications can be included in SFDC ​  … that draw data from external systems ​  … and allow purchases and other actions to be performed on those external systems Sample uses of JavaScript in the AOL Support Console ​ Embedding entirely custom apps inside of Salesforce
  • 6. Salesforce-JavaScript Interaction Behind-the-scenes: Visualforce compiles down to HTML and JavaScript Tools you can use: • Remote Objects (JS data structures that mirror the SFDC database) • Remote Actions (Defined in Apex, invoked from JS using JS callbacks) • Action Methods (Action Functions, Action Support, Action Pollers) • REST web services (Salesforce provides many and lets you write your own too) • Lightning Components (learn more elsewhere) • Tip: exchange complex objects between Apex and JS using JSON.de/serialize() • Tip: overcome cross-org limitations by using JSONP or CORS-enabled services
  • 7. Salesforce-JavaScript Interaction How to pass control and data back and forth
  • 8. JavaScript Concepts Some core ideas to help Salesforce developers
  • 9. JavaScript Concepts Most developers know some JavaScript It’s worth getting to know it well! What we’ll touch on: • Object-oriented JavaScript (properties, methods, protoypes) • Asynchronous JavaScript: (callbacks and promises) • Variable scope in JavaScript (scope, closures, namespacing)
  • 10. Selecting Elements in Visualforce Pages The Id that Visualforce gave them when it renders your pageThe Id you gave to your elements Try this method instead (this also works in CSS)These methods won’t work to select this element
  • 11. Minimize use of global variables Having all of your variables in the global scope is poor practice, makes debugging harder, and there are 950 global variables used in Salesforce that your variable names might conflict withIdeally, create a single global “namespace” object and make your code into its methods and properties
  • 12. Group Related Variables into Objects JSON.parse and JSON.stringify provide an elegant way to pass complex data
  • 14. Promises and the jQuery Deferred Object Deferred objects allow for concise and clear expression of how asynchronous requests should be handled
  • 15. Debugging JavaScript using Chrome DevTools Getting to know the capabilities of Chrome DevTools or Firebug is essential Capabilities of DevTools: • Inspect and modify HTML, CSS, JS, browser data • Monitor network traffic and page rendering timeline • Set breakpoints and step through JS execution • Emulate devices and network speeds • Profile and Audit your JavaScript and page loads DevTools can give you complete visibility into every aspect of your JS
  • 16. Unit Testing JavaScript on Salesforce Setting up a unit testing framework and preparing your code
  • 17. Unit testing JavaScript on Salesforce Why unit test your JavaScript? Setting up a unit testing stack: Node-Karma-Mocha-Chai (locally or in CI) Preparing your code: • Try to move all of your JS into static resources (this can also speed page load) • But {!controllerBindings} need to stay on the Visualforce page • Make sure your code is testable: • Testable methods need to be globally-accessible • Emphasize discrete, independent functions that don’t depend on global values Write unit tests using Mocha and Chai syntax with Sinon for stubs/mocks
  • 18. Sample Unit Testing Stack For installing dependenciesPlatform Test Runner (Creates a web server) Assertion Syntax assert.typeOf(foo, 'string'); assert.equal(foo, 'bar'); assert.lengthOf(foo, 3); Mocks & Stubs var callback = sinon.stub(); callback.withArgs(42).returns(1); callback.withArgs(1).throws("TypeError"); Testing & Reporting (The core test framework) describe('testFn Suite', function () { it('should return true', function () { assert.equal(true, testFn() ); }); });
  • 19. Unit testing stack setup (on your machine) 1.  Install Node.js on your development machine(s). 2.  Grab our code at goo.gl/fMFrm1 (github.com/abd3/DF15-JS-Heavy-Salesforce) 3.  Copy these files from our sample project into yours and customize as needed: a.  /package.json - Node.js will use this to get all the packages you need b.  /karma.conf.js - The configuration file for Karma (which files to test, etc.) c.  /test/* - The actual unit tests used in our sample project 4.  In the root of your development directory (where package.json is installed) run the following one-time installation command from the command line: npm install 5.  Run the following code from the command-line every time you want to start the karma test runner: ./node_modules/karma/bin/karma start karma.conf.js 6.  autowatch = true in our sample configuration file, so Karma will re-run all the unit tests each time you save a change to one of the files being tested.
  • 20. Unit Test FilesSalesforce Files Anatomy of a JavaScript Unit Test Salesforce Files Unit Test Files Similar Unit Tests can be Grouped Salesforce Files Setup & Tear-down steps Actual Unit Tests
  • 21. Setup & Tear-down steps Setup & Tear-down steps Anatomy of a JavaScript Unit Test
  • 22. Actual Unit Tests Anatomy of a JavaScript Unit Test
  • 23. Karma’s unit testing interface Command-line or web-based Browser-based test runner The command-line test runner auto-runs when files change
  • 24. Karma’s code coverage report (karma-junit-reporter style) This shows aggregate and line-by-line unit test coverage on the project Aggregate code-coverage for the project Line-by-line code coverage
  • 25. Unit testing stack (for Continuous Integration) ​ General guidelines for including JS unit testing in your CI builds: •  Set up a separate build step for the JS unit testing •  Install the nodejs plugin for Jenkins and specify your version of node & npm •  The build step consists simply of running this shell command: npm install node_modules/karma/bin/karma start karma.conf.js --single-run •  The build step will succeed or fail based on the results of that command •  This will also output some reports in the /reports folder that can be ingested to report on code coverage, code quality, etc. if you’re using code quality tools
  • 26. ​  Build step for JS code quality analysis in SonarQube JavaScript Unit Testing as a Continuous Deployment Step ​ JS unit testing can be one step in a CI/CD setup ​  Build step to run JavaScript unit tests Jenkins build step to deploy to a QA org. This also runs Apex unit tests
  • 27. Karma outputs, displayed in SonarQube Karma’s unit testing output can be ingested and displayed in other products
  • 28. Questions & Answers Grab the code at goo.gl/fMFrm1 (https://github.com/abd3/DF15-JS-Heavy-Salesforce)
  • 29. Share Your Feedback, and Win a GoPro! 3 Earn a GoPro prize entry for each completed survey Tap the bell to take a survey2Enroll in a session1
  • 31. Salesforce - JavaScript Integration Methods Note that you can mix and match any combination of these as needed Action Methods JavaScript Remoting Remote Objects Salesforce REST API Require Controller Code yes yes no no Require Visualforce yes yes yes no Have their own Governor Limits no no no yes Main Benefit Call static or instance methods while maintaining controller state Call static Apex methods quickly and efficiently Create a JS version of Salesforce object with CRUD access Perform Salesforce actions and modify data from external apps Main Disadvantage Submits form data; slower Limited to static methods Can’t execute custom Apex from VF this way