How toDeveloper Guide

Developer Guide

Get a Custom Field from the Item Table in NAV during Product Import

In this chapter you can learn how to extend the product import by getting extra data from NAV and saving it to the product table in the SQL database. The data is added to the product table in the SQL database automatically when you add fields to the fields' collection.

There are two ways how to get extra data from NAV during the product import: one can be accomplished by .NET developers and another one by NAV developers.

The following chapter presents an example of how to retrieve the 'Producer' custom field from NAV. In the 'Item' table you have a custom column with the 'Producer' name.

In the notes below an appropriate example is illustrated.

For .NET developers:

.NET developers can get extra data from NAV during the product import using the 'EntityFields' property of the 'IProductListLoadOptions' interface. To do this:

  • Configure the 'Product Import' task. Read this chapter about the 'Product Import' task configuration;
  • Create a class and inherit it from the 'ProductImportTask' class. Replace the 'type' attribute of the 'Product Import' task with newly created class;
  • Override the 'CreateProductListLoadOptions' method and add the following code:
  • public class CustomProductImportTask : ProductImportTask
    {
    protected override IProductListLoadOptions CreateProductListLoadOptions(int pageIndex, int batchSize, bool visibleOnly, IEnumerable<string> entityFields)
      {
    IProductListLoadOptions options = base.CreateProductListLoadOptions(pageIndex, batchSize, visibleOnly, entityFields);
    List<string> fields = new List<string>(options.EntityFields);
    // Add fields' names which must be imported from NAV into the fields' collection
    options.EntityFields = fields;
    return options;   }
    }
  • Example:
    public class CustomProductImportTask : ProductImportTask
    {
    protected override IProductListLoadOptions CreateProductListLoadOptions(int pageIndex, int batchSize, bool visibleOnly, IEnumerable<string> entityFields)
     {
    IProductListLoadOptions options = base.CreateProductListLoadOptions(pageIndex, batchSize, visibleOnly, entityFields);
    List<string> fields = new List<string>(options.EntityFields);
    fields.Add("Producer");
    options.EntityFields = fields;
    return options;
    }
     }
  • The field can be accessible through the 'Fields' collection by key. You can use the following code to get the field value:
    var producer = product["[FIELD NAME]"];
    
    Example:
    var producer = product["Producer"];
    

    You can also save the value in another place by overriding the 'SaveProduct' product function of the class inherited from the 'ProductImportTask' class and adding the code to the save value.

For NAV developers:

NAV developers can get extra data from NAV during the product import by creating additional fieldelements in the response XML in NAV. To do this:

  • Open the codeunit 11123318 'SC - Catalog Helper' in design mode;
  • Locate the 'AddProductXMLDefaultFields' function;
  • Add the following code to this function:
    XMLFunctions.AddFieldElement(ResultNode,'CustomField','Value');
    
    Example:
    XMLFunctions.AddFieldElement(ResultNode,'Producer',Item.Producer);
    
  • Save and recompile the codeunit;
  • The field can be accessible through the 'Fields' collection by key. You can use the following code to get the field value:
    var producer = product["[FIELD NAME]"];
    
    Example:
    var producer = product["Producer"];
    

After you complete these steps the additional data will be imported from NAV to the SQL database.

How toDeveloper Guide