Notes
Apex
The createNAccounts
method in the AccountUtility
class creates and inserts a specified number (n
) of new Salesforce Account records. Each account has a unique name in the format "Bank of America 1", "Bank of America 2", ..., "Bank of America n".
public class AccountUtility {
public static void createNAccounts(Integer n){
List<Account> acList = new List<Account>();
for(Integer i=1; i<=n; i++){
acList.add(new Account(Name='Bank of America '+i));
}
insert acList;
}
}
Execute the method to create 20 Account.
AccountUtility.createNAccounts(20);
Flow Builder
Sub Flow: A subflow is a modular and reusable component within a larger business process or workflow. It allows for the encapsulation of specific tasks, promoting modularity, reusability, and organization in process design. Subflows can be hierarchically structured, accept parameters, and enhance the overall efficiency and maintainability of complex workflows.
Let's design a Subflow to generate a specified number of Account records.
We require the following variables.
ac:
acList:
NumberOfAccounts: This will serve as the input and should be assigned when calling or running the subflow. It must be provided as an input.
Counter: This variable will assist in tracking the remaining number of accounts that can be created. Its default value will be set equal to the NumberOfAccounts variable. The Counter value will decrease by one after each account is created. When the Counter reaches 0, the flow will proceed to insert all the accounts into the database.
index: This number variable will be utilized to differentiate the created accounts from each other, such as "Bank of America 1," "Bank of America 2," and so on. The initial value of the index will be set to 1, and it will increase after each account is created.
Decision: First, it is necessary to check if the NumberofAccounts
variable is greater than 0. If the value of NumberofAccounts
is indeed greater than 0, the flow will assign values to the Account
variable (ac
), which we created as an Account Record variable.
Assignment: At the beginning of this flow, we created an Account record variable named AC
. Now, it's time to assign values to Account fields such as name, rating, etc.
This step assigns values to our first Account. If the value of NumberofAccounts
is provided as 20, then we need to create 20 accounts. The counter will decrease by 1, and the index will be 2. The value of the index will help us modify the name of the Account as "Bank of America 1."
The next step is to check if the counter is still greater than zero. If the counter is more than zero, it indicates that we need to create more accounts.
Decision: At this point, it's essential to check if the counter is zero or not. If it's not zero, we will connect our flow to the assignment for another creation of an account. If it is zero, the flow will follow the default path and insert all accounts (acList
) into the database.
If the counter is greater than 0, we connect to the Assignment (Go back) part of the flow to create another account. This process is repeated until the counter becomes zero. When the value of the counter is zero, we insert all the accounts into the Salesforce database.
Create Record: We insert all the records in the acList
collection into the database only if the counter is 0.
You cane now see The final view of the subflow.
Execute the Subflow to generate 5 accounts.
Result:
Screen Flow:Design a screen flow to invoke and execute the subflow with the necessary input parameters.
Integrate an input field into the screen to specify the desired number of accounts to be created.
Invoke the subflow and input the value from the screen into the subflow, assigning it to the variable NumberOfAccounts
.
Execute the Screen Flow to test whether it successfully creates accounts based on the provided input.
Note: You can add a success screen immediately after invoking the Subflow.