0

I need to create automation that creates one grandchild record per child record, when criteria are met on the parent record.

The initial criteria are straightforward: two field values on the parent record.

On the grandchild record, many values should be hard-coded by this code but some should be filled at the time of insertion by child record data. The data they are filled with is different, there are four different criteria that change which fields are inserted with what.

This code should run once per parent record and never again, even if the parent's field values change to something other than the criteria, and then back again.

  • 1
    What have you tried so far? Where are you stuck? It's hard to give advice when you haven't even sketched a basic outline to your current approach. – Adrian Larson Jun 28 '18 at 16:31
  • @AdrianLarson Sorry, I'll be more specific: I don't understand at all... at all.... how achieve the above through code. I have attempted to self-study and can usually wrap my mind around a single element (like how to update a record with some hardcoded and some dynamic values, or how to get to the grandchild level) but I don't understand how to put it all together. I'm a baby in the woods and would sincerely appreciate any help. – CodeNotClicks Jun 28 '18 at 16:38
  • 2
    Have you worked your way through any of the relevant Trailhead modules? That would be the place to start building the baseline skills you need. – Adrian Larson Jun 28 '18 at 16:51
  • 3
    This sounds to me like it's achievable with Process Builder and Flow using a Flow iteration component. You're going to need to get your requirements really well defined, and probably add an "Already Processed" checkbox on your parent record, but it sounds very achievable without code if that's your preference. – David Reed Jun 28 '18 at 16:53
1

Here's a sketch of how you can achieve this using Process Builder and Flow alone - no code required at all. I'm proposing this based on your description of yourself as a beginner with code. This route may be more approachable, but please note this is only a sketch to help you get started attacking the problem, and you'll need some familiarity with Flow Designer in particular.

Process Builder

  1. Add a new Checkbox field to the parent object. Call it "Processed"; no need to add it to the page layout.
  2. Set up your Process Builder to fire on a data update into the starting state for this workflow, but also include the criterion that Processed = false. Set it to fire only on specific changes.
  3. Add two actions to your Process Builder:
    1. Record update, to set the Processed field to true on your parent account.
    2. Run a Flow, passing the Id of the parent record as a parameter. (Note that you'll actually need to build the Flow first to have it available in this step).

This way, your Process will execute exactly once on any parent object, at the point when it first enters the criteria you're looking for.

Flow

I cannot run Flow Designer on the machine I'm currently on, so please forgive me if my description is a bit rough.

Your Flow will have an input variable for the parent record Id, which is your parameter from Process Builder.

You'll start with a Fast Lookup element, which should look for child records based upon the value of their lookup field to the parent being equal to the Id you get in as your parameter. It'll store the records it finds in an sObject collection variable. Make sure to ask for the fields you need to determine how to create grandchild records.

Then you'll go into a Loop element, to loop over the records returned in that collection. The loop's Next Element connection will go to an Assignment element, where you'll create a record of your grandchild object, based on the data in the child records, and assign it to a new sObject variable. Make sure to populate its lookup to the child record with the Id of the child record you're currently processing. You'll use another Assignment element with the add operator to add this record to your sObject collection variable of grandchild records.

Note that this sequence of operations could involve building Flow formulas to determine the right data points, or even decision elements. Depending on your logic, it might get complex, but the overall pathway is to build these records within the loop and accumulate them in an sObject collection variable.

Finally, upon exiting the loop (the End of Loop connection), you'll perform a Fast Create on the sObject collection variable holding your new grandchild records. Then you're done!

Resources

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.