Using Process Builder to Generate Parent-Child Records in Salesforce

The use case is familiar to any seasoned Salesforce consultant: your client needs their records to match their business workflow, say, by transforming closed Opportunities with Products into Orders with Products. Or, perhaps you’ve built a custom “Project” module for a client who sells professional services and needs to track their delivery milestones after launch. Generate Parent-Child Records in Salesforce

Conventional Salesforce wisdom (and repeated scouring of the Success Forums) would lead you to believe that generating parent and child object records at the same time is best handled with APEX Triggers or Flows — and though that is technically true — the advantages of configuration with Process Builder are well known, especially for clients without much budget for custom development. Here’s how it’s done:

Configuration Steps

1. Opportunity Object

  • Create a lookup relationship field for the Order object, called something like Related Order, and put it in a visible spot on the page layout. This will make it easy to navigate to the Order you generate right from the Opportunity record.
  • Optional: Create a checkbox field on the Opportunity called Generate Order. Then, create a Lightning Action of type “Update Record” with a predefined, read-only field value that meets the condition you’ll set in Process Builder #1. This step is not necessary if, for instance, simply changing an Opportunity to a “closed-won” Stage should trigger the order creation.

Process Builder2. Order Object

  • There is already a lookup field for an Opportunity on the standard Orders object, but in my experience, the default visibility is turned off, so you’ll want to make sure your users have sufficient access to view this.
  • Optional: I like to relabel this standard field to Source Opportunity to make it clear when Orders are created from an Opportunity (to contrast with Orders that, say, sync from an e-Commerce platform and may not have had a pre-existing sales opportunity).

3. Opportunity Product Object

  • Create a checkbox field called something like Create Order Product. This doesn’t necessarily need to be on the page layout, but might be helpful to see while you’re testing your work.
  • Create a lookup relationship to the Order object, called something like Generated Order. This doesn’t necessarily need to be on the page layout, but might be helpful to see while you’re testing your work.

Process #1: Generate Order from Opportunity

Starting Object: Opportunity (when a record is created or edited)

Condition: This is up to your discretion. Some folks may want to simply generate orders when an Opportunity is set to a “Closed Won” stage; other clients may require some post-sale actions prior to producing orders for fulfillment or synchronization to finance systems. My preferred solution is to create a checkbox field on the Opportunity called “Generate Order” so that we can create a simple Update Record Lightning action on the page layout to launch this process. Regardless of your condition, make sure to check the setting to “execute the actions only when specified changes are made” — otherwise you’ll end up generating new orders every time you edit the Opportunity!**

**Admins want to know – why oh why isn’t this “advanced” setting defaulted to true??

Actions: 

    1. Generate Order: make sure to include all required fields in your mapping. Here’s a sample of a basic mapping of the fields.
      Generate Order: Set Fields Value
    2. Flag Opportunity Products – set all the Opportunity’s related Opportunity Product Generate Order checkbox fields to “TRUE”

Process #2: Pass Order ID back to Opportunity Products

Starting Object: Order (when a record is created or edited)

Condition: Source Opportunity field is not blank (Does not equal / Global Constant / $GlobalConstant.Null). This ensures that any Orders created without a pre-existing Opportunity do not accidentally trigger this process and cause it to fail.

Action: Pass Order ID to Opportunity Products – this ensures that your Opportunity Products have a direct relationship to the Order for which they will pass along their data as new Order Product records.

Pass Order ID back to Opportunity Products

Process #3: Generate Order Products from Opportunity Products

Starting Object: Opportunity Product (when a record is created or edited; also set the Recursion Flag to true!)

Condition: Generated Order field is not blank (Does not equal / Global Constant / $GlobalConstant.Null). This is the field that should have been populated by the action of Process #2.

Action: Create Order Products: make sure to include all required fields in your mapping. Here’s a sample of a basic mapping of the fields.

Process Builder

Test Your Work

  1. Setup a test Opportunity with Products attached (or the parent/child object combination you built in the steps above)
  2. Use your Generate Order record update action, or the field trigger you specified to launch Process #1
  3. Check your Generated Order lookup field – if the Order was created successfully, it should have also set off Process #2, populating this field.
  4. Check out your new order record: it should have been created along with related Products matching the Opportunity products.

That’s it. You’re done! Take a look at the video below (in fullscreen for legibility) to see how this works!

 

Ready to learn more? Let’s chat!

Let's Get Started