Impex: Examples and Tips

Impex is the main way to import data into the SAP Hybris eCommerce Suite. It is based on CSV and offer additional features like scripting, handling of maps and collections and removal of items.

Examples

Importing a property with a map 

INSERT_UPDATE BTGConfig[unique=true];defaultTimeUnit(code);usedRuleTypes(code);operandMapping(key(code),value(code))[map-delimiter=|]
BTGConfig;WEEK;ORDER,CART,USER,WCMS;ORDER->BTGOrganizationTotalSpentInCurrencyRelativeDatesOperand,BTGOrganizationTotalSpentInCurrencyLastYearOperand,BTGNumberOfOrdersAboveAmountRelativeDateOperand,BTGCategoriesInOrdersOperand,BTGEachOrderTotalSumOperand,BTGOrderTotalSumOperand,BTGProductsInOrdersOperand,BTGNumberOfOrdersOperand,BTGNumberOfOrdersRelativeDateOperand,BTGLastOrderDateOperand|CART->BTGCartIsEmptyOperand,BTGCartTotalSumOperand,BTGCategoriesInCartOperand,BTGProductsInCartOperand,BTGQuantityOfProductInCartOperand|WCMS->BTGViewedProductsOperand,BTGViewedCategoriesOperand,BTGVisitedContentpagesOperand,BTGReferalUrlOperand,BTGUrlParameterOperand|USER->BTGOrganizationUIDOfUserOperand,BTGUserAddressPostalCodeOperand,BTGUserCountryOperand,BTGUserGenderOperand,BTGUserMemberOfGroupsOperand|SCRIPT->BTGMediaScriptOperand,BTGStringScriptOperand

The last column contains the values for a property defined as map<BTGRuleType, BTGOperandTypeCollection>

–Based on Hybris 6.3

Importing two objects which depend on each other with mandatory fields

Suppose that the following relation is defined:

      <relation code="B2BUser2Lagerhaus" autocreate="true" generate="true" localized="false">
         <deployment table="b2bUsers2Lagerhaeuser" typecode="15067"/>
         <sourceElement qualifier="b2bZentralbesteller" type="B2BUser" cardinality="many">
            <modifiers optional="false"/>
         </sourceElement>
         <targetElement qualifier="b2bLagerhaeuser" type="Lagerhaus" cardinality="many">
            <modifiers optional="false"/>
         </targetElement>
      </relation>

The following Impex Scripts adds an user and a lagerhaus at the same time:

INSERT_UPDATE B2BUser;UID[unique=true];groups(uid)[mode=append];b2bLagerhaeuser(uid);
#% impex.setValidationMode("import_relaxed");
;dummyB2BUser;;LH_200

INSERT_UPDATE Lagerhaus;uid[unique=true];id;locname;rechtsform;homepage;mandantId;b2bZentralbesteller(uid)
;LH_200;200;Lagerhaus Aubing;eGen;http://www.acme.de;200;dummyB2BUser

Relaxing the validation means that Hybris doesn't check the mandatory fields. The use of item references with the symbol & don't solve this issue.

Testing if a condition is meet

This is an example on how to use if statements in Impex

#Due to the initial attributes, we cannot update the execution result, we can only insert them if they doesn't exist.
#% if: de.hybris.platform.core.Registry.getApplicationContext().getBean(de.hybris.platform.servicelayer.search.FlexibleSearchService.class).search("SELECT {r.PK} FROM {ScriptExecutionResult as r}").getCount() == 0;

INSERT ScriptExecutionResult;name[unique=true];canBeRunnedAgain[forceWrite=true];description[lang=en];;SUCCESS;false;Success;;ERROR;true;Error;

#% endif:

–Based on Hybris 6.5

How to export some selected items with Impex

In some occasions we need to export a subset of the existing products or other items. This can be do using Impex and a flexible search query. The Impex Writer class allows to use a flexble search query to select the items to export:

INSERT_UPDATE Product;pk[unique=true];sapECommerceVersandort
"#% impex.exportItemsFlexibleSearch(""select {p.pk}, {p.sapECommerceVersandort} from {Product as p} where {p.sapECommerceVersandort} is not null order by {p.pk}"");"

In the above example with export all the products (including subclasses) where the field sapECommerceVersandort is not null. The only restriction is that the query must return the pk of the items. Then the header defines what fields are going to be exported.

You can also export many-to-many relations and use the fields source and target to extract the information you need. In this example where get a list of product code with their keywords:

INSERT_UPDATE Product2KeywordRelation;source(keyword)[unique=true];target(code)[unique=true]
"#% impex.exportItemsFlexibleSearch(""select {p2k.pk} from {Product2KeywordRelation as p2k inner join Product as p on {p2k.target}={p.pk}} order by {p.pk}"");"

:!: The flexible search must be in one line.

–Based on SAP Hybris 6.4

Remove a many to many relation if exist

$country = DE
$productCatalog = productCatalog$country
$productCV = catalogVersion(catalog(id[default=$productCatalog]), version[default='Online'])[unique=true]
$mediaCatalog = mediaCatalog$country
$mediaCV = catalogversion(catalog(id[default=$mediaCatalog]), version[default='Online'])[unique=true, default=$mediaCatalog:Online]

# Remove medias from the medias field if they exist.
REMOVE CategoryMediaRelation[batchmode = true]; source(code, $productCV)[unique = true]; target(code, $mediaCV)[unique = true]
                                              ; windows                          ; thumbnail-windows  ;                                              

–Based on SAP Commerce 2005

Discussion

Sankalp Sharma, 2020/08/17 11:54

Very good useful info. Thanks Antonio.

Enter your comment. Wiki syntax is allowed: