This repo contains resources and demo code from my theater session at Dreamforce 2022.
It's a Date(time): A Courtship with JavaScript & Salesforce
Working with Dates and Datetimes in Salesforce and JavaScript can get complicated. Learn the edge cases involved in working on the Salesforce Platform in a date- and time-driven global application.
-
Date resources: My collection of helpful resources for working with Dates and Datetimes in Salesforce and JavaScript.
-
-
Installing the app using a Scratch Org: This is the recommended installation option. Use this option if you are a developer who wants to experience the app and the code. This sample application is designed to run on Salesforce Platform.
-
This demo app includes some Lightning web components that allow you to experience working with Dates and Datetimes in Salesforce and JavaScript. Below are the key elements:
-
A custom object called Project that has two date fields (Start Date and End Date)
-
A Lightning web component called projectDates that explores some best practices in working with Dates and Datetimes on the Salesforce Platform:
-
Various wire adapters give you date or date time information in different formats (e.g. getFieldValue returns an ISO String vs. getFieldDisplayValue gives you the value formatted and localized to the user's setting in Salesforce)
-
When using the lightning-formatted-date-time base component, pass in UTC as the time zone for Date fields, and then pass in the user's setting in Salesforce time zone for a Datetime field. If you don't pass in time zone information, it defaults to the user's device time zone
-
-
A Lightning web component called endDateReminder that has two versions:
-
The default version uses some simplistic date math to render a warning message when the project end date is within one month or one day
-
The second version uses Day.js to help with the date math; see instructions here for using a Third-Party JavaScript Library with Lightning web components
-
-
Set up your environment. Follow the steps in the Quick Start: Lightning Web Components Trailhead project. The steps include:
- Enable Dev Hub in your Trailhead Playground
- Install Salesforce CLI
- Install Visual Studio Code
- Install the Visual Studio Code Salesforce extensions, including the Lightning Web Components extension
-
If you haven't already done so, authorize your hub org and provide it with an alias (myhuborg in the command below):
sfdx auth:web:login -d -a myhuborg
-
Clone this repository:
git clone https://github.com/whitelisab/df-22-demo.git cd df-22-demo
-
Create a scratch org and provide it with an alias (dfdemo in the command below):
sfdx force:org:create -s -f config/project-scratch-def.json -a dfdemo
-
Push the app to your scratch org:
sfdx force:source:push
-
Assign the Project Access permission set to the default user:
sfdx force:user:permset:assign -n Project_Access
-
Import sample data:
sfdx force:apex:execute -f ./scripts/apex/create.sample.data.apex
-
Open the scratch org:
sfdx force:org:open
-
In App Launcher, select DF Demo App from the App Launcher.
Use the script noted above to import four sample projects that allow you see the various states of the End Date Reminder LWC. If you want to refresh dates without having to change the end dates manually, run the update script:
sfdx force:apex:execute -f ./scripts/apex/update.sample.data.apex
This repository contains several files that are relevant if you want to integrate modern web development tooling to your Salesforce development processes, or to your continuous integration/continuous deployment processes.
Prettier is a code formatter used to ensure consistent formatting across your code base. To use Prettier with Visual Studio Code, install this extension from the Visual Studio Code Marketplace. The .prettierignore and .prettierrc files are provided as part of this repository to control the behavior of the Prettier formatter.
ESLint is a popular JavaScript linting tool used to identify stylistic errors and erroneous constructs. To use ESLint with Visual Studio Code, install this extension from the Visual Studio Code Marketplace. The .eslintignore file is provided as part of this repository to exclude specific files from the linting process in the context of Lightning Web Components development.
This repository also comes with a package.json file that makes it easy to set up a pre-commit hook that enforces code formatting and linting by running Prettier and ESLint every time you git commit
changes.
To set up the formatting and linting pre-commit hook:
- Install Node.js if you haven't already done so
- Run
npm install
in your project's root folder to install the ESLint and Prettier modules (Note: Mac users should verify that Xcode command line tools are installed before running this command.)
Prettier and ESLint will now run automatically every time you commit changes. The commit will fail if linting errors are detected. You can also run the formatting and linting from the command line using the following commands (check out package.json for the full list):
npm run lint
npm run prettier
Much of the instructions in this README is borrowed from the Trailhead Demo Apps found here.