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
XMLPort using Text
variables:
- Source Type is the table of Payment Terms and we
add 3 lined with source type text corresponding to the 3 fields.
- Create a Boolean variable as below
- Add the Boolean to the Request Page.
- 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.
- Run the XMLport as below and import the file.
- Payment Terms is updated as follows
XMLPort using Table
and Fields:
- Source Type is the table of Payment Terms and we
choose relevant fields that we shall import.
- Create a Boolean variable as above
- Add the Boolean in the Request Page as above.
- 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.
- Run the XMLPort choosing 'File contains Header' and select the file.
- The Payment Terms gets updated