How to Import PX Data to a Datamart

We can import PX data to the Datamart using an Analytics calculation logic:

  1. Create an Analytics calculation logic with a Groovy element (set the Calculation Context of this element to Init) similar to this "Competition" PX Category example:

    def target = api.getDatamartRowSet("target")
    def result ="PX", null /* sort-by*/, 
                            Filter.equal("name", "Competition"))
    while (result.hasNext()) {
       def comp =
       // to be able to load rows in the DS we need at least the key fields
       if (comp.sku != null && comp.attribute1 != null) {
          def row = ["DateTime":now,
                     "sku": comp.sku,
                     "Competitor" : comp.attribute1,
                     "Price" : comp.attribute2,
                     "ShippingCost" : comp.attribute5,
                     "DataProvider" : comp.attribute7,
                     "Availability" : comp.attribute9,
                     "Relevant" : comp.attribute11]
  2. Provided you already created an appropriate Data Source to store the data, in the Data Loads tab you can add a Data Load of the type "Calculation" which uses the above logic to load your PX data.

This next example is not related to the one before, it only shows in principle how to cache data read from PX table to a global Map

def sku2Competitor =
if (sku2Competitor == null){
  sku2Competitor = [:] = sku2Competitor
def skn = api.getElement("SKN")
if (sku2Competitor.containsKey(skn)){
  return sku2Competitor.get(skn)
  // attribute4 = Competitor
  def pxNameFilter = api.filter("name", "NamedCompetition")
  def pxCompFilter = api.filter("attribute4", api.getElement("Competitor"))
  def pxSKNFilter = api.filter("sku", api.getElement("SKN"))
  def competitors = api.find("PX", 0,1, null /*sort*/, ["id"], pxNameFilter, pxCompFilter, pxSKNFilter)
  //api.trace("test", competitors?.size().toString(), pxCompFilter)
  def hasCompetitor = competitors?.size() > 0? 1:0
  sku2Competitor.put(skn, hasCompetitor)

  return hasCompetitor

A global variable 'sku2Competitor' is created to ensure that the logic is not run multiple times for the same SKN. In the else statement, we set up filters using api.filter("fieldName", value). Then we create an array "competitors" using api.find(String typeCode, int startRow, int maxRows, String sortBy,List<String> fields, Filter... filters) with the filters that we created to find the row in the NamedCompetition PX that has the given Competitor and SKN. The SKN and Competitor are then stored in the global variable.