Deployment

Apex Deployment

Deploying Apex Code

Apex deployment uses Change Sets or Salesforce CLI.

Introduction to Apex Deployment

Apex deployment is a crucial step in moving your Salesforce development work from a sandbox environment to a production environment. Salesforce provides two primary methods for deploying Apex code: Change Sets and the Salesforce CLI. Each method has its benefits and use cases.

Deploying Apex with Change Sets

Change Sets are a point-and-click tool in Salesforce that allows you to package up and deploy your Apex code and other metadata from one Salesforce org to another. This method is ideal for users who prefer a more visual interface or who are not comfortable with command-line tools.

Creating a Change Set

To create a Change Set, follow these steps:

  1. Log in to your Salesforce Sandbox org.
  2. Navigate to Setup.
  3. Enter Change Sets in the Quick Find box, then select Outbound Change Sets.
  4. Click New to create a new Change Set.
  5. Give your Change Set a name and description, then click Save.
After saving, you will add components, such as Apex classes, triggers, and other metadata, to the Change Set.

Uploading and Deploying a Change Set

Once your Change Set is complete, you need to upload it to the target org. Here's how:

  1. In your Change Set, click Add to include components like Apex classes or triggers.
  2. After adding components, click Upload and select the target org (e.g., Production).
  3. Log in to the target org and navigate to Inbound Change Sets to deploy.
  4. Locate your Change Set, validate it (if necessary), and then click Deploy.
Note that you must have deployment permissions in the target org to complete this process.

Deploying Apex with Salesforce CLI

For developers who prefer using command-line tools, the Salesforce CLI offers a powerful alternative for deploying Apex code. This method is more suitable for complex deployments, automation scripts, or when integrating with CI/CD pipelines.

Setting Up Salesforce CLI

Before deploying Apex via Salesforce CLI, ensure you have the CLI installed and authenticated to your Salesforce org. Follow these steps:

  1. Download and install the Salesforce CLI from the official website.
  2. Open your terminal or command prompt.
  3. Authenticate your org using the command: sfdx auth:web:login -a MyOrgAlias. This command opens a browser window for you to log in to Salesforce.

Deploying Apex Code Using CLI

Once authenticated, deploy your code using the following steps:

  1. Navigate to the directory containing your Apex code.
  2. Run the command: sfdx force:source:deploy -p path/to/your/apex/code.
  3. Monitor the deployment process directly in your terminal window.
  4. Use additional flags such as --checkonly to validate the deployment without actually deploying.
This method is efficient for iterative deployments and continuous integration setups.

Conclusion

Both Change Sets and the Salesforce CLI offer robust solutions for deploying Apex code. Change Sets are user-friendly and ideal for simpler deployments, while the CLI is better suited for developers who require more control and automation. Choose the method that best fits your workflow and project requirements.

Deployment

Previous
Test Data