There is an ER pattern called ‘The Callback‘ for data exporting, followed by adding data to tables or entities in the system. This pattern is typically used to mark certain records as exported. I also use it sometimes to add the export timestamp. I learned about this pattern a few years ago, but I couldn’t find the original source, which is why I’m not linking it here.
In this guide, I’m going to explain how to export records from the following table that are not marked as ‘Exported‘ and then mark them as ‘Exported‘ after exporting.
The main reason for using ER for this instead of code is that when we run an ER from code, we don’t know how the mapping and format will filter the table. However, the drawback is that this pattern is asynchronous, which means there’s a chance that two ERs might run at almost the same time and export the same records.
If you’re unfamiliar with exporting and importing formats using ER, I recommend reading my series beforehand.
Model
The model will have two roots: the first one, ‘Root’, contains all the fields we need for the report, and the second one, ‘Callback’, includes the fields of a unique key of the table. In this example, the unique key consists of the fields FieldString1
+ FieldString2
.
Export Mapping
Let’s create a mapping with ‘Root’ as the model definition.
We declare the table records node with our table, the NoYes
Enum, and create a calculated field to filter records that are not exported yet:
FILTER
(
tst_table001,
tst_table001.Exported = NoYes.No
)
Finally, we map the records to the model.
Format
After completing the Model and Mapping, we can create the format. In this example, we’ll use a TXT format:
Callback in the format
First, let’s mark the Records
sequence as a list so the format mapping will detect it as a record list.
Next, select the Map format to Model
button at the top, and create a new record with the definition pointing to the Callback.
Save and select Designer
.
Here, we simply bind the fields, and then we can complete the format:
Callback in the mapping
We create a new mapping record pointing To destination
:
In the Designer, in the Data Model column, we add our table with the record action as Update
:
Then, in the Data Sources, we add our model, pointing to the Callback root:
We also declare the NoYes
Enum that we find under the D365 for Operations
node.
And we bind the data source with the data model. (The names of the sections are a bit confusing; in reality, we are binding the model with the table.)
We can complete the mapping now and try it by running the format:
If everything is fine, the format with the records should be exported, and the Exported field in the original table will be marked as Yes
.
If we don’t want the key fields in the format
Maybe in our format, we don’t need the fields String1
and String2
, yet those fields are necessary for the callback. There’s a workaround: we modify the format so that the root is a folder. Under this folder, we include two files—the one we actually need and the one required for the callback. For the callback file, I usually give it a descriptive name like ‘Internal’.
Then, in the Format Mapping Designer
, I link this new file:
And complete the format.
Now we get a ZIP file with both files. This isn’t ideal, but at least it works:
However, we can use ER Destinations to export only the file we want, without including the ZIP or the internal file. To do this, we create the records for all the components but leave the settings blank for the ones we don’t want:
Leave a Reply