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..
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
Wow. That worked flawless. Thank you!
That's a good challenge.
This is what you need.
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
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 ...)
This video will help you achieve the task of inserting the new element.
I i try it like on the screenshot above, the XML elements are added, but are all added to the first productionorder, hehe