Skip to main content

Notifications

Power Automate - Building Flows
Suggested answer

Add multiple related records with numbers

Posted on 20 Nov 2024 21:40:50 by 1,135
Good Morning,
 
Have a model driven app with a Parent record that has a whole number column called number of items.
 
I would like to create a flow that automatically creates the number of related child records based off the number above.
 
In the child records there is a position column and I would like these to be automatically filled starting at the number 1.
 
so for example
 
Parent records gets created and the number of items column says 4
 
Flow should then create 4 related child records like so
 
1st Record - Position = 1
2nd Record - Position = 2
3rd Record - Position = 3
4th Record - Position = 4
 
There are other fields in the child records but I can sort them out. Its just knowing and creating the number of records as well as the "auto number" thing.
 
Thanks in advance
 
Todd
  • Tango Profile Picture
    Tango 1,135 on 21 Nov 2024 at 22:45:47
    Add multiple related records with numbers
    Thank you @Gopala_Krishna for your detailed response.
     
    For the position, how do you get the current value of the loop?
     
    Cheers Todd
  • Suggested answer
    Gopala_Krishna Profile Picture
    Gopala_Krishna 1,423 on 21 Nov 2024 at 03:24:02
    Add multiple related records with numbers

    To implement this solution using Power Automate for a model-driven app with a parent-child relationship, here's a step-by-step approach:

    1. Trigger the Flow

    You want the flow to trigger whenever a new parent record is created or updated. You can use the Common Data Service (Dataverse) connector to trigger this event.

    • Trigger: Use the When a row is added, modified or deleted trigger with the "Add" event for the Parent record entity.
      • Choose the Parent table (the entity where Number of Items exists).
      • Specify the trigger to activate when a new parent record is created.

    2. Get the Number of Items value from the Parent record

    Once the trigger runs, you'll need to get the value from the Number of Items field of the parent record.

    • Action: Use the Get row action (Dataverse) to fetch the Parent record and retrieve the Number of Items field.

    3. Create Child Records Based on Number of Items

    Now that you have the Number of Items, you'll create a loop to generate the required child records.

    • Action: Use the Initialize variable action to create a variable of type Integer that holds the count from the parent record (this is the Number of Items field).

    • Action: Use the Apply to each action to loop through the child records you want to create. You can use an array for looping, such as a range from 1 to the number of items.

      You can create a list of numbers (1, 2, 3, 4, etc.) using the Expression to create an array based on the Number of Items field.

      Here’s an expression you can use in the Apply to each loop:

    • range(1, <Number of Items>)
       
    • Where <Number of Items> is the value retrieved from the Parent record.
    • For each iteration of the loop, you will create a child record.

    • 4. Create Child Record and Set Position Field

      Inside the loop, use the Add a new row action to create the child record in the related child table.

    • For the Position field, set it to the current value of the loop (which will be the iteration number, i.e., 1, 2, 3, etc.).
    • You can set the other fields of the child record as needed (you mentioned you will handle those).
    • Full Flow Outline:

    • Trigger: When a row is created (Parent record in your model-driven app).
    • Action: Get the Number of Items value from the Parent record.
    • Action: Initialize a variable to hold the number of items.
    • Action: Use Apply to each to loop from 1 to Number of Items.
    • Action inside loop: Create a new child record and set the Position field to the loop index.
     
    Note: Be sure to handle cases where the Number of Items might be 0 or empty, as you don't want to create any child records in that scenario.
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

October 2024 Newsletter…

October 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #4 How to Conntact Support…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,061

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,795

Leaderboard