web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Add XLM element in For...
Power Automate
Answered

Add XLM element in For Each Loop

(0) ShareShare
ReportReport
Posted on 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

 

 

 

 

I have the same question (0)
  • dinnuk Profile Picture
    10 on at

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

  • VJR Profile Picture
    7,635 on at

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

  • dinnuk Profile Picture
    10 on at

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

  • Verified answer
    VJR Profile Picture
    7,635 on at

    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 at

    Wow. That worked flawless. Thank you!

  • dinnuk Profile Picture
    10 on at

    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

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 503 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 321 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard