Blog Posts

Blog Posts

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 Flows – 26 Features Introduced in Spring ’23 Release

Salesforce Flows Spring '23 Features

Flows is the present and future of Automation in Salesforce and continues to take more and more Centre stage. Every Salesforce release contains tens of enhancements to Flows. In Spring ’23 Release of Salesforce, we could find about 26 enhancements related to Salesforce Flows.

And here is our top pick from these enhancements:

Solution Options for Inbound Integration to Salesforce

What are the different solution options for Inbound Integration to Salesforce? That is sending data from external systems into Salesforce? Here is the list of options to choose from.

  1. Salesforce APIs
  2. Custom Apex Class
  3. Platform Events
  4. File Based Integration
  5. Other Salesforce/3rd Party Products

References & Useful URLs

My Experiment with ChatGPT for Salesforce Related Questions

ChatGPT is an is a AI powered chatbot that will provide you answers to any questions that you ask. It was released as a prototype in late November ‘2022 and has become a rave in the technology world. It stands for “Chat Generative Pre-trained Transformer”.

Though you can ask any question under the sun, I experimented with asking questions related to Salesforce and the responses were mind boggling. I think this takes the technology in general and AI in particular to another level.

Here is an example. I asked chatGPT “Give me salesforce apex code to update all contact addresses with account address for an account”

ChatGPT for Salesforce Related Questions

And here is the code that chatGPT generated for me. Unbelievable!

// Retrieve the Account record
Account account = [SELECT Id, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry FROM Account WHERE Id = :accountId];

// Retrieve all the Contacts for the Account
List<Contact> contacts = [SELECT Id, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry FROM Contact WHERE AccountId = :accountId];

// Update the address fields of each Contact with the Account's address
for (Contact contact : contacts) {
  contact.MailingStreet = account.BillingStreet;
  contact.MailingCity = account.BillingCity;
  contact.MailingState = account.BillingState;
  contact.MailingPostalCode = account.BillingPostalCode;
  contact.MailingCountry = account.BillingCountry;

// Save the updated Contacts
update contacts;

Want to have some fun? Try these questions and see what ChatGPT comes up with. 😊

  1. How to learn Salesforce?
  2. How to create custom object in Salesforce?
  3. How do I secure my Salesforce Org?
  4. What are the career options in Salesforce?
  5. How much can I earn as Salesforce Architect?
  6. What is the difference between Aura Component and LWC in Salesforce?
  7. Which one is a better automation tool in Salesforce – Workflow Rule, Process Builder or Salesforce Flow?
  8. What are best practices for Salesforce Apex?
  9. What are some Salesforce Admin Interview Questions?
  10. How does Salesforce compare with AWS, Google Cloud and Microsoft?
  11. Please give me the code for a simple data entry form in Salesforce LWC?
  12. How do I update all contacts address when account address changes in Salesforce?
  13. How do I create a Flow in Salesforce to delete all cases for an Account?

Here is the URL for you to try this out –