Developer Tools

Git and Salesforce: A Getting Started Guide for Development

Many developers find it challenging to use Git with Salesforce. Even though about 99% open-source projects use some kind of version control like Git, it’s still hard to blend it smoothly into Salesforce projects. This can lead to lost work, confusion over different code versions, and trouble working together.

This guide offers a step-by-step approach to seamlessly integrating Git with Salesforce, ensuring your development process is smooth, efficient, and error-free.

Understanding Git Basics

Git is a distributed version control system designed to help developers track changes in their code over time. It allows multiple developers to collaborate seamlessly on a project by providing a central repository where all changes are stored.

4 Key Git Concepts

In Salesforce development, Git introduces critical concepts such as repositories, branches, commits, and pull requests to enhance version control and collaboration.

  1. A repository is where the source code and project files are stored. It serves as the single source of truth for the development team.
  2. A branch is a pointer to a specific commit in the repository and represents an independent line of development.
  3. Commits represent a set of changes made to the code and include a unique identifier. It acts as a snapshot of the code at a specific point in time, enabling developers to track and manage the evolution of the codebase.
  4. Pull requests provide a platform for collaboration and team ownership in the development process. When developers complete their work on a feature branch, they can create a pull request to propose their changes for merging into the main codebase.

9 Best Practices for Version Control in Salesforce

Version control is crucial for Salesforce development as it helps track changes, collaborate efficiently, and maintain code integrity. Here are the best practices for version control in Salesforce:

1. Choose the Right Version Control System

  • Git is King: Given its widespread adoption and robust features, Git is often the preferred choice. It’s crucial to select a version control system that integrates well with Salesforce and meets the team’s collaboration needs.

2. Branching Strategy

In Salesforce projects, implementing effective strategies for branching and merging is crucial for maintaining code stability and facilitating collaboration among developers. Here are some strategies to consider in your branching strategy:

  1. Implement a Branching Model: Use a branching strategy like GitFlow or Feature Branching to manage different development streams, new features, and bug fixes.
  2. Isolate Development: Keep feature development and bug fixes separate from the main branch to avoid conflicts and ensure stability.
  3. Feature Branching: Create separate branches for each new feature or development task. This allows developers to work independently without impacting the stability of the main branch. Once the feature is complete, it can be merged into the main branch.
  4. Release Branching: Create a release branch before deploying changes to production. This branch acts as a controlled environment for testing and finalizing features. Once the release branch is stable, it can be merged into the main branch for deployment.
  5. Continuous Integration: Implement a continuous integration process to merge changes from different developers into the main branch frequently. This ensures that conflicts are identified and resolved quickly, promoting seamless collaboration.

3. Consistent Commit Practices

  • Frequent Commits: Regularly commit changes to track progress and avoid massive, hard-to-manage updates.
  • Descriptive Commit Messages: Each commit should have a clear, concise message that explains the changes and the reason behind them.

4. Automate Deployments

  • Continuous Integration (CI): Implement CI to automate the testing and deployment of Salesforce metadata. Tools like Jenkins, Bamboo, or Salesforce DX can be used.
  • Validate Before Deploying: Always validate your changes in a test environment or scratch org before merging into the main branch.

5. Handle Conflicts Promptly

  • Regular Merges: Frequently merge changes from the main branch into feature branches to minimize conflicts.
  • Conflict Resolution: When conflicts arise, resolve them promptly and carefully to maintain code integrity.

6. Track and Audit Changes

  • Use History Wisely: Leverage the version history to understand changes, roll back when necessary, and audit modifications for compliance and debugging.

7. Backup and Security

  • Regular Backups: Ensure your version control repository is backed up regularly to prevent data loss.
  • Access Control: Manage access to the repository, ensuring that only authorized personnel can make changes to critical branches.

8. Training and Documentation

  • Educate Your Team: Ensure that all team members are trained in using version control systems and understand the team’s branching and commit strategies.
  • Document Your Processes: Maintain clear documentation for your version control processes, standards, and branching model.

9. Review and Refine

  • Code Reviews: Implement peer review processes for commits to improve code quality and share knowledge within the team.
  • Iterate Your Strategies: Regularly review and update your version control strategies to adapt to changing project needs and team dynamics.

Setting up Git for Salesforce Development

Setting up Git for Salesforce development is essential for effective version control and seamless collaboration within development teams. With version control, you can track changes, revert to previous versions if necessary, and maintain a centralized repository for your codebase.

To integrate Git with Salesforce DX, follow these steps:

1. Install Git: Begin by installing Git on your local machine. Visit the Git website and download the appropriate version for your operating system. It’s available for Windows, Mac, and Linux.

2. Set up Salesforce DX: Salesforce DX is a set of tools and features that enhance development on the Salesforce platform. Install Salesforce CLI, which includes the necessary commands to interact with Salesforce DX. Visit the Salesforce CLI installation page.

3. Initialize Git: Open the command line interface and navigate to your project directory. Use the the following command to initialize Git within your project folder.

git init

4. Connect to a remote repository: If you already have a remote repository set up, use the following command to connect your local repository to the remote one. If not, create a new repository on a hosting platform like GitHub or Bitbucket and then run the following command

git remote add origin <remote_repository_URL>

5. Create a Salesforce DX project: Use the Salesforce CLI to create a new project in your desired directory. This project will serve as the local workspace for your Salesforce development. In your prompt, run the following:

sf project generate -n my-salesforce-project

6. Work with scratch orgs: Salesforce DX uses scratch orgs, which are temporary Salesforce environments for development and testing. Use the Salesforce CLI to create and manage scratch orgs. Develop and test your changes in these orgs before pushing them to the main codebase.

sf org create scratch -edition developer -alias my-scratch-org

7. Create Branches: Create separate branches for different features or bug fixes using the following command command. This allows for isolated development and enables team ownership of specific features.

git branch feature-branch-name

8. Switch to the branch: Switch to the newly created branch using the following command. This will ensure that any changes you are making to the code will be saved in the new branch.

git checkout - b feature-branch-name

9. Make changes in scratch org: Use the Salesforce CLI and your preferred development tools, such as Visual Studio Code, to make necessary changes to the scratch org.

10. Pull changes: If the changes have been made directly in the Salesforce Org, and once you are satisfied with the changes made, pull these changes from the org as metadata files to store it in source control.

sf project retrieve start

11. Push changes to the Git repository: use Git commands to add and commit the changes locally. Then, push these changes to the corresponding branch in the Git repository.

git add [file-name or .]
git commit -m "Description of the changes"
git push origin feature/my-feature-branch

12. Merge changes: To ensure collaboration with other developers, regularly pull changes from the principal or developer branches using Git commands. Merge these changes into your branch to incorporate the latest updates.

git checkout main
git pull origin main
git checkout feature/my-feature-branch
git merge main

Handling Conflicts and Merges

Resolving conflicts and effectively merging changes is essential for maintaining code stability and ensuring a smooth development process. To handle conflicts in Salesforce metadata, developers can follow these steps:

  1. Identify the conflicting components: Use the Git command line interface or a Git GUI tool to identify the components with conflicting changes.
  2. Understand the changes: Review the conflicting changes made by different developers to understand the differences and determine the desired outcome.
  3. Make necessary adjustments: Modify the conflicting metadata components to resolve the conflict. This could involve reconciling differences, rewriting code, or collaborating with other developers to find a resolution.
  4. Test the changes: After resolving the conflict, it is crucial to test the merged components thoroughly to ensure code stability and functionality.
  5. Commit and push the changes: Once the conflicts are resolved and the code is stable, commit the changes to the respective branch and push them to the remote repository.

Final Thoughts

Using Git and Salesforce can significantly enhance the development process and improve collaboration within development teams. Git-based version control provides a centralized and organized system for managing code changes, ensuring a single source of truth for the development project.

With features like branches and pull requests, developers can work on separate features and easily merge them into the main codebase. This allows for seamless collaboration and a tighter feedback loop within the team.

If developers integrate third-party DevOps tools and automation, the development and deployment process can be streamlined and more efficient. Additionally, scratch orgs and clicks-based DevOps solutions enable developers to quickly and easily test and deploy changes in a production-like environment.

Salesforce Code Builder is Now Generally Available

Salesforce announces the general availability of its much awaited tool Code Builder. Salesforce Code Builder is a web-based development environment, designed to modernize the experience of building applications on the Salesforce platform.

Code Builder runs in the browser and is easily available without the need for complex integrations. This is unlike traditional development tools that require installation and setup on a local machine.

Built on Microsoft’s Visual Studio Codespaces, it offers many of the powerful features of Visual Studio Code. This includes support for extensions, a familiar interface for those who’ve used VS Code, and a robust set of tools for coding, debugging, and integrating with other Salesforce tools.

Salesforce Code Builder is Now Generally Available

References & Useful URLs

A Sneak Peak of Einstein for Developers

Einstein for Developers is an AI-powered tool that assists Salesforce developers in writing code more efficiently and effectively.

It is available as a Visual Studio Code extension and utilizes the power of Salesforce’s CodeGen large language model to generate code from natural language instructions.

For example I asked Einstein for Developer to “Write an apex code to accept an account id and populate the account’s billing address to all contacts mailing address for that account”

And here is the Apex code that it came up with:

// Apex class to populate the Account's billing address to all its related Contact Mailing addresses
public with sharing class AddressUtil {
    
    // Method to update the Address on Account and Contacts
    public static void updateAddress(Id accountId, String billingStreet, String billingCity, String billingState, String billingPostalCode, String billingCountry) {
        // Update the Address on the Account
        Account account = [SELECT Id, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry FROM Account WHERE Id = :accountId LIMIT 1];
        if (account != null) {
            account.BillingStreet = billingStreet;
            account.BillingCity = billingCity;
            account.BillingState = billingState;
            account.BillingPostalCode = billingPostalCode;
            account.BillingCountry = billingCountry;
            update account;

            // Update the Address on all related Contact Mailing addresses
            List<Contact> contacts = [SELECT Id, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry FROM Contact WHERE AccountId = :accountId];
            if (contacts != null) {
                for (Contact contact : contacts) {
                    contact.MailingStreet = billingStreet;
                    contact.MailingCity = billingCity;
                    contact.MailingState = billingState;
                    contact.MailingPostalCode = billingPostalCode;
                    contact.MailingCountry = billingCountry;
                }
                update contacts;
            }
        }
    }
}

You can use it Einstein for Developers to create boilerplate code, get code suggestions, and scan for code vulnerabilities.

One of the major benefits of using “Einstein for Developers” over other similar tools (e.g. GitHub Copilot) is that it is trained for Salesforce languages and frameworks. The other major benefit is it will evolve every time Salesforce releases new features on the platform. For example when Dataweave in Apex was introduced, Einstein for Developers could write Apex code to use Dataweave.

Here is a sneak peek of Einstein for Developers in Action.

References & Useful URLs

#SalesforceWinter24 – Salesforce Code Analyzer Visual Studio Code Extension (Beta)

#1MinuteTip #SalesforceWinter24 This extension enables VS Code to use Code Analyzer to interact with your code. Run scans using PMD and RetireJS engines plus Salesforce Graph Engine to produce lists of violations and improve your code.

Product Area: Developer Tools

Salesforce Code Analyzer Visual Studio Code Extension
Image Source: Winter ’24 Release Note Article

References & Useful URLs

Want to Receive these Tips in your Inbox?

Finding it overwhelming to keep pace with Salesforce’s new release features? Try our “1 Day 1 Tip 1 Minute” emails, where you are going to get 1 tip every day that won’t take more than a minute to read. To find out more and subscribe please click here.

#SalesforceWinter24 – Monitor Async Apex Job Limit Usage

#1MinuteTip #SalesforceWinter24 Monitor your org’s async Apex usage on the Apex Jobs Setup page to mitigate potential limit problems before they happen. You see the percentage of async Apex used and how many Apex operations have been used out of the allowed 24-hour org limit.

Product Area: Development -> Apex

Monitor Async Apex Job Limit Usage

References & Useful URLs

  • For Related Winter ’24 Release Note Article – Click Here

Want to Receive these Tips in your Inbox?

Finding it overwhelming to keep pace with Salesforce’s new release features? Try our “1 Day 1 Tip 1 Minute” emails, where you are going to get 1 tip every day that won’t take more than a minute to read. To find out more and subscribe please click here.

Scroll to Top
Introducing All Access Pass