Using ChatGPT to create Salesforce triggers and humor
ChatGPT is a prototype artificial intelligence chatbot designed by OpenAI to understand natural human language and generate written text that closely resembles human writing.
I've played with ChatGPT for a little while to create a Salesforce trigger and will tell the results but, first, the routine it gave me when I've asked "Write a Seinfeld routine about Salesforce triggers."
Then I've asked ChatGPT: "Please write a Salesforce trigger that notifies case team members of any new Case Feed post."
Below was the response followed by ChatGPT's very nice explanation: "This trigger will be executed whenever a new Case Feed post is created. It will first query the CaseTeamMember object to retrieve the case team members for the case associated with the post, and will store their user IDs in a set. It will then use these user IDs to query the User object and create a map of users. Finally, it will create an email message for each new Case Feed post, setting the recipients to the case team members, and will send the messages using the Messaging.sendEmail method."
trigger NotifyCaseTeam on CaseFeed (after insert)
? ? // Create a set to store the user IDs of the case team members
? ? Set<Id> userIds = new Set<Id>();
? ? // Loop through each new Case Feed post
? ? for (CaseFeed post : Trigger.new) {
? ? ? ? // Get the ID of the case associated with the post
? ? ? ? Id caseId = post.ParentId;
? ? ? ? // Query the CaseTeamMember object to retrieve the case team members
? ? ? ? List<CaseTeamMember> teamMembers = [
? ? ? ? ? ? SELECT UserId
? ? ? ? ? ? FROM CaseTeamMember
? ? ? ? ? ? WHERE ParentId = :caseId
? ? ? ? ];
? ? ? ? // Add the user IDs of the case team members to the set
? ? ? ? for (CaseTeamMember teamMember : teamMembers) {
? ? ? ? ? ? userIds.add(teamMember.UserId);
? ? ? ? }
? ? }
? ? // Create a map to store the users associated with the user IDs
? ? Map<Id, User> users = new Map<Id, User>(
? ? ? ? [SELECT Id, Email
? ? ? ? ?FROM User
? ? ? ? ?WHERE Id IN :userIds]
? ? );
? ? // Create a list to store the email messages
? ? List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
? ? // Loop through each new Case Feed post
? ? for (CaseFeed post : Trigger.new) {
? ? ? ? // Create a new email message
? ? ? ? Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
? ? ? ? // Set the recipients of the email to the case team members
? ? ? ? message.setToAddresses(
? ? ? ? ? ? users.values().stream()
? ? ? ? ? ? ? ? .map(user -> user.Email)
? ? ? ? ? ? ? ? .collect(Collectors.toList())
? ? ? ? );
? ? ? ? // Set the subject of the email
? ? ? ? message.setSubject('New Case Feed post for case: ' + post.ParentId);
? ? ? ? // Set the body of the email
? ? ? ? message.setPlainTextBody(
? ? ? ? ? ? 'A new Case Feed post has been added to the following case:\n\n' +
? ? ? ? ? ? 'Case ID: ' + post.ParentId + '\n' +
? ? ? ? ? ? 'Post Body: ' + post.Body + '\n\n' +
? ? ? ? ? ? 'Please log in to Salesforce to view the case and the new post.'
? ? ? ? );
? ? ? ? // Add the email message to the list
? ? ? ? messages.add(message);
? ? }
? ? // Send the email messages
? ? Messaging.sendEmail(messages);
}
{
As you can see, the code is well-formatted and commented out, however it will NOT work properly:
Then I've tried again giving further details: "Please write a Salesforce trigger in Apex that notifies case team members of any new Case Feed post and avoid queries inside loops."
The response:
trigger NotifyCaseTeam on CaseFeed (after insert)
? ? // Create a set to store the IDs of the new Case Feed posts
? ? Set<Id> postIds = new Set<Id>();
? ? // Loop through each new Case Feed post and add its ID to the set
? ? for (CaseFeed post : Trigger.new) {
? ? ? ? postIds.add(post.Id);
? ? }
? ? // Query the Case object to retrieve the cases associated with the new posts
? ? Map<Id, Case> cases = new Map<Id, Case>(
? ? ? ? [SELECT Id
? ? ? ? ?FROM Case
? ? ? ? ?WHERE Id IN (
? ? ? ? ? ? ?SELECT ParentId
? ? ? ? ? ? ?FROM CaseFeed
? ? ? ? ? ? ?WHERE Id IN :postIds
? ? ? ? ?)]
? ? );
? ? // Query the CaseTeamMember object to retrieve the case team members
? ? Map<Id, List<CaseTeamMember>> teamMembers = new Map<Id, List<CaseTeamMember>>(
? ? ? ? [SELECT ParentId, UserId
? ? ? ? ?FROM CaseTeamMember
? ? ? ? ?WHERE ParentId IN :cases.keySet()]
? ? );
? ? // Create a map to store the users associated with the user IDs
? ? Map<Id, User> users = new Map<Id, User>(
? ? ? ? [SELECT Id, Email
? ? ? ? ?FROM User
? ? ? ? ?WHERE Id IN (
? ? ? ? ? ? ?SELECT UserId
? ? ? ? ? ? ?FROM CaseTeamMember
? ? ? ? ? ? ?WHERE ParentId IN :cases.keySet()
? ? ? ? ?)]
? ? );
? ? // Create a list to store the email messages
? ? List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
? ? // Loop through each new Case Feed post
? ? for (CaseFeed post : Trigger.new) {
? ? ? ? // Get the case associated with the post
? ? ? ? Case case = cases.get(post.ParentId);
? ? ? ? // Create a new email message
? ? ? ? Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
? ? ? ? // Set the recipients of the email to the case team members
? ? ? ? message.setToAddresses(
? ? ? ? ? ? teamMembers.get(case.Id).stream()
? ? ? ? ? ? ? ? .map(teamMember -> users.get(teamMember.UserId).Email)
? ? ? ? ? ? ? ? .collect(Collectors.toList())
? ? ? ? );
? ? ? ? // Set the subject of the email
? ? ? ? message.setSubject('New Case Feed post for case: ' + post.ParentId);
? ? ? ? // Set the body of the email
? ? ? ? message.setPlainTextBody(
? ? ? ? ? ? 'A new Case Feed post has been added to the following case:\n\n' +
? ? ? ? ? ? 'Case ID: ' + post.ParentId + '\n' +
? ? ? ? ? ? 'Post Body: ' + post.Body + '\n\n' +
? ? ? ? ? ? 'Please log in to Salesforce to view the case and the new post.'
? ? ? ? );
? ? ? ? // Add the email message to the list
? ? ? ? messages.add(message);
? ? }
? ? // Send the email messages
? ? Messaging.sendEmail(messages);
}
{
It did respect the best practice of not placing a query inside a loop, but, otherwise, it had almost the same flaws:
领英推荐
So I've made a third attempt with more details: "Please write a Salesforce Case Feed trigger in Apex that collects the parent case ids and uses it to retrieve the Case Team Member emails, then notifies each case team member about the Case Feed post. Avoid queries inside loops. Avoid stream() and collect() methods." resulted in:
Fourth try: "Please write a Salesforce Case Feed trigger in Apex that collects the ParentIds and use them to retrieve the Case Team Member emails from the CaseTeamMember object, then notifies each case team member about the Case Feed post. Avoid queries inside loops. Avoid stream() and collect() methods. Avoid nested queries."
It did figure out the part of querying the User object to collect the email addresses but the resulting code became more incomplete and looking worse than before. It included another query inside a loop. It was as if ChatGPT just started to give up! ????♂?
I will try a more "baby-steps approach" later.
Then, since ChatGPT seems to shine at fiction writing, I've asked: "Write a dialogue that Lamar Davis would say in GTA 5 about Salesforce flows."
Lamar: "Yo, have you seen those Salesforce flows? They're like a bunch of little automation scripts, all connected together in one big flowy thing. You can use them to automate all kinds of processes, like creating records, updating fields, and sending notifications. It's like a whole other level of hustling, right there in your Salesforce org."
That does resemble Lamar, maybe after getting some Trailhead badges.
Then I've tried another Seinfeld routine: "Write a Seinfeld routine about Salesforce flows."
Amusing! I would like to see what results others got!
Salesforce Architect | Salesforce Saturday Co-Lead | Salesforce Mentor | Licensed Pharmacist
1 年Hieu Nguyen I think you will find this interesting!