CRM and CX Blogs by SAP
Stay up-to-date on the latest developments and product news about intelligent customer experience and CRM technologies through blog posts from SAP experts.
cancel
Showing results for 
Search instead for 
Did you mean: 
priti_mittal
Product and Topic Expert
Product and Topic Expert
1,807

Title : How to customise pre-persistence hook and validate from postman


Integration API Module : SAP Commerce's Integration API Module exposes a set of interfaces used for data integration with SAP Commerce. The Integration API module simplifies data integration with SAP Commerce using Integration Objects. 

Quicklinks:
Integration API Module
Persistence Hooks



Content


0.  Prerequisites
1.  Introduction
2.  Tutorial
2.1. Pre-Persistence hook customisation
2.2. Postman validation

 

0. Prerequisites



  • Understanding of  SAP Commerce

  • Familiar with the usage of Postman


1. Introduction


Standard SAP integration module does not support to replicate B2B Organisation with empty addresses . In this blog , we will see the use case where  B2B unit will be replicated with empty addresses .

Pre-Persistence Hooks : Pre-Persistencehook is used to execute business logic before  a request to persist an item.

2. Tutorial


 In case of B2BUnit replication , Pre-persist hook "SapCpiB2BUnitPersistenceHook" is used by standard . in this tutorial , SapCpiB2BUnitPersistenceHook will be customised to support B2B unit replication with empty addresses and the same will be validated via postman .


 

2.1. Pre-Persistencehook customisation


To customise a persistence hook, define a custom PrePersistHook  and then specify that persistence hook in a persistence request.

2.1.1. Create custom hook

Sample Code
package org.custom.integration.sapcpiproductexchange.inbound.events;

import de.hybris.platform.b2b.model.B2BUnitModel;
import de.hybris.platform.core.model.ItemModel;
import de.hybris.platform.sap.sapcpicustomerexchangeb2b.inbound.events.SapCpiB2BUnitPersistenceHook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.Optional;

/**
* This replaces SapCpiB2BUnitPersistenceHook to handle empty address scenario
*/
public class CustomCpiB2BUnitPersistenceHook extends SapCpiB2BUnitPersistenceHook
{
private static final Logger LOG = LoggerFactory.getLogger(CustomCpiB2BUnitPersistenceHook.class);

@Override
public Optional<ItemModel> execute(final ItemModel item)
{
if (item instanceof B2BUnitModel b2BUnitModel)
{
LOG.info("The persistence hook customCpiB2BUnitPersistenceHook is called!");
//Process addresses in case not empty
if (!CollectionUtils.isEmpty(b2BUnitModel.getAddresses()))
{
return super.execute(item);
}
}
return Optional.of(item);
}
}

 

2.1.2. Register the PrePersistHook in a Spring context for it to be available in the application

Sample code : <>-spring.xml
<!-- Inbound Custom B2BUnit Pre-Persistence Hook -->
<bean id="customCpiB2BUnitPersistenceHook"
class="org.custom.integration.sapcpiproductexchange.inbound.events.CustomCpiB2BUnitPersistenceHook"/>

 

2.1.3. Configure required dependency in extensioninfo.xml

Sample code :

Add the dependence of sapcpicustomerexchangeb2b extension in extensioninfo.xml of <custom> extension
<requires-extension name="sapcpicustomerexchangeb2b"/>

 

2.2. Validation via Postman


2.2.1.  Request Header :  Define customCpiB2BUnitPersistenceHook as value against key 'Pre-Persist-Hook'

Sample :


Body :


 

2.2.2.  Test your changes :  Send the request via postman and validate the data in backoffice 

Conclusion  : All the above steps and suggestions are given from the experience gained during implementation phase.

Kindly share your thoughts and feedback in the comments section below.
5 Comments