{"id":678,"date":"2022-12-06T22:19:00","date_gmt":"2022-12-07T04:19:00","guid":{"rendered":"https:\/\/blog.felineflock.com\/?p=678"},"modified":"2024-01-03T22:29:48","modified_gmt":"2024-01-04T04:29:48","slug":"chatgpts-blend-of-tech-and-humor-a-new-approach-to-salesforce-triggers","status":"publish","type":"post","link":"https:\/\/blog.felineflock.com\/index.php\/2022\/12\/06\/chatgpts-blend-of-tech-and-humor-a-new-approach-to-salesforce-triggers\/","title":{"rendered":"ChatGPT&#8217;s Blend of Tech and Humor: A New Approach to Salesforce Triggers"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-41-1024x512.png\" alt=\"\" class=\"wp-image-679\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-41-1024x512.png 1024w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-41-300x150.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-41-768x384.png 768w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-41.png 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p id=\"ember3415\">ChatGPT is a prototype artificial intelligence chatbot designed by OpenAI to understand natural human language and generate written text that closely resembles human writing.<\/p>\n\n\n\n<p id=\"ember3416\">I&#8217;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&#8217;ve asked &#8220;<strong>Write a Seinfeld routine about Salesforce triggers.<\/strong>&#8220;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"771\" height=\"1024\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-42-771x1024.png\" alt=\"\" class=\"wp-image-680\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-42-771x1024.png 771w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-42-226x300.png 226w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-42-768x1020.png 768w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-42.png 1129w\" sizes=\"auto, (max-width: 771px) 100vw, 771px\" \/><\/figure>\n\n\n\n<p id=\"ember3418\">Then I&#8217;ve asked ChatGPT: &#8220;<strong>Please write a Salesforce trigger that notifies case team members of any new Case Feed post.<\/strong>&#8220;<\/p>\n\n\n\n<p id=\"ember3419\">Below was the response followed by ChatGPT&#8217;s very nice explanation: &#8220;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 <strong>Messaging.sendEmail<\/strong> method.&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"897\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-43-1024x897.png\" alt=\"\" class=\"wp-image-681\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-43-1024x897.png 1024w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-43-300x263.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-43-768x673.png 768w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-43.png 1392w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"883\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-44-1024x883.png\" alt=\"\" class=\"wp-image-682\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-44-1024x883.png 1024w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-44-300x259.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-44-768x663.png 768w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-44.png 1428w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"824\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-45-1024x824.png\" alt=\"\" class=\"wp-image-683\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-45-1024x824.png 1024w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-45-300x241.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-45-768x618.png 768w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-45.png 1410w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p id=\"ember3420\">As you can see, the code is well-formatted and commented out, however it will NOT work properly:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>the trigger is missing the opening {, which ended up in the bottom of the code<\/li>\n\n\n\n<li>it runs a query on CaseTeamMember inside the first loop<\/li>\n\n\n\n<li>it uses &#8220;stream() .map(user -&gt; user.Email) .collect(Collectors.toList())&#8221; which is not Apex and uses what seems like an undeclared variable &#8220;Collectors&#8221;.<\/li>\n<\/ol>\n\n\n\n<p id=\"ember3422\">Then I&#8217;ve tried again giving further details: &#8220;Please write a Salesforce trigger in Apex that notifies case team members of any new Case Feed post and avoid queries inside loops.&#8221;<\/p>\n\n\n\n<p id=\"ember3423\">The response: <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"924\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-46-1024x924.png\" alt=\"\" class=\"wp-image-684\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-46-1024x924.png 1024w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-46-300x271.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-46-768x693.png 768w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-46.png 1438w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"867\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-47-1024x867.png\" alt=\"\" class=\"wp-image-685\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-47-1024x867.png 1024w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-47-300x254.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-47-768x650.png 768w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-47.png 1422w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"943\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-48-1024x943.png\" alt=\"\" class=\"wp-image-686\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-48-1024x943.png 1024w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-48-300x276.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-48-768x708.png 768w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-48.png 1424w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"296\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-49-1024x296.png\" alt=\"\" class=\"wp-image-687\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-49-1024x296.png 1024w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-49-300x87.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-49-768x222.png 768w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-49.png 1434w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p id=\"ember3424\">It did respect the best practice of not placing a query inside a loop, but, otherwise, it had almost the same flaws:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>the trigger is missing the opening {, which ended up in the bottom of the code<\/li>\n\n\n\n<li>instead of collecting the case ids (parentId) it collected the post ids and added an unnecessary query on Case\/CaseFeed<\/li>\n\n\n\n<li>again it used &#8220;stream() &#8230; .collect()&#8221; (non-Apex).<\/li>\n<\/ol>\n\n\n\n<p id=\"ember3426\">So I&#8217;ve made a third attempt with more details: &#8220;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.&#8221; resulted in:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>it still collected the post ids instead of parent ids<\/li>\n\n\n\n<li>unnecessary nested queries<\/li>\n\n\n\n<li>missed some query optimizations<\/li>\n<\/ol>\n\n\n\n<p id=\"ember3428\">Fourth try: &#8220;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.&#8221;<\/p>\n\n\n\n<p id=\"ember3429\">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! \ud83e\udd37\ud83c\udffd\u2642\ufe0f<\/p>\n\n\n\n<p id=\"ember3430\">I will try a more &#8220;baby-steps approach&#8221; later.<\/p>\n\n\n\n<p id=\"ember3431\">Then, since ChatGPT seems to shine at fiction writing, I&#8217;ve asked: &#8220;<strong>Write a dialogue that Lamar Davis would say in GTA 5 about Salesforce flows<\/strong>.&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-50.png\" alt=\"\" class=\"wp-image-688\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-50.png 960w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-50-300x169.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-50-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p id=\"ember3433\">Lamar: &#8220;Yo, have you seen those Salesforce flows? They&#8217;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&#8217;s like a whole other level of hustling, right there in your Salesforce org.&#8221;<\/p>\n\n\n\n<p id=\"ember3434\">That does resemble Lamar, maybe after getting some Trailhead badges.<\/p>\n\n\n\n<p id=\"ember3435\">Then I&#8217;ve tried another Seinfeld routine: &#8220;<strong>Write a Seinfeld routine about Salesforce flows<\/strong>.&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1009\" height=\"1000\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-51.png\" alt=\"\" class=\"wp-image-689\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-51.png 1009w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-51-300x297.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-51-150x150.png 150w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-51-768x761.png 768w\" sizes=\"auto, (max-width: 1009px) 100vw, 1009px\" \/><\/figure>\n\n\n\n<p id=\"ember3437\">Amusing! I would like to see what results others got!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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&#8217;ve asked &#8220;Write a Seinfeld [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-678","post","type-post","status-publish","format-standard","hentry","category-blog"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/comments?post=678"}],"version-history":[{"count":1,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/678\/revisions"}],"predecessor-version":[{"id":690,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/678\/revisions\/690"}],"wp:attachment":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/media?parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/categories?post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/tags?post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}