This topic contains an example showing how you can use an XMLport to import data from an XML document into the Microsoft Dynamics NAV database.
Example
In this example, you design an XMLport to import data from an XML sales order document, XML Sales Order.xml, to the Microsoft Dynamics NAV database. The following code example shows the sales order.
Copy Code | |
---|---|
<?xml version="1.0" ?> <HeaderLines> <SalesOrder> <Header Date="17-02-04" Type="Quote"> <SellTo CountryRegion="GB"> <Name>The Cannon Group PLC</Name> <Address>192 Market Square</Address> <City>Birmingham</City> <Zip>B27 4KT</Zip> </SellTo> <BillTo CountryRegion="GB"> <Name>The Cannon Group PLC</Name> <Address>192 Market Square</Address> <City>Birmingham</City> <Zip>B27 4KT</Zip> </BillTo> <Lines> <Item PartNum="LS-150"> <ProductName>Loudspeaker, Cherry, 150W</ProductName> <Quantity>8</Quantity> <UnitPrice>129,00</UnitPrice> <ShipmentDate /> <Comment>Confirm the voltage is 75W</Comment> </Item> <Item PartNum="LS-MAN-10"> <ProductName>Manual for Loudspeakers</ProductName> <Quantity>20</Quantity> <UnitPrice /> <ShipmentDate /> <Comment /> </Item> <Item PartNum="LS-2"> <ProductName>Cables for Loudspeakers</ProductName> <Quantity>10</Quantity> <UnitPrice>21,00</UnitPrice> <ShipmentDate /> <Comment /> </Item> </Lines> <Contact>Mr. Andy Toal</Contact> <Terms>14 days</Terms> </Header> </SalesOrder> </HeaderLines> |
After analyzing the XML document, you can see that the data belongs in two database tables: XML Header - Import and XML Items - Import. You need to design an XMLport that can insert the data into these tables.
The design of the XMLport shows that the data in the XML
document has been mapped to database tables which have a
header-line relation. The XMLport must insert the header
information before inserting the line information. You need to set
the LinkedTableForcedInsert
property to
Yes
for the <Item>
node to ensure
that this happens. The LinkTable
and
LinkFields
properties for this node also need to be
set to indicate the relationship between the XML Header -
Import and XML Items - Import tables. It is important to
specify this binding information to ensure that the XMLport enters
the data into the correct tables. Note that you have given the
XML Items - Import table the variable name L
and that the XML Header - Import table has the variable name
H
. The following table shows the properties for the
<Item>
node.
Property | Value |
---|---|
Indentation |
4 |
NodeName |
Item |
NodeType |
Element |
SourceType |
Table |
SourceTable |
XML Items - Import |
VariableName |
L |
SourceTableView |
<Undefined> |
CalcFields |
<Undefined> |
LinkTable |
H |
LinkTableForceInsert |
<Yes> |
LinkFields |
Doc No=FIELD(Order No) |
Temporary |
<No> |
Width |
<0> |
MinOccurs |
<Once> |
MaxOccurs |
<Unbounded> |
After all the records have been inserted into the XML Items -
Import table, the Contact Person and Payment
Terms fields in the XML Header - Import table have to be
updated. You can do this by adding the following C/AL code line on
the Contact - Import::OnAfterAssignField()
trigger and
on the Terms - Import::OnAfterAssignField()
trigger:
Copy Code | |
---|---|
H.MODIFY; |