[SalesForce] How to use collections within a flow

I'm trying to use a combination of process builder and flow to accomplish the following. When an order is completed, create an invoice and invoice lines that are based off of the order and order lines. I can easily create the process builder to fire when the order is complete and start the flow that will create the invoice. I'm getting stuck on how to get the order lines and create the corresponding billing lines. Is this possible using flow? Any examples would be helpful.

Also, is it possible to reference custom settings in flow to pull in address info that is stored in a custom setting?

Thanks.

Best Answer

The entire flow is pretty cumbersome to layout (because it's a visual programming language), but here's a basic outline.

First, create your flow. Next, create a simple variable with a data type of Text, and a Input/Output Type of Input Only. You can call this orderId.

Next, create five SObject Variables, orderRecord, invoiceRecord, tempOrderLineItem, invoiceLineItem, and tempInvoiceLineItem, and two SObject Collection variables, orderLineRecords and invoiceLineRecords, all of the appropriate data types.

Third, create a Fast Lookup that looks up your order record, filtered by the Order Id = {!orderId}, and mark it as the start element. Create another Fast Lookup that loads orderLineRecords.

After that, you need use an Assignment to create all the values for your invoiceRecord to copy data from the orderRecord, and a Fast Create call to create the Invoice. After this, set up a loop that loops over the orderLineRecords using the variable tempOrderLineItem.

Create a new Assignment that sets invoiceLineItem to tempInvoiceLineItem, which creates a new blank item, then another Assignment that copies all the values you want from tempOrderLineItem and invoiceRecord to invoiceLineItem, and finally, an Assignment that adds invoiceLineItem to invoiceLineRecords.

For the exit branch of the loop, add another Fast Create for the invoiceLineRecords collection, and you're done with the flow.

For the final step, in the Process Builder, attach the Order Id to the flow's input orderId. This is how the Process Builder tells the Flow which order should be copied.

Here's what the flow should look like when you're done:

Sample Flow for Creating Records

For the second question: yes, you can query custom settings just like normal objects (e.g. create a variable and then perform a Lookup). You can also create Formulas that can reference the Hierarchy custom setting, just like a validation rule. List Custom Settings must be queried, however, as there is no formula syntax to access those values at this time.

Related Topic