Using Temporary IDs For Linking Records Together at Time of Creation

21 Oct
Using Temporary IDs For Linking Records Together at Time of Creation

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.

Temp ID Flow

Temp ID Flow

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.

TempID Flow

TempID Flow

How to Use Temporary IDs to Link Related Records

There are seven basic steps

  1. Setup program use fields on the objects
  2. Construct the parent and child records being sure to save the same TempID in related records
  3. Create the parent record collection
  4. 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
  5. Create the child record collection
  6. Clean up the parent record collection by removing the TempID values
  7. 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.

Program Use Field

Program Use Field

Setting up the Record Collections

Now to construct the parent and child collections:

Constructing Collections

Constructing 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.

  1. Before the ME loop, set a comparison variable to an empty string.
  2. For each record in the ME collection, check the field you want to compare to your comparison variable.
    1. If the field value is the same as the comparison variable, skip past getting a new temp ID and setting up an FTP record
    2. 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
  3. Do additional things to the FF records as needed
  4. Add the FF record to an “interim” collection
  5. 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.

Using TempIDs to Link Records

Using TempIDs to Link Records

There are eight basic steps to follow:

  1. First the FTP collection (the parent collection) is created. Once created the Id’s for these records are available.
  2. 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.
  3. Once the matching TempID is found, save the FTP Id to the FF record.
  4. The ProgramUse field can now be cleared (set to nothing)
  5. Then the FF record is then added to a create collection.
  6. At the end of the loop create the FF record collection with a Fast Create element.
  7. Finally, loop through the FTP records to clear the ProgramUse field and place the records into an update collection as you go.
  8. 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!

Leave a comment

Posted by on October 21, 2015 in Visual Flow


Tags: , ,

Leave a Reply

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

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

Google+ photo

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

Twitter picture

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

Facebook photo

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


Connecting to %s

%d bloggers like this: