Developer Tools

Step by Step Guide to Leveraging AI in Writing Salesforce Apex Code

And write codes 3X Faster

Write Salesforce Apex Code Using AI

On November 30th, 2022, the technology world experienced a major shift when ChatGPT chatbot was launched. It has taken the technology world by storm and could make traditional search engines like Google irrelevant with its ability to understand and respond to human language in a conversational manner and with unprecedented accuracy.

AI is changing the world the way Computers changed it in the 1980s and Internet changed it in the 1990s. And this blog post showcases on such example where AI can assist developers in writing code based on their intent, using the AI tool GitHub Copilot.

What is GitHub Copilot?

GitHub Copilot (GC) is an AI based code completion tool. A copilot is a person who assists the pilot in the cockpit. Similarly, GitHub Copilot assists the developer in writing code. It was developed by GitHub and OpenAI (the same company who is behind ChatGPT)

GC was first announced by GitHub on 29 June 2021 and was made available as a subscription-based service for individual developers on 22nd June 2022. It was made available for Enterprise customers on December 8th, 2022.

And what is amazing about this AI based tool is that you just state your intent as comments in the code and GC will suggest the code to complete the task. When you accept the code, it will be added to the code editor, and you can modify it as per your needs.

I took GC for a spin and experimented with two use cases to come up with Salesforce Apex Code. (It is available as free trial for 60 days, after which there is a subscription price of US$ 10 / month or US$ 100 / year.)

  1. The first one was a simple scenario where I wrote the code update the address of all Contacts for an Account based on the address of the Account.
  2. And the second was slightly more advanced using List, Set & Map features of Apex to populate the WhoId on Task records based on the Email Address.

In both the cases, I was able to write the code 3X faster with the help of GC and that too by a conservative estimate. (Please refer to the video below)

What I liked about GitHub Copilot?

  • The code suggestions were generated based on the comments written in plain English.
  • Complete blocks of code were suggested, and I was able to use them as is in most cases, reducing the time type the code.
  • The correct API names of the objects and fields were suggested, specifically for standard objects and fields in Salesforce, reducing the time to type the API names.
  • For custom objects and fields, GC came up with the API names based on the comment and even added ‘__c’ to the API name. But since the API name in my Salesforce Org was different, I corrected the suggested name. And once I did that, GC AI used the correct API name in subsequent blocks of code. Very cool!

Concerns about GitHub Copilot

  • While GC AI was able to write the code with amazing accuracy, it wasn’t 100% correct all the time. In some tests, it added an “update” statement inside the loop, which is not a best practice. So, you do need to understand the code that it generates and may need to fine tune it.
  • Any code that is written using GitHub Copilot may be subject to copyright if it incorporates existing code that is protected by copyright.
  • GC collects user engagement data that may include personal data. Please make sure that you understand what data is collected and how it is used and shared before deciding to use it for work.

My suggestion, while you should be okay to play with it for your learning and experiment, please take the approval of your organization / client before using it for work.

If you are new to Apex, GitHub Copilot can be a great way to learn the language. Just by typing what you want to do as comments, you will be able to see the blocks of code that GC AI is suggesting. And if you are an experienced developer, GC is a great way to write code faster.

Here is a step by step guide on how to use GitHub Copilot in writing Salesforce Apex Code.

And here is a short video (8 minutes) showing you a demo of writing Salesforce Apex Code with and without using GC AI, and the time it takes.

References & Useful URLs

Salesforce Functions Now Globally Available

#1MinuteTip Salesforce has announced the global availability of Salesforce Functions. You can now deploy Salesforce Functions to the region of your choice.

This should make it easier for development teams to meet their security and regulatory requirements by staying within the Salesforce trust boundary.

Salesforce Functions Now Globally Available

References & Useful URLs

How to Create Org Shape to Create Scratch Orgs

#1MinuteTip When we create scratch orgs (during the build phase to build the solution), it was often a challenge to mirror the features and settings of the Production Org. Unlike Sandboxes, scratch orgs do not inherit anything from the Production Org.

But now, you can create “Org Shape” from your Production Org and then spin up Scratch Orgs from it. So the flow looks like

Production Org -> Org Shape -> Scratch Org

With “Org Shape”, you can pre-define the features, Metadata API settings, edition, limits, and licenses that your scratch orgs should have. (Note that Org shape will still not get the custom metadata that you have built in the Production Org).

Here is a video (5 minutes) on Salesforce Developers channel on YouTube explaining Org Shape and how to create it.

References & Useful URLs

Upcoming Improvements to Salesforce CLI

#1MinuteTip Salesforce CLI (Command Line Interface) is a tool that you can use to build and deploy metadata components in Salesforce orgs, manage orgs, synchronize source to and from orgs, create and install packages etc. One of the major benefits of using Salesforce CLI (or any other CLI for that matter) is that you can automate tasks using O/S scripts.

Salesforce is planning to come up with big improvements in Salesforce CLI. Here is the one liner description of these upcoming improvements. If you would like to find out more, please navigate to Salesforce Developer’s blog post mentioned below.

  1. Improved typing experience: sfdx will let you use spaces or colons as separators
  2. Taxonomy-free CLI: sfdx will let you type your command in any order
  3. Easily discover commands with new dedicated sfdx search command
  4. Flags will become easier to read with hyphens
  5. sfdx will get better help messages, with -h for the concise help and --help for the full version
  6. Commands built for sf will be available in sfdx.
Salesforce CLI Improvements

References & Useful URLs