Back to Articles
article cover
author avatar
Rimon Hanna
5 months ago
Writing

Mastering Salesforce Development with Salesforce CLI

Setting Up Your Development Environment

Before diving into Salesforce development, you need to set up your environment. This involves installing the necessary tools and configuring your Salesforce project.

If you're looking to simplify the complexities of these processes so you can focus on innovation rather than process management, we've got the solution for you! 

Join our limited early access waitlist now and be among the selected to leverage Tekunda Serpent to transform your workflow!

Installing Salesforce CLI

First, you need to install the Salesforce CLI. This tool is essential for interacting with Salesforce orgs, creating scratch orgs, and managing metadata.

  1. Download and Install SF CLI:

    Download the installer from the Salesforce CLI page. Follow the installation instructions for your operating system.

  2. Authenticate with Salesforce:

    After installing the CLI, authenticate with your Salesforce org using the following command:

    sf login
  3. Set Up Your Salesforce DX Project:

    Create a new Salesforce DX project or navigate to your existing project directory. Initialize the project using:

    sf project create --name YourProjectName

Creating a Scratch Org

Scratch orgs are ephemeral environments that you can use for development and testing. They can be namespaced or non-namespaced, depending on your requirements.

  1. Define the Scratch Org Configuration:

    Create a config/project-scratch-def.json file in your project directory with the necessary configuration. For example:

    {
      "orgName": "Demo Company",
      "edition": "Developer",
      "features": ["Communities", "ServiceCloud"],
      "settings": {
        "orgPreferenceSettings": {
          "s1DesktopEnabled": true,
          "selfSetPasswordInApi": true
        }
      }
    }
  2. Create the Scratch Org:

    Use the following command to create a scratch org:

    sf org create scratch --definitionfile config/project-scratch-def.json --setdefaultusername --durationdays 7 --alias YourScratchOrgAlias

Working with Namespaced Orgs

If you're developing a managed package, you might need a namespaced scratch org. Namespaced orgs are particularly useful for developing and testing managed packages because they allow you to test your code in an environment that closely resembles your production environment. Here are some common use cases for namespaced orgs:

  1. Isolation of Code: Namespaced orgs help in isolating your code from other packages and customizations in your Salesforce environment. This isolation ensures that there are no conflicts between your code and other packages.
  2. Testing Managed Packages: When developing managed packages, it's crucial to test them in a namespaced org to ensure that the package's namespace is correctly applied and does not interfere with other code or packages.
  3. Consistency with Production: Namespaced scratch orgs mimic the behavior of your production environment, providing a consistent testing ground. This consistency helps identify potential issues that could arise in production.
  4. Compliance and Security: By using namespaced orgs, you can better manage compliance and security aspects, ensuring that your managed packages adhere to Salesforce's security guidelines.

Ensure your Dev Hub has a namespace registered, and include the namespace in your scratch org definition.

Deploying Metadata and Code

With your scratch org ready, you can deploy metadata and code. This process involves pushing your local source to the scratch org and setting up any necessary test data and permissions.

Deploying to the Scratch Org

  1. Push Source to Scratch Org:

    Deploy your local source to the scratch org using:

    sf project deploy start
  2. Assign Permission Sets:

    Assign necessary permission sets to your user:

    sf org assign permset --name YourPermsetName
  3. Load Test Data:

    If you have test data, load it using SOQL queries or by importing data files:

    sf data import tree --plan data/sample-data-plan.json
  4. Install Dependencies:

    If your project depends on managed packages, install them using:

    sf package install --package YourPackageAliasOrId --wait 10

If you're looking to simplify the complexities of these processes so you can focus on innovation rather than process management, we've got the solution for you! 

Join our limited early access waitlist now and be among the selected to leverage Tekunda Serpent to transform your workflow!

Source Control Management

Effective source control is crucial for team collaboration and project integrity. Git is the most commonly used source control system in Salesforce development.

Using Git for Source Control

  1. Initialize Git Repository:

    If your project isn't already under source control, initialize a Git repository:

    git init
  2. Add and Commit Changes:

    Add your changes to the repository and commit them:

    git add .
    git commit -m "Initial commit"
  3. Create Feature Branches:

    Use Git Flow to manage your branches. Create a new feature branch for development:

    git checkout -b feature/new-feature
  4. Fetch and Merge Changes:

    Regularly fetch and merge changes from the main branch to keep your feature branch up-to-date:

    git fetch origin
    git merge origin/main
  5. Commit and Push Changes:

    After developing your feature, commit and push your changes:

    git commit -m "Developed new feature"
    git push origin feature/new-feature
  6. Create Pull Requests:

    Open a pull request to merge your feature branch into the main branch. Ensure your code is reviewed and approved by your team.

Managing Releases

Once your development and testing are complete, it's time to manage releases. This process can differ depending on whether you're working with a non-packaged org or a managed package.

Releasing to a Non-Packaged Org

  1. Deploy to Production:

    Deploy your changes to the production org using the Salesforce CLI:

    sf project deploy start --target-org YourProductionOrgAlias
  2. Run Tests:

    Ensure all tests pass before finalizing the deployment:

    sf apex run test --target-org YourProductionOrgAlias
  3. Monitor Deployment:

    Use the Salesforce CLI to monitor the deployment status:

    sf project deploy report --target-org YourProductionOrgAlias --job-id <yourDeploymentJobId>

Releasing a Managed Package

  1. Create a Managed Package:

    Create a new package version using the Salesforce CLI:

    sf package version create --package YourPackageAlias --installation-key YourKey --wait 10 --code-coverage
  2. Promote the Package Version:

    Promote the package version to make it available for installation:

    sf package version promote --package "YourPackageVersionId"
  3. Submit for Security Review:

    If your package will be listed on the AppExchange, submit it for a security review. This process ensures your package meets Salesforce's security standards.

  4. Install the Package:

    Install the managed package in your production org:

    sf package install --package YourPackageVersionId --target-org YourProductionOrgAlias --wait 10

Conclusion

Developing for Salesforce using the Salesforce CLI involves several steps, from setting up your development environment to managing releases. By leveraging scratch orgs, deploying metadata and code, handling source control, and managing releases efficiently, you can streamline your Salesforce development process.

Whether you're developing for a non-packaged org or creating a managed package, the Salesforce CLI offers powerful commands to automate and simplify your workflow. By following best practices and maintaining effective source control, you can ensure your projects are well-managed and ready for deployment.

If you're looking to simplify the complexities of these processes so you can focus on innovation rather than process management, we've got the solution for you! 

Join our limited early access waitlist now and be among the selected to leverage Tekunda Serpent to transform your workflow!