Skip to Content
Technical Articles
Author's profile photo Priti Mittal

SAP Commerce’s Integration API Module : Understanding of Persistence Hooks customisation and validation via postman

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 :

Header

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.

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Cristian Manole
      Cristian Manole

      this was very useful for me. especially the part of adding the "Pre-Persistent-Hook" to the call in postman. We were going in circles trying to see why the hook is not called. You are a life saver!

      Author's profile photo Yashwanth Kumar Koratikere
      Yashwanth Kumar Koratikere

      Very Well Articulated Article Priti Mittal

      Author's profile photo Vijay Devaruppala
      Vijay Devaruppala

      Hi Priti Mittal  Great write up on the topic. I was wondering if you can please share the sample postman collection on this topic for  Validation of B2BUnits/B2BCustomers ? Will appreciate your help. Thanks

      Author's profile photo Priti Mittal
      Priti Mittal
      Blog Post Author

      I do not have the payload from OOTB system hence unable to share .

      Author's profile photo Prathmesh Aras
      Prathmesh Aras

      Can someone please help how to get the Model Object from Hybris DB in a Pre-Perist-Hook ?