Using XMLports in AL code

As we said in Chapter 2, Mastering a Modern Development Environment, XMLports are objects used to import and export data between Dynamics 365 Business Central and external data sources (this is managed by the Direction property, which can be set to Import, Export, or Both). Data can be imported or exported in XML or CSV (text) format (the Format property can be set to Xml, Variable Text, or Fixed Text).

Now, consider the sample XMLport defined in Chapter 4, Extension Development Fundamentals:

xmlport 50100 MyXmlportImportCustomer
{
Direction = Import;
Format = VariableText;
FieldSeparator = ';';
RecordSeparator = '<LF>';
schema
{
textelement(NodeName1)
{
tableelement(Customer; Customer)
{
fieldattribute(No; Customer."No.")
{
}
fieldattribute(Name; Customer.Name)
{
}
fieldattribute(Address;Customer.Address)
{
}
fieldattribute(City;Customer.City)
{
}
fieldattribute(Country;Customer."Country/Region Code")
{
trigger OnAfterAssignField()
begin
//Executed after a field has been assigned a value and before it is validated and imported.
end;
}
}
}
}
}

To execute an XMLport in Dynamics 365 Business Central, you need to run it from a page or a codeunit object (you cannot directly run it). XMLport request pages (used to set filters or insert parameters) in the Dynamics 365 Business Central web client are not supported.

To execute an XMLport in Dynamics 365 Business Central to import data from a file, you need to use the following code:

procedure RunXMLportImport()
var
FileInstream: InStream;
FileName: Text;
begin
UploadIntoStream('','','',FileName,FileInstream);
Xmlport.Import(Xmlport::MyXmlportImportCustomer,FileInStream);
Message('Import Done successfully.');
end;

Here, the file is loaded into an InStream object and then the XMLport is executed by passing the InStream object as input.

To execute an XMLport in Dynamics 365 Business Central to export data to a file, you need to use the following code:   

procedure RunXMLportExport()
var
TempBlob: Codeunit "Temp Blob";
FileName: Text;
FileOutStream: OutStream;
FileInStream: InStream;
outputFileName: Text;
begin
TempBlob.CREATEOUTSTREAM(FileOutStream);
Xmlport.Export(Xmlport::MyXmlportImportCustomer, FileOutStream);
TempBlob.CREATEINSTREAM(FileInStream);
outputFileName := 'MyOutputFile.xml';
DownloadFromStream(FileInStream,'','','',outputFileName);
//The output is saved in the default browser's Download folder
end;

Here, we have seen how to use XMLports from AL code to import or export data. In the next section, we'll see how to create and extend Role Centers in Dynamics 365 Business Central.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.104.248