Creating child records using Visual Workflow
This will be a quick and dirty overview of using Fast lookups, loops, assignments, and Fast Create elements to iterate through a collection of records and create N number of child records.
Its awesome. Caveat - I brute forced my way through this due to a recent need and cannot say whether or not any of this is “correct”, but it works. There are likely governor limits and what-not that may pop up in production scenarios, and I will try to hit on those where I can.
So the sample use case I will model is as follows - take all of the contacts in an account and add them to a campaign. This requires the following steps
Fast lookup - lookup all contacts in the account and assign them to an Sobject collection of contacts
Loop through the Sobject collection of contacts, and for each record, do the following
-create a Campaign Member Sobject variable record using the contact ID, and a fixed value for campaign ID and status
-assign that Sobject variable record to an Sobject collection of campaign member recordsRun a fast create on the Campaign member record Sobject collection.
I think of Sobject variables as virtual jellybeans that represent records, and Sobject collections as virtual jars in which you can place your virtual beans. Each flavor of jellybean is different - you can have a contact jellybean, an account jellybean, etc. And you can only store a contact jellybean in a contact jellybean jar, etc. Enough jellybeans…still working on that metaphor.
Here is an overview screenshot of the flow. The initial “testing - assignment” element just lets me hard-code an account ID for testing purposes.
The fast lookup is filtered for contacts where Account Id = vaAccountId (a variable I defined) and adds the contacts to an Sobject Collection Variable ( ie contact jellybean jar). Make sure to save the contact ID in the variable, as you need that to create the campaign member record later.
The loop takes each of these contacts, and creates a campaign member sobject variable, and then assigns that SOV to a campaign member Sobject collection (a jar of sobject variables). Note that the loop variable is a contact type Sobject variable - it is not a campaign member. The loop variable is the “Active” contact that is being processed in the loop, and you can access any of the fields that you mapped in your fast lookup (Step 1).
For the first assignment element, I created a new sobject variable, of a campaign member type. Then I assign values to this campaign member variable’s fields (Contact ID, Member Status, and Campaign ID). Contact ID is mapped from ID field of the loop variable, while status and campaign ID are hard-coded for this example.
Then in the second assignment, I add the campaign member sobject variable from the first assignment to a Sobject collection of campaign members. This is what I then feed into the fast create, and voila
One tricky bit is finding the fields you want to set in the first (campaign member) assignment variable. Once you have created the sobject variable, you either just type in the name (ie id) and you should see the options, or you can click the triangle and you will see all the fields you can select.