How to Manage Program Supplies (Inventory) on Salesforce – Part 3: Automate with Flow

Last week, in Part 2 of this series, I detailed a data map for a robust inventory management app. A crucial part of automation in this app is the bulk creation of inventory records whenever supplies are added to inventory and the bulk update of those records when they are removed from inventory (expended/used). While programmatic solutions are surely possible, in this post I will show how to accomplish this automation using Flow. Indeed, we are encouraged to use declarative methods of development before resorting to writing custom apex.

Photo by Franck V. on Unsplash

The Big Picture

If you remember from Part 2, we have custom objects in our app: Purchase Order, Inventory, and Requisition. Purchase Orders represent the addition of items to Inventory (storage), and Requisitions represent the utilization of items from Inventory. The Inventory object is, therefore, the representation of all the individual items that have passed through our inventory system, whether they are sitting in storage or already used.

This means if we order 20 cases of paper towels from Vendor X, we need to create the corresponding number of Inventory records to represent each of those 20 cases (or each of those rolls of paper towels if we distribute them by the roll instead of by the case–see last week’s discussion of Distribution UOM vs. Purchase UOM).

Furthermore, when a staff person makes a request for supplies (a Requisition) and those supplies are given to them from Inventory, we need to grab the corresponding number of Inventory records and update them to a status of something like “Distributed”.

We’ll address these two business processes with two different Flows, triggered by two different Processes (in Process Builder). The Flows are reasonably simple and make use of a loop in which we’ll iterate over a variable that dictates how many Inventory records to create/update.

Inventory Creation Flow

Step 1 – Retrieve the PO Line Item

Our PO is composed of separate PO Line Items for the different items on our order/donation, and these PO Line Items is where we find information like Item Name and Quantity. Therefore we need to access those PO Line Items as we create Inventory records. For this reason, our Flow is kicked off by actions on the PO Line Items, not the overarching PO. In this setup, we will start the flow when a PO Line Item is marked as “Received,” and our first element will be a Get Records element, which obtains the PO Line Item that started the Flow/Process.

This is done by creating a variable in which to store a PO Line Item’s Id (labeled “POLineItemID” in the below screenshot from Process Builder). Then we’ll configure Process Builder to pass the triggering PO Line Item’s Id to this variable (“Set Flow Variables”).

The “Launch a Flow” action in Process Builder

Step 2 – Create the Loop

To prepare for our loop, we first need to create a variable in which to store the number of records we wish to create. We’ll use an assignment element to start the “LoopCounter” variable equal to the PO Line Item field, “Distribution Units Received.”

Next, create the loop which does the following:

  • Takes values from the triggering PO Line Item and assigns them to a Record Variable
  • Adds that record variable to a Collection Variable
  • Subtracts 1 from the Counter variable

Step 3 – Insert the Inventory Records

When the Counter variable reaches 0, effectively exiting our loop, we use a Create Records element to insert the Collection Variable (i.e., all the Inventory Records).


Inventory Update Flow

Step 1 – Get Records & Set the Counter

The Flow for changing existing Inventory records from “In-Stock” to “Distributed” when a Requisition Line Item is marked as “Delivered”/”Fulfilled” is quite similar to the creation flow. However, now we need to retrieve two types of records before we enter our loop:

  • The Requisition Line Item that started the flow, so our Flow knows which Inventory records to update
  • The Inventory records corresponding to the items being requested from Inventory

Since we are planning for “first-in, first-out” so that the current value of items sitting in storage is calculated based on the most recent purchases, it is vital that our Flow grab the oldest Inventory records. This way, the records that are being updated to “Distributed” are the ones that were “first-in.” We accomplish this by sorting the records entering our Get Records element by Created Date:

NOTE: Currently, the Flow pictured above grabs all the in-stock Inventory records for the Item listed on the Requisition Line Item, and then our loop only updates the appropriate number of Inventory records—based on the Quantity Distributed field on the Requisition Line Item). This may not be ideal and could unnecessarily use memory.

Step 2 – Enter the Loop

Our loop starts iterating over the collection variable that has our in-stock Inventory items, and we exit the loop when our counter variable reaches 0.

The first element in our loop is an Assignment element which subtracts 1 from the counter variable and assigns the Status of “Distributed” to the Inventory record currently in the loop. Then we add that record back into a collection representing the group of records we’ll update in the final step.

The Decision element which checks the value of the counter variable, sending the flow back into the loop if the counter is not yet equal to 0.

Step 3 – Exit the Loop & Update the Inventory Records

Now that we’ve iterated over the appropriate number of Inventory records for the Item in question, each time updating the record’s status to “Distributed,” we exit the loop and use an Update Records element to commit our updates to the database.


Wrapping Up

With our Inventory records now being updated automatically, we can keep an accurate accounting of item quantity and value. Since our Update Inventory Flow changes the status of the Inventory records from “In-Stock” to “Distributed,” it is easy to exclude those records from our in-stock totals.

For the sake of space, I’ve tried to select the most informative screenshots, but if anything is unclear, please let me know. I’m glad to expound upon any of the flow elements, and I would love to hear your ideas for making the solution to this business process even better.

Follow along in your inbox!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.