Publishing Canonical Item Variants
You can have source data that defines several product variants for a single product, but your target system does not support such variants. To address the conflict, you may use the Target Attribute transformation expressions to flatten this view for publication. The expressions produce unique product items that are derived from a combination of product and variant data items.
To de-normalize this relationship and to export a flattened structure into the target ApparelProduct, define the TargetItemMetadata for the ApparelProduct as follows:
The canonicalItemSource field should refer to CanonicalProductVariant item type. The reference means each CanonicalProductVariant becomes one ApparelProduct item in SAP Commerce Cloud.
The TargetAttributeDefinitions for ApparelProduct have the following values:
| id | attributeName | transformationExpression | targetItemMetadata |
|---|---|---|---|
| ... | code | sku | ... |
| ... | name | resolve('CanonicalProduct').name | ... |
| ... | size | size | ... |
The transformation expression from the extension XML file that is used to populate the code value is simply sku. It is the primary key of the CanonicalProductVariant. Processing this transformation expression assigns the value of the sku attribute in the CanonicalProductVariant item being evaluated to the code attribute of the target ApparelProduct item.
To populate the name attribute in the ApparelProduct, the transformation expression contains a call to the resolve function with the referenced item type: resolve('CanonicalProduct'). This function looks up and returns a CanonicalProduct referenced by the foreign key. The foreign key is implicitly defined by the naming convention and the primary key definition in the referred item type. The primary key identifier of the referred CanonicalProduct record is stored in productId attribute of the CanonicalProductVariant item. After resolving the referenced instance, you simply point to an attribute (name, in this case) to take the value from.
The resolve function is a SAP enhancement to the SpEL language, but the ability to navigate the object graph using the '.' to separate nested objects is a benefit of SpEL. You can use any other SpEL features in your transformation expressions. For example, if it is possible that the canonical product referred from the canonical product variant item does not exist, you can use this expression: resolve('CanonicalProduct')?.name, to make it null safe.
You can take advantage of the SpEL compilation configuration IMMEDIATE in Spring versions 4.1 and higher to improveData Hub performance. See http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html
Section 9.3.3.
Examples:
resolve('CanonicalProduct')
Searches for a canonical product having the same primary key value as the foreign key in the context object. The context object must be in the same pool where the expression context object exists.
resolve('CanonicalProduct', 'integrationKey=123')
Looks up a canonical product with an integration key value of '123' in the same data pool where the context object exists.
resolve('CanonicalProduct', 'GLOBAL')
Looks up a canonical product with a primary key matching the foreign key of the context object. The GLOBAL data pool is where the canonical product search is done.
resolve('CanonicalProduct', 'GLOBAL', 'manufacturerId=Sony', 'manufacturerProductId=NL123')
Searches for a canonical product in the GLOBAL pool. The canonical product has specified primary key attribute values (assume manufacturerId and manufacturerProductId is the primary key of the product).
Error Messages:
- If the referred item is not found, a MissingReferenceException is thrown. A FAILURE status results with a cause of MISSING_REFERENCE recorded for the publication status of the referencing item of the target system publication in progress. The referencing canonical item fails to publish then to the target system for the publication in progress.
- If a specific pool is not specified as a parameter, it limits its search scope to the same pool of data. Using the example above, you have loaded CanonicalProduct into one data pool and the CanonicalProductVariant into another data pool. If the resolve function does not locate the referenced CanonicalProduct, a MissingReferenceException results causing a FAILURE status. The cause of the failure status is a MISSING_REFERENCE. The CanonicalProductVariant item then fails to publish to the target system for the publication in progress.