Convert Flow to Apex : Daryl Moon
by: Daryl Moon
blow post content copied from CertifyCRM
click here to view original post
**Summary: Can We Convert Flow to Apex Code?** A recent discussion on LinkedIn by Azlam Abdulsalam raised an interesting question: Can we convert Salesforce Flow into Apex code using AI tools like ChatGPT? The conversation explored whether flows are still necessary if AI can automate code generation. **Key Details:** - **Flow Example:** A simple record-triggered flow was used as a test case. This flow updates the Account's rating to "Hot" if the type is "Customer - Direct" and the industry is "Finance," provided the rating is not already set. - **Using ChatGPT:** The author used Salesforce Inspector Reloaded to generate an XML file of the flow and then instructed ChatGPT to convert this XML into Apex code. - **Apex Trigger:** ChatGPT successfully generated an Apex trigger that performs the same function as the flow, updating the Account's rating based on the specified conditions. - **Test Class:** A test class was also created to ensure the trigger works correctly, covering both positive and negative scenarios. - **Process Builder and Screen Flows:** The author also tested the conversion of Process Builder and Screen Flows into Apex and Lightning Web Components (LWC), with successful results. **Conclusion:** While AI tools like ChatGPT can assist in converting flows to Apex, they do not replace the need for understanding Apex development. This capability can enhance learning and improve performance for experienced developers. **Additional Context:** - **Salesforce Flows vs. Apex:** Flows are user-friendly and allow non-developers to automate processes, while Apex provides more control and performance for complex logic. - **Learning Opportunity:** This process can serve as a learning tool for those looking to transition from using flows to writing Apex code. **Hashtags for SEO:** #Salesforce #Apex #FlowToApex #ChatGPT #SalesforceDevelopment #Automation #AIinDevelopment #LightningWebComponents #ProcessBuilder #SalesforceLearning
(Can we really convert Flow to Apex code?)

Do you really need flows?
This question was asked on LinkedIn by Azlam Abdulsalam:
If an LLM model can write code and visualize it using mermaid, plantuml etc.
That got me thinking …
Do we really need Apex?
Could ChatGPT take a flow and convert it into Apex for me?
I can’t write Apex but I can write a good flow, can ChatGPT help me with this?
I asked Mohith Shrivastava, Principal Developer Advocate at Salesforce | AI Agents & Apps.
Well I don’t think he was calling me stupid so I thought it was worth a shot!

A Sample Flow to Test
I had a simple record-triggered flow that I could use to see if I can convert Flow to Apex.
It just sets the Account rating field if the type = Customer – Direct and the Industry is Finance (where rating is not already set).

The Start criteria is as follows:

The record is updated to Hot:

Enter ChatGPT
I just need to feed this flow into ChatGPT.
I know that one quick, easy way is to use Salesforce Inspector Reloaded to generate an XML file from my flow.
Click the little icon to open the inspector and then select Download Metadata:

Untick the Select All option and just choose Flow (1) and then click Download metadata (2).

When finished, click the See downloaded metadata link:

The results are contained in a zip file a couple of folders deep.
Metadata.zip -> unpackaged -> flows

Navigate down until you find the flow and copy the file.
Navigate back to a higher folder (eg downloads) and paste the copy there.
This will extract it from the zip file making a copy that you can easily access.

Now we need to instruct ChatGPT:
take this uploaded xml for a salesforce flow and convert it to apex
Got it! Please upload the XML file, and I’ll convert the Salesforce Flow into Apex for you.
?xml version="1.0" encoding="UTF-8"?>
<Flow xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>57.0</apiVersion>
<description>Account update rating</description>
<environments>Default</environments>
<interviewLabel>Account-UpdateRating {!$Flow.CurrentDateTime}</interviewLabel>
<label>Account-UpdateRating</label>
<processMetadataValues>
<name>BuilderType</name>
<value>
<stringValue>LightningFlowBuilder</stringValue>
</value>
</processMetadataValues>
<processMetadataValues>
<name>CanvasMode</name>
<value>
<stringValue>AUTO_LAYOUT_CANVAS</stringValue
</value>
</processMetadataValues>
<processMetadataValues>
<name>OriginBuilderType</name>
<value>
<stringValue>LightningFlowBuilder</stringValue>
</value>
</processMetadataValues>
<processType>AutoLaunchedFlow</processType>
<recordUpdates>
<description>Update rating on account when Type = Customer - Direct and Industry = Finance</description>
<name>Update_Account</name>
<label>Update Account</label>
<locationX>176</locationX>
<locationY>311</locationY>
<inputAssignments>
<field>Rating</field>
<value>
<stringValue>Hot</stringValue>
</value>
</inputAssignments>
<inputReference>$Record</inputReference>
</recordUpdates>
<start>
<locationX>50</locationX>
<locationY>0</locationY>
<connector>
<targetReference>Update_Account</targetReference>
</connector>
<doesRequireRecordChangedToMeetCriteria>true</doesRequireRecordChangedToMeetCriteria>
<filterLogic>and</filterLogic>
<filters>
<field>Rating</field>
<operator>IsNull</operator>
<value>
<booleanValue>true</booleanValue>
</value>
</filters>
<filters>
<field>Type</field>
<operator>EqualTo</operator>
<value>
<stringValue>Customer - Direct</stringValue>
</value>
</filters>
<filters>
<field>Industry</field>
<operator>EqualTo</operator>
<value>
<stringValue>Finance</stringValue
</value>
</filters>
<object>Account</object>
<recordTriggerType>CreateAndUpdate</recordTriggerType>
<triggerType>RecordBeforeSave</triggerType>
</start>
<status>Active</status>
</Flow>
Here’s an equivalent Apex trigger to replace your Salesforce Flow.
This trigger updates the Rating
field on Account
records before they are saved, based on the conditions specified in your Flow.
trigger AccountUpdateRating on Account (before insert, before update) {
for (Account acc : Trigger.new) {
// Check if Rating is null, Type is 'Customer - Direct', and Industry is 'Finance'
if (acc.Rating == null && acc.Type == 'Customer - Direct' && acc.Industry == 'Finance') {
acc.Rating = 'Hot';
}
}
}
Explanation:
- The trigger runs before insert and before update (equivalent to a before-save flow).
- It loops through the
Trigger.new
list (all records being processed). - It checks:
- If
Rating
isnull
- If
Type
is'Customer - Direct'
- If
Industry
is'Finance'
- If
- If all conditions are met, it updates
Rating
to'Hot'
.
can you create a test class for that trigger
Absolutely! Here’s a test class to ensure the trigger works correctly:
Test Class:
@isTest
public class UpdateAccountRatingTest {
@isTest
static void testUpdateAccountRating() {
// Create an Account that meets the trigger criteria
Account acc = new Account(
Name = 'Test Account',
Type = 'Customer - Direct',
Industry = 'Finance',
Rating = null // This should be updated by the trigger
);
// Insert the account - trigger should fire
insert ACC;
// Retrieve the inserted record
Account insertedAcc = [SELECT Id, Rating FROM Account WHERE Id = :acc.Id];
// Verify the Rating field was updated correctly
System.assertEquals('Hot', insertedAcc.Rating, 'Rating should be set to Hot');
// Now, test an update scenario
Account accToUpdate = new Account(
Id = insertedAcc.Id,
Type = 'Customer - Direct',
Industry = 'Finance',
Rating = null // Simulating Rating being reset to null
);
// Update the account
update accToUpdate;
// Retrieve the updated record
Account updatedAcc = [SELECT Id, Rating FROM Account WHERE Id = :accToUpdate.Id];
// Verify the Rating field was updated correctly again
System.assertEquals('Hot', updatedAcc.Rating, 'Rating should be set to Hot after update');
// Negative Test Case - Account not meeting criteria
Account accNegative = new Account(
Name = 'Test Account 2',
Type = 'Customer - Indirect', // Different Typ
Industry = 'Finance',
Rating = null
);
// Insert the account
insert accNegative;
// Retrieve the inserted record
Account insertedAccNegative = [SELECT Id, Rating FROM Account WHERE Id = :accNegative.Id];
// Verify the Rating field was NOT updated
System.assertEquals(null, insertedAccNegative.Rating, 'Rating should remain null for non-matching records');
}
}
What This Test Class Does:
Inserts an Account meeting the trigger conditions and verifies that
Rating = 'Hot'
.
Updates an Account with
Rating = null
and ensures the trigger fires again.
Tests a negative case where the account does not meet criteria (Type ≠ “Customer – Direct”).
This test ensures that your trigger works as expected and covers both positive and negative scenarios.
Let me know if you need modifications!
Create the Apex Trigger File
To create our Apex Trigger in our Salesforce org, follow these steps:
Open the Developer console:

Select New -> Apex Trigger:

Give the trigger a name:

A default trigger file is created:

Remove the highlighted text and paste in our new trigger as created by ChatGPT:

Save the new trigger

Create the Test Class
Now, create the test class file (needed to migrate this file to production and meet the test coverage requirements):

Give it a name:

Paste in the test class as generated by ChatGPT and then save the file:

Remember to save the file.
Now select Test and choose New Run to execute a test.

Select the Test Class (1) and then the test within it (2) and click Run.

Click the AccountUpdateRating.apxt tab and then the small arrow circled here to see the test coverage.

Before testing the trigger – deactivate your flow or you will have two automations running on the account and it will be confusing.
Now create a new Account or find one with an empty Rating. Set the Type to Customer – Direct and the Industry to Finance.
Save the records and the trigger should now run and set the Rating to Hot as shown below.

Winning!!
What about Process Builder?
Next, I told my buddy Tim Combridge about it – he asked would it work for a Process Builder.
Under the covers, a process builder is like a really simple flow with a similar internal structure so I was reasonably confident it may just work.
Luckily I had an old one hanging around and …..
YES it worked.
What about a Screen Flow?
Next, I thought what about a Screen Flow? Could it take a screen flow and make a Lightning Web Component from that?
I have a screen flow that uses a table to display Accounts with a Record Type = Retail.
so I told ChatGPT:
create a salesforce LWC from the flow code I will post next
I then generated the XML using the Salesforce Inspector Reloaded, pasted it in to ChatGPT and it successfully created all the parts of a lightning Web Component:
- An ApexController
- LWC Javascript
- LWC HTML
- LWC Metadata XML
- A Test Class
Wow, I am seriously impressed.
While I realize this does not make me an Apex Developer, it could help me learn how to create Apex triggers and classes from my existing flows. If I was an experienced Apex developer it may be a good tool to convert some flows over to Apex to gain better performance too.
The post Convert Flow to Apex appeared first on CertifyCRM.
March 19, 2025 at 05:57PM
Click here for more details...
=============================
The original post is available in CertifyCRM by Daryl Moon
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Post a Comment