Have you ever needed to link records for different objects together as they are being created in a Visual Flow?
For example, let’s say you’re creating new FTP “parent” records and for each of those FTP records you’re creating one or more new FF “child” records on a different object. After creating the FTP collection you now want to loop through the FF collection and link them to the FTP records by putting the FTP Id’s into their field that looks up to that object.
I’ve needed to do this several times, so just in case someone else needs this solution, here is what I have done.
Random Number Generator
First you’ll need a random number generator. As of this writing (October, 2015) Salesforce didn’t have a random number generator, so you’re going to have to construct one. Here is a link to my previous post on how I made a pseudo random number generator. There may be other solutions available, but none that I was able to find (yet!).
The CreateTempID Flow
Next you’ll need to construct a Create Temporary ID Flow. See my prior post for the details on this.
How to Use Temporary IDs to Link Related Records
There are seven basic steps
- Setup program use fields on the objects
- Construct the parent and child records being sure to save the same TempID in related records
- Create the parent record collection
- Go through the child record collection and look for a matching TempID in the parent collection. Save the parent record Id in the child record and remove the TempID
- Create the child record collection
- Clean up the parent record collection by removing the TempID values
- Update the parent record collection
Setting Up Program Use Fields on the Objects
Here is a trick for using this technique: Create a field on each object for storing your TempID. (See note at end for a possible workaround to this step.)
This is an example of a field on one of the objects that I use for saving the TempID. As you can infer from the name, I use this field in programs (Flows) only.
Setting up the Record Collections
Now to construct the parent and child collections:
In this simplified example I’m looping through an object I’ve named “ME”. For each ME record an FF record will be created, and there will also be FTP records created, but there will be fewer of them because sometimes multiple FF records will be related to a single FTP record.
- Before the ME loop, set a comparison variable to an empty string.
- For each record in the ME collection, check the field you want to compare to your comparison variable.
- If the field value is the same as the comparison variable, skip past getting a new temp ID and setting up an FTP record
- If the field value is NOT the same as the comparison variable, set the comparison variable to the field value, get a new temp ID, and setup a new FTP record
- Do additional things to the FF records as needed
- Add the FF record to an “interim” collection
- If a new FTP record was created, add it to a FTP create collection
As mentioned, this is a simplified example and you may very well find that you’ll have additional steps in this process. My recommendation is to start with the “bones” of it, like in this example, and test the logic to make sure you’re creating the number of records you expect to create, and that they’re liking together as they should. After the logic is sound add in any additional elements you need to fully fill the fields on your objects.
Creating your Records
Once your collections are all ready to go you’ll be ready for the record creation process.
There are eight basic steps to follow:
- First the FTP collection (the parent collection) is created. Once created the Id’s for these records are available.
- Now the FF interim collection (the child collection) is looped, and for each record in this collection, loop through the FTP collection looking for the matching TempID.
- Once the matching TempID is found, save the FTP Id to the FF record.
- The ProgramUse field can now be cleared (set to nothing)
- Then the FF record is then added to a create collection.
- At the end of the loop create the FF record collection with a Fast Create element.
- Finally, loop through the FTP records to clear the ProgramUse field and place the records into an update collection as you go.
- At the end of the loop save the updated FTP records with a Fast Update element.
As you can see, there are several loops in this process, but if you don’t have a lot of records to deal with you’ll be able to stay within governor limits.
Using Regular Fields in Lieu of Program-Use-Only Fields
Just as an aside, I’ve done this same technique using regular fields instead of special program-use-only fields and then later assigned a real value to those fields. You’ve got to be careful, but it’s totally doable in some cases.
For instance, let’s say I have a field on the FTP record that isn’t going to be used for anything at the moment, or that I can assign a value to later. I put the TempID value into that field, then after both collections are created and I’m cleaning up the FTP records I replace the TempID values with the values that should be in that field (or just remove the TempID value altogether).
And on the FF records a field such as RecordTypeId can be used since once the FTP Id has been identified for that record the TempID is no longer needed and the RecordTypeId can take its place.
It’s the exact same process as above, but instead of just removing the TempID value you’re also adding back in a value that will be available on the record.
Well, anyway, ‘hope this is useful.
‘Until next time!