Skip to main content
Community site session details

Community site session details

Session Id : /l2tGhUU/QcrmW43cphu9e
Power Automate - Power Automate Desktop
Answered

Add XLM element in For Each Loop

Like (0) ShareShare
ReportReport
Posted on 28 Jul 2022 00:32:46 by 10

Hi!

 

I am building a flow which should add an XML element for each record.

 

Something like:

 

<ERPSystem>

<productionorders>

<productionorder = "0001">

<Reference number> test </Reference number>

</productionorder>

<productionorder = "0002">

<Reference number> test </Reference number>

</productionorder>

<productionorder = "0003">

<Reference number> test </Reference number>

</productionorder>

<productionorder = "0004">

<Reference number> test </Reference number>

</productionorder>

<productionorder = "0005">

<Reference number> test </Reference number>

</productionorder>

</productionorders>

</ERPSystem>

 

 

The XML-element Reference Number is non-existing in the XML i start with, and i would like to add this element to every productionorder (to fill it with an value)

 

However, i am struggeling to formulate the Xpath in the loop..

 

dinnuk_0-1658968288283.png

 

 

 

 

  • dinnuk Profile Picture
    10 on 28 Jul 2022 at 09:43:32
    Re: Add XLM element in For Each Loop

    Hi @VJR

     

    One more question. It seems that the software where I import the XML expects a specific location for the Element.

    Could I instruct Power Automate to place the new element on the second position within the Productionorder?

    And not at the bottom?

     

    Tnx

  • dinnuk Profile Picture
    10 on 28 Jul 2022 at 07:34:59
    Re: Add XLM element in For Each Loop

    Wow. That worked flawless. Thank you!

  • Verified answer
    VJR Profile Picture
    7,635 on 28 Jul 2022 at 06:09:36
    Re: Add XLM element in For Each Loop

    That's a good challenge.

    This is what you need.

     

    VJR_0-1658988295024.png

     

     

    The PAD script which you can paste in a new Flow editor and test the results

    XML.ReadFromFile File: $'''C:\\Test\\Add XML element\\Books.xml''' Encoding: XML.FileEncoding.DefaultEncoding XmlDocument=> XmlDocument
    SET NodeCounter TO 1
    LOOP WHILE (NodeCounter) <= (10)
     XML.InsertElement Document: XmlDocument XPathQuery: $'''ERPSystem/productionorders/productionorder[%NodeCounter%]''' Element: $'''<NewElement>Value Here%NodeCounter%</NewElement>'''
     ON ERROR
     GOTO LabelToContinue
     END
     Variables.IncreaseVariable Value: NodeCounter IncrementValue: 1
    END
    LABEL LabelToContinue
    # COMMENT: do the rest of the saving actions here.
    

     

    Notes:

    - Even better error handling can be done by pin pointing to the exact error that line no. 4 throws.

    - Make sure to change the xml path

    - After line no. 7 do the usual thing as explained in the video

    - I have given dummy values. Add your own values for the new elements.

     

     

    Output after running the process

    VJR_1-1658988503839.png

     

  • dinnuk Profile Picture
    10 on 28 Jul 2022 at 05:30:32
    Re: Add XLM element in For Each Loop

    Hi!

     

    Thank for the suggestion, indeed that video helped me along the way at first. The main problem is however the loop part.

     

    If I place the add element outside the loop, then only the first production order gets the new element, I I place it within a loop, then the first productionorder gets ALL the new elements (so if the loop is for 60 productionorders, the first production order gets 60 similar elements ...)

  • VJR Profile Picture
    7,635 on 28 Jul 2022 at 04:39:41
    Re: Add XLM element in For Each Loop

    This video will help you achieve the task of inserting the new element.

  • dinnuk Profile Picture
    10 on 28 Jul 2022 at 00:34:02
    Re: Add XLM element in For Each Loop

    I i try it like on the screenshot above, the XML elements are added, but are all added to the first productionorder, hehe

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

Announcing our 2025 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for…

Paul Stork – Community Spotlight

We are honored to recognize Paul Stork as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Loading complete