EinsteinGPT For Developers: How To Use It? How not to use it?
Andres Perez
Influencer | Senior Manager, Trailhead Academy Solution Architect Lead at Salesforce
TL: DR; The article discusses the use of EinsteinGPT for Developers, a tool that generates code based on prompts. It covers the mechanics of using the tool, provides examples of correct and incorrect code generated, and discusses when to use GPT tools. The importance of reading and verifying the code generated. The article also mentions the limitations of GPT tools and the need for developer skills to improve the quality of the generated code. It concludes that GPT tools can be helpful for writing boilerplate code and getting syntax right, but developers should not rely on them entirely and must read the code generated.
I recently created a post on LinkedIn that surprisingly had many hits and even new followers (thanks for your support). So, I have decided to expand on the idea and cover how and when to use the new EinsteinGPT For Developers working with Apex code. But I will go further and explain why you want to use it and more importantly when not to use it.
?? EinsteinGPT For Developers is still in development on Beta. Handle with care ??
EinsteinGPT For Developers Mechanics
From this point forward, we will give a nickname to the “EinsteinGPT For Developers” tool and call it E-GPT for short.
Let’s start with the basics, how to use E-GPT? You must follow several steps to get E-GPT working:
One common way of using EinsteinGPT For Developers is as follows:
Should you read the code that is being written? ABSOLUTELY! Sometimes E-GPT, like any other GPT tool makes stuff up that must be corrected. This is so common, that it receives a name: hallucinations.
That idea was the basis of my post that started this blog:?
Developers do not, and will not, get paid for writing code. We do get paid, and will get paid, for reading code !!!
As mentioned above, this includes ensuring the code generated by any GPT engine is correct.
So, what does E-GPT do?
Now that we have covered the mechanics of how to use E-GPT, let’s let’s take a closer look at how this tool can be used, and how it should not be used. For this section, I am going to write some prompts and what E-GPT does well and what it does not do well.
Vague prompts
With the first prompt, we are going to ask to have a lot of code written, but without providing too much information and see where it takes us. So this is the prompt I am using with E-GPT:
Write an Apex trigger and it's class to properly handle case assignments
This is the code generated:
trigger CaseTrigger on Case (before insert, before update) {
if (Trigger.isInsert || Trigger.isUpdate) {
CaseTriggerHandler handler = new CaseTriggerHandler();
handler.handleCases(Trigger.new);
}
}
public class CaseTriggerHandler {
public void handleCases(List<Case> cases) {
for (Case c : cases) {
if (c.AssignmentRuleId == null) {
c.AssignmentRuleId = '01p60000007FbMS';
}
}
}
}
What did E-GPT do right?
What did it do wrong?
But more importantly, E-GPT does not know how to “properly handle case assignment”. Do we want to assign the case to support levels 1, 2 or 3? Do we want a round-robin? Is this a priority customer who requires a special queue? Is this a red account? Etc, etc.
Sure, we could have added those details to the prompt to be more specific and help E-GPT generate better code. Let’s try that…
Prompt:
Write the Apex code to properly handle case assignments following these rules: 1. New cases should go to "level 1" support. 2. There is a queue for each region, assign the cases to the correct case depending on the contact's region if this is not a new case.
This is the code generated
trigger HandleCaseAssignment on Case (before insert, before update) {
// Get the regions and their corresponding queues
Map<String, String> regionsToQueues = new Map<String, String>();
for (Group q : [SELECT Id, Name FROM Group WHERE Type = 'Queue' AND Name IN ('North America', 'Europe', 'Asia Pacific')]) {
regionsToQueues.put(q.Name, q.Id);
}
for (Case c : Trigger.new) {
if (c.ContactRegion__c != null) {
// Assign the case to the correct queue
if (regionsToQueues.containsKey(c.ContactRegion__c)) {
c.OwnerId = regionsToQueues.get(c.ContactRegion__c);
} else {
// No queue found for the region, default to the level 1 queue
c.OwnerId = levels1to5.Id;
}
} else {
// Contact region not provided, default to the level 1 queue
c.OwnerId = levels1to5.Id;
}
}
}
What did E-GPT do right?
What did it do wrong?
About that last point, I was expecting that if the case is being inserted (new case) then it should go to level 1 support, but if we are updating a case and it’s being escalated from level 1 support (as specified by some status field) then it would look at the region. Maybe I could have written a better prompt…
I could go on and on with multiple other prompts and have E-GPT generate more and more code, but from the results we have so far, we can already see some interesting things.
At this time we would be programming in a new programming language called “GPT Prompts” rather than Apex. This by the way brings me to another interesting, somewhat related, idea that I have been thinking about recently: Software Development Evolution.
Our Jobs Are Safe!
As you saw in the previous samples, E-GPT got some things right but it also got some things wrong. E-GPT needs a lot of help, via writing good prompts, to get things the way we need them.
I think GPT will never replace software developers, since it’s difficult to make predictions in this industry, let’s say that we won’t be replaced for now.
We will need people who have “developer” skills to come up with good pseudocode prompts to have GPT generate good code. But even that, it’s still in the future.
So, just like we don’t wire cables to program computers anymore, or we don’t write code in Assembler and keep track of those darn registries, we have evolved to write code in higher languages like Apex, and in the future, we will evolve to write code using GPT prompts.
This evolution has allowed us to get more by doing less. Can you imagine building a complex web application in Assembler? ??
Should I use GPT tools today?
Can I use GPT tools today? Or do I have to wait for the future to arrive?
I am not saying that GPT tools are useless, as you saw before it got some things correct. You just have to have the right expectations of the tools and what they can help you with.
So what can I expect from a GTP tool??
The short answer is that E-GPT will replace searching for the right syntax in the developer guides, googling for some sample code, or even replacing some basic tutorials.
E-GPT is a great assistant tool that will help you (not replace you, not yet) in writing code. You are still in control of the code that gets generated and you MUST read (and fix) the code that GPT tools create for you.
These use cases will give you a good idea of when to use E-GPT or any other GPT-based tools:
E-GPT Can Write Some Boilerplate Code
Prompt:?
What is the syntax for a batch Apex class that preserves the state between transactions?
Code generated:
领英推荐
public class PreserveStateBatch implements Database.Batchable<sObject>, Database.Stateful {
public List<sObject> records = new List<sObject>();
public Database.QueryLocator start(Database.BatchableContext context) {
return Database.getQueryLocator([SELECT Id, Name, State FROM Account]);
}
public void execute(Database.BatchableContext context, List<Database.SaveResult> results) {
for (Database.SaveResult result : results) {
if (result.isSuccess()) {
sObject record = result.getRecord();
records.add(record);
}
}
}
public void finish(Database.BatchableContext context) {
// The records list is preserved between transactions
System.debug(records);
}
}
Note that E-GPT got us a good starting point, but there are still syntax issues with the code generated.
When we copy this code to a class and compile it, we get this error:
Class PreserveStateBatch must implement the method: void Database.Batchable<SObject>.execute(Database.BatchableContext, List<SObject>) (1:14)
The reason is that E-GPT hallucinated again… The parameter is a list of sObjects, rather than a list of? Database.SaveResults. In this particular case, we can use a list of Accounts. Let’s fix that and compile it again.
This time we get a different error:
SELECT Id, Name, State FROM Account
ERROR at Row:1:Column:18???????????????????????????????
No such column 'State' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. (4:35)
The reason is that E-GPT hallucinated again… The Account sObject does not have a field named “state”. Let’s fix that and compile it again.
Good... the code compiles, and we can start writing out the logic required to solve our requirements.
E-GPT Can Help Get The Syntax Right
This is one that I use multiple times per day because it’s hard to remember the syntax for everything. Before I used E-GPT, I had to Google and find some documentation that would help me find the answer. Nowadays I ask E-GPT to give me the answer and it’s much faster!
Prompt:
Write a trigger that uses a switch statement to check the type of event being handled
Response:
trigger OpportunityTrigger on Opportunity (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
switch on Trigger.operationType {
when before_insert {
// Insert logic here
}
when before_update {
// Update logic here
}
when before_delete {
// Delete logic here
}
when after_insert {
// Insert logic here
}
when after_update {
// Update logic here
}
when after_delete {
// Delete logic here
}
when after_undelete {
// Undelete logic here
}
}
}
This time it got it right, but be careful because when I was writing the blog and coming up with the examples E-GPT gave me the wrong answer.
Prompt:
Write a trigger that uses a switch statement
Response:
trigger TriggerName on ObjectName(before insert, before update) {
switch on Trigger.operationType {
when HOLD {
// Logic for hold operation
}
when RELEASE {
// Logic for release operation
}
when EDIT {
// Logic for edit operation
}
when INSERT {
// Logic for insert operation
}
when DELETE {
// Logic for delete operation
}
when UNDELETE {
// Logic for undelete operation
}
}
}
We know it’s completely inaccurate… So make sure you READ the code generated. If it does not make sense, ask again. Change the prompt slightly like in the previous example and try again.
E-GPT Generates Test Data
When writing test code, it sometimes it’s useful to create some test data so we could use a prompt like this:
Write some Apex code to generate a list of 50 Contact records with required fields populated, then insert them into the database.
Response:
// Create a list of 50 Contact records
List<Contact> contacts = new List<Contact>();
for (Integer i = 1; i <= 50; i++) {
Contact contact = new Contact();
contact.FirstName = 'FirstName ' + i;
contact.LastName = 'LastName ' + i;
contact.Email = 'email' + i + '@example.com';
contacts.add(contact);
}
// Insert the Contacts into the database
insert contacts;
Not bad at all, let’s challenge E-GPT to see how it does with this prompt:
Write some Apex code to generate a list of 50 Contact records with required fields and all my custom fields populated, then insert them into the database.
Response:
// Create a list of 50 Contact records
List<Contact> contacts = new List<Contact>();
// Retrieve a list of custom fields on the Contact object
Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Contact.fields.getMap();
// Populate the required fields for each Contact record
for (Integer i = 0; i < 50; i++) {
Contact contact = new Contact();
contact.FirstName = 'FirstName ' + i;
contact.LastName = 'LastName ' + i;
// Populate all custom fields on the Contact object
for (String fieldName : fieldMap.keySet()) {
Schema.SObjectField field = fieldMap.get(fieldName);
contact.put(fieldName, field.getDescribe().getDefaultValue());
}
contacts.add(contact);
}
// Insert the Contact records into the database
insert contacts
Interesting response, it knows how to work with Dynamic Apex to find the fields on the Contact sObject and how to get the default values but it populates every field with the default value, which is not needed because it’s the default value ??
So once more… Make sure you READ the code generated, do not trust it at face value.
Let me give you another example, this time I want to use this prompt:
How to make a post request from Apex?
Response:
HttpRequest req = new HttpRequest();
req.setEndpoint('https://yourEndpoint.com');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody('{name: "John Doe", age: 30}');
Http http = new Http();
HttpResponse res = http.send(req);
Not bad at all!
When I got this code executed anonymously, I got this error:
System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://yourEndpoint.com
So I asked E-GPT for help, but it could not explain the error. This is probably because E-GPT is still in beta mode. But I have another GPT-based tool that I like using, and when I ask that tool about the error, it explains it very well. See reponse here
E-GPT Writes Test Code
E-GPT can help you write Apex test code to help test and deploy your code.
Prompt:
Write the Apex test code for this code
public with sharing class EinsteinDemo {
????public static integer sum(integer a, integer b) {
????????return a + b;
????}
}
????This is the response that got generated
@isTest
public class EinsteinDemoTest {
@isTest
static void testSum() {
System.assertEquals(3, EinsteinDemo.sum(1, 2));
System.assertNotEquals(5, EinsteinDemo.sum(1, 2));
System.assertNotEquals(4294967294, EinsteinDemo.sum(2147483647, 2147483647));
}
}
Not bad, the assertions look appropriate. Especially the third one because the result should not be larger than the sum of the maximum integer numbers available. The problem is that the code does not compile and I get this error:
Illegal integer (7:32)
I could go on, and on, with many examples of when to use GPT-based tools, and when not to use them, and please READ the code that gets generated. It will never be perfect!
As you saw in the samples, E-GPT can be a great help for working with prompts that will give you short, direct and specific answers rather than entire applications. Really good idea to work with E-GPT to come up with boilerplate code and syntax for things that we can't always remember.
PLEASE READ THE CODE GENERATED!
Those tools should not worry about replacing you, at least not shortly, but you should use them to help improve the speed at which you write code and create bigger and nicer projects, just like previous tools have done for software developers in the history of this amazing field.
Resources:
Tech-for-good enthusiast. Head of Systems Improvement at Social Investment Business. Salesforce Application Architect, Agile PM. Azure dabbler
10 个月I’ve found it helpful for unit tests. But the character limit on the prompt can make this tricky. Any tips?