Monday 12 November 2018

Import CSV File with Header using XMLPort

When we start to build an XMLPort, we specific the XMLport to be based on either a table or text variables. What happens when we import a file source having header in these two cases. Let’s cover these two cases in this post.
Data File: We are going to import data into Payment Terms table. Our csv file has header and the data as follows
       
(Scenario 1)




 (Scenario 2)

XMLPort using Text variables:


  1.   Source Type is the table of Payment Terms and we add 3 lined with source type text corresponding to the 3 fields.
  2. Create a Boolean variable as below
  3. Add the Boolean to the Request Page.
  4. Add the below code. When XMLPort reads the file, it evaluates/maps all the data in the file as text, since the source type is given to text. We need to further convert the text to respective field data types using evaluate function.
  5.   Run the XMLport as below and import the file.
  6. Payment Terms is updated as follows


XMLPort using Table and Fields:
  1. Source Type is the table of Payment Terms and we choose relevant fields that we shall import.
  2. Create a Boolean variable as above
  3. Add the Boolean in the Request Page as above. 
  4. Now is the main difference between importing into a source table directly vs. using text variables. When we import data into source tables directly, the header data i.e text in the file cannot be evaluated into non-text data type fields we have selected. Because XMLPort reads every line of CSV and tries to evaluate into respective fields. For this, we instruct NAV to skip the first line if our file has headers, like above. But we cannot assign it in the OnBeforeInsertRecord() because the evaluation (NOT Insertion) takes place before this trigger. So we place the code to skip the first-line import in OnAfterInitRecord() which is triggered before any evaluation into fields or assigning into variables takes place.
  5. Run the XMLPort choosing 'File contains Header' and select the file.
  6. The Payment Terms gets updated




Hope this helps!

Monday 29 October 2018

Using XML DOM Management to export data in XML

In this demonstration, we will be making use of the XML DOM Management codeunit and its capabilities to export the Payment Terms data in XML format.