Logout succeed
Logout succeed. See you again!

Beginning Salesforce DX: Versatile and Resilient Salesforce Application Development PDF
Preview Beginning Salesforce DX: Versatile and Resilient Salesforce Application Development
Beginning Salesforce DX Versatile and Resilient Salesforce Application Development Ivan Harris Beginning Salesforce DX: Versatile and Resilient Salesforce Application Development Ivan Harris FROME, UK ISBN-13 (pbk): 978-1-4842-8113-0 ISBN-13 (electronic): 978-1-4842-8114-7 https://doi.org/10.1007/978-1-4842-8114-7 Copyright © 2022 by Ivan Harris This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Susan McDermott Development Editor: Laura Berendson Coordinating Editor: Jessica Vakili Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights, please e-mail [email protected]. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on the Github repository: https://github.com/Apress/Beginning-Salesforce-DX. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper To my wife Rosie, and my son Hugo. Table of Contents About the Author ���������������������������������������������������������������������������������������������������xiii About the Technical Reviewer ���������������������������������������������������������������������������������xv Foreword ���������������������������������������������������������������������������������������������������������������xvii Chapter 1: Introduction���������������������������������������������������������������������������������������������1 Chapter Summary �������������������������������������������������������������������������������������������������������������������������4 Part I: Preparing for Salesforce DX �����������������������������������������������������������������5 Chapter 2: Environment Setup ����������������������������������������������������������������������������������7 Installing the Salesforce CLI ���������������������������������������������������������������������������������������������������������8 Installing the Java Development Kit ��������������������������������������������������������������������������������������������10 Installing Visual Studio Code �������������������������������������������������������������������������������������������������������12 Installing the Salesforce Extensions for Visual Studio Code �������������������������������������������������������14 Chapter Summary �����������������������������������������������������������������������������������������������������������������������17 Chapter 3: Concepts Overview ��������������������������������������������������������������������������������19 Salesforce CLI �����������������������������������������������������������������������������������������������������������������������������20 Visual Studio Code ����������������������������������������������������������������������������������������������������������������������21 Salesforce DX Projects ����������������������������������������������������������������������������������������������������������������26 Source Format Metadata ������������������������������������������������������������������������������������������������������������28 Scratch Orgs �������������������������������������������������������������������������������������������������������������������������������29 Unlocked and 2GP Packages�������������������������������������������������������������������������������������������������������30 Dev Hub ���������������������������������������������������������������������������������������������������������������������������������������31 Namespaces �������������������������������������������������������������������������������������������������������������������������������31 Chapter Summary �����������������������������������������������������������������������������������������������������������������������31 v Table of ConTenTs Part II: Working with Salesforce DX �������������������������������������������������������������33 Chapter 4: Salesforce CLI ���������������������������������������������������������������������������������������35 Anatomy of a CLI Command ��������������������������������������������������������������������������������������������������������36 Command-Line Help �������������������������������������������������������������������������������������������������������������������38 The sfdx Application ������������������������������������������������������������������������������������������������������������������40 Controlling the CLI �����������������������������������������������������������������������������������������������������������������������42 Runtime Configuration Values �����������������������������������������������������������������������������������������������������46 Listing Runtime Configuration Values ������������������������������������������������������������������������������������48 Setting a Runtime Configuration Value ����������������������������������������������������������������������������������49 Reading Runtime Configuration Values ���������������������������������������������������������������������������������51 Clearing a Runtime Configuration Value ��������������������������������������������������������������������������������52 Environment Variables ����������������������������������������������������������������������������������������������������������������53 File Parameters ���������������������������������������������������������������������������������������������������������������������������55 Command-Line Parameters and Arguments �������������������������������������������������������������������������������57 JSON Formatted Command Output ���������������������������������������������������������������������������������������������59 CLI Logging ���������������������������������������������������������������������������������������������������������������������������������62 Salesforce CLI Command Execution �������������������������������������������������������������������������������������������63 Using a Terminal Application �������������������������������������������������������������������������������������������������64 Using the Visual Studio Code Integrated Terminal �����������������������������������������������������������������65 Using the Visual Studio Code Command Palette �������������������������������������������������������������������66 Using a Visual Studio Code Context Menu �����������������������������������������������������������������������������66 Which Command Execution Method? ������������������������������������������������������������������������������������67 Chapter Summary �����������������������������������������������������������������������������������������������������������������������68 Chapter 5: Dev Hub �������������������������������������������������������������������������������������������������69 Creating a Trial Dev Hub Org �������������������������������������������������������������������������������������������������������69 Enabling the Dev Hub ������������������������������������������������������������������������������������������������������������������71 Enable Source Tracking for Sandboxes ���������������������������������������������������������������������������������������72 Enabling Unlocked Packages and Second- Generation Managed Packages �������������������������������73 Enabling Einstein Features ���������������������������������������������������������������������������������������������������������73 Adding Salesforce DX Users��������������������������������������������������������������������������������������������������������74 vi Table of ConTenTs Authorizing a Dev Hub Org ����������������������������������������������������������������������������������������������������������78 Authorizing a Global Scope Dev Hub Org �������������������������������������������������������������������������������79 Authorizing a Local Scope Dev Hub Org ��������������������������������������������������������������������������������82 Authorizing a Global and Local Scope Dev Hub Org ��������������������������������������������������������������84 API Limits ������������������������������������������������������������������������������������������������������������������������������������86 Chapter Summary �����������������������������������������������������������������������������������������������������������������������90 Chapter 6: Projects �������������������������������������������������������������������������������������������������91 This Chapter’s Commands ����������������������������������������������������������������������������������������������������������93 Project Directories and Files �������������������������������������������������������������������������������������������������������93 Creating a Salesforce DX Project ������������������������������������������������������������������������������������������������95 Configuring Salesforce DX Projects ��������������������������������������������������������������������������������������������98 Chapter Summary ���������������������������������������������������������������������������������������������������������������������100 Chapter 7: Orgs �����������������������������������������������������������������������������������������������������101 This Chapter’s Commands ��������������������������������������������������������������������������������������������������������102 This Chapter’s Project ���������������������������������������������������������������������������������������������������������������103 Managing Scratch Orgs�������������������������������������������������������������������������������������������������������������103 Creating Scratch Orgs ���������������������������������������������������������������������������������������������������������104 Listing Authorized Orgs and Active Scratch Orgs ����������������������������������������������������������������107 Displaying Detailed Org Information ������������������������������������������������������������������������������������110 Opening Scratch Orgs ����������������������������������������������������������������������������������������������������������113 Deleting Scratch Orgs ����������������������������������������������������������������������������������������������������������115 Configuring Scratch Orgs ����������������������������������������������������������������������������������������������������������117 Scratch Org Definition File ���������������������������������������������������������������������������������������������������118 Scratch Org Edition ��������������������������������������������������������������������������������������������������������������121 Scratch Org Features and Settings ��������������������������������������������������������������������������������������123 Adding Sample Data �����������������������������������������������������������������������������������������������������������������129 Viewing Scratch Orgs from a Dev Hub Org �������������������������������������������������������������������������������130 Authorizing Non-scratch Orgs ���������������������������������������������������������������������������������������������������133 Logging In to a Non-scratch Org ������������������������������������������������������������������������������������������134 Logging Out of a Non-scratch Org ���������������������������������������������������������������������������������������137 vii Table of ConTenTs Managing Sandboxes ����������������������������������������������������������������������������������������������������������������138 Creating Sandboxes�������������������������������������������������������������������������������������������������������������139 Checking Sandbox Creation Status �������������������������������������������������������������������������������������145 Cloning Sandboxes ��������������������������������������������������������������������������������������������������������������150 Deleting Sandboxes �������������������������������������������������������������������������������������������������������������153 Chapter Summary ���������������������������������������������������������������������������������������������������������������������154 Chapter 8: Metadata ���������������������������������������������������������������������������������������������155 This Chapter’s Commands ��������������������������������������������������������������������������������������������������������156 This Chapter’s Project ���������������������������������������������������������������������������������������������������������������157 Source Format vs� Metadata API Format ����������������������������������������������������������������������������������158 Metadata Synchronization Options �������������������������������������������������������������������������������������������160 Synchronizing Source Format Metadata with Source-Tracked Orgs ����������������������������������������163 Pulling Source Format Metadata from Source- Tracked Orgs ����������������������������������������������164 Pushing Source Format Metadata to Source-Tracked Orgs ������������������������������������������������169 Synchronizing Source Format Metadata with Non- source-Tracked Orgs ���������������������������������173 Retrieving Source Format Metadata from Non-source- Tracked Orgs ���������������������������������175 Deploying Source Format Metadata to Non-source- Tracked Orgs ��������������������������������������185 Excluding Source Format Files from Syncing and Converting ��������������������������������������������������205 Ignoring Source Format Files ����������������������������������������������������������������������������������������������207 Ignoring Metadata Components ������������������������������������������������������������������������������������������209 Synchronizing Metadata API Format Metadata with Source-Tracked and Non-source-Tracked Orgs ���������������������������������������������������������������������������������������������������������211 Retrieving Metadata API Format Metadata from Source- Tracked and Non-source-Tracked Orgs����������������������������������������������������������������������������������������������������212 Deploying Metadata API Format Metadata to Source- Tracked and Non-source-Tracked Orgs����������������������������������������������������������������������������������������������������229 Converting Between Metadata API Format and Source Format ������������������������������������������������243 Converting from Source Format to Metadata API Format ����������������������������������������������������244 Converting from Metadata API Format to Source Format ����������������������������������������������������255 Chapter Summary ���������������������������������������������������������������������������������������������������������������������259 viii Table of ConTenTs Chapter 9: Version Control ������������������������������������������������������������������������������������261 Git, GitHub, and GitHub Desktop ������������������������������������������������������������������������������������������������262 Creating a GitHub Account ��������������������������������������������������������������������������������������������������������265 Installing Git ������������������������������������������������������������������������������������������������������������������������������268 Setting Up Your Git Identity �������������������������������������������������������������������������������������������������������269 Installing GitHub Desktop ����������������������������������������������������������������������������������������������������������270 Working with a New Salesforce DX Project ������������������������������������������������������������������������������274 Creating a Remote GitHub Repository ���������������������������������������������������������������������������������274 This Chapter’s Project ���������������������������������������������������������������������������������������������������������276 Creating a Local Repository �������������������������������������������������������������������������������������������������276 The .gitignore File ����������������������������������������������������������������������������������������������������������277 Performing the Initial Add and Commit �������������������������������������������������������������������������������279 Changing the Branch Name �������������������������������������������������������������������������������������������������281 Linking a Local and Hosted Remote Repository ������������������������������������������������������������������281 Synchronizing Tracked Changes ������������������������������������������������������������������������������������������282 Working with an Existing Salesforce DX Project �����������������������������������������������������������������������287 Example Git Workflow ���������������������������������������������������������������������������������������������������������������288 Creating a Local Feature Branch �����������������������������������������������������������������������������������������289 Creating a Lightning Web Component ���������������������������������������������������������������������������������290 Testing the Component in a Scratch Org �����������������������������������������������������������������������������292 Adding the Component to the Git Staging Area �������������������������������������������������������������������296 Committing the Component Changes ����������������������������������������������������������������������������������297 Pushing Changes from the Local to the Remote Repository �����������������������������������������������298 Creating a Pull Request �������������������������������������������������������������������������������������������������������300 Merging Changes ����������������������������������������������������������������������������������������������������������������301 Chapter Summary ���������������������������������������������������������������������������������������������������������������������304 Chapter 10: Test Users ������������������������������������������������������������������������������������������305 This Chapter’s Commands ��������������������������������������������������������������������������������������������������������306 This Chapter’s Project ���������������������������������������������������������������������������������������������������������������306 Creating Scratch Org Users �������������������������������������������������������������������������������������������������������307 ix Table of ConTenTs Assigning Permission Sets to Users �����������������������������������������������������������������������������������������314 Generating User Passwords ������������������������������������������������������������������������������������������������������320 Chapter Summary ���������������������������������������������������������������������������������������������������������������������323 Chapter 11: Test Data �������������������������������������������������������������������������������������������325 This Chapter’s Commands ��������������������������������������������������������������������������������������������������������325 This Chapter’s Project ���������������������������������������������������������������������������������������������������������������326 Executing SOQL Queries �����������������������������������������������������������������������������������������������������������327 Record CRUD Commands ����������������������������������������������������������������������������������������������������������330 Creating Records �����������������������������������������������������������������������������������������������������������������331 Reading Records �����������������������������������������������������������������������������������������������������������������333 Updating Records ����������������������������������������������������������������������������������������������������������������337 Deleting Records �����������������������������������������������������������������������������������������������������������������339 Bulk Data Commands� ���������������������������������������������������������������������������������������������������������������340 Bulk Upserting ���������������������������������������������������������������������������������������������������������������������341 Bulk Deleting �����������������������������������������������������������������������������������������������������������������������352 SObject Tree Commands �����������������������������������������������������������������������������������������������������������355 Creating Hierarchical Test Data �������������������������������������������������������������������������������������������357 Export Command SOQL Queries ������������������������������������������������������������������������������������������361 Exporting Without a Plan Definition File ������������������������������������������������������������������������������362 Importing Without a Plan Definition File ������������������������������������������������������������������������������369 Exporting with a Plan Definition File������������������������������������������������������������������������������������371 Importing with a Plan Definition File �����������������������������������������������������������������������������������379 Chapter Summary ���������������������������������������������������������������������������������������������������������������������380 Chapter 12: Development �������������������������������������������������������������������������������������381 This Chapter’s Commands ��������������������������������������������������������������������������������������������������������382 This Chapter’s Project ���������������������������������������������������������������������������������������������������������������383 Working with Apex Classes �������������������������������������������������������������������������������������������������������383 Executing Anonymous Apex �������������������������������������������������������������������������������������������������384 Creating Apex Classes ���������������������������������������������������������������������������������������������������������387 Creating Apex Triggers ���������������������������������������������������������������������������������������������������������391 x