Skip to Content
Technical Articles

Add Child entities to already created entities with OData Adapter in CPI

In this blog post you would be able to learn how to add an entity to an existing parent entity. Consider a scenario where a parent entity is created using the create operation with two child entities and you need to add another child entity to the parent entity. One common mistake that users try to do is to update the parent entity with the payload consisting of the parent and the child entity. However, deep update is not supported in OData Receiver adapter in CPI. We will show a simple example to achieve this scenario. For example purposes we are using the Northwind service as the OData service (https://services.odata.org/(S(random))/V2/OData/OData.svc).

Example Scenario

Our scenario is that we first create a “Category” entity with two “Products” as child entities of the product. After that we will create another “Product” entity and link it with the earlier created “Category” entity. Thus, the first Create operation creates a “Category” with two “Products”. The second operation will update the existing “Category” by adding a third “Product”. To do this we created a simple iflow with a “Sequential Multicast” with two branches. The first branch creates the “Category” with two “Products”. The second branch will update the “Category” adding a third “Product”.

Creating Parent Entity(Category)

The first branch first contains a Content Modifier with the following payload for the OData Adapter. The payload is used to create a Category called “test category” with two “Products”. To know more about the payload structure for the OData Receiver adapter please go through the blog https://blogs.sap.com/2017/09/06/payload-structures-in-odata-v2-adapter-for-sap-cloud-platform-integration/

 

<Categories>
	<Category>
		<ID>1001</ID>
		<Name>test catgeory</Name>
		<Products>
			<Product>
				<ID>100001</ID>
				<Name>Product1</Name>
				<ReleaseDate>1992-01-01T00:00:00</ReleaseDate>
				<Rating>2</Rating>
				<Price>100</Price>
			</Product>
			<Product>
				<ID>100002</ID>
				<Name>Product2</Name>
				<ReleaseDate>1992-01-01T00:00:00</ReleaseDate>
				<Rating>3</Rating>
				<Price>101</Price>
			</Product>
		</Products>
	</Category>
</Categories>

 

The OData Receiver configuration should be as follows. The following screenshot shows the connection tab.

 

Model the scenario using the query modeller by selecting Categories and the child entity Products.

 

You can save the output of the

 

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.text.SimpleDateFormat;
import java.util.Date;

def Message processData(Message message) {
    
	def headers = message.getHeaders();
	def body = message.getBody(java.lang.String) as String;
	def messageLog = messageLogFactory.getMessageLog(message);

    if(messageLog != null){
        messageLog.addAttachmentAsString("QueryResponse_before_update", body, "text/plain");
     }
    return message;
}

 

Creating Child Entity(Product)

We will now model the second branch of the the multicast in a second Local Integration Process. First we have a content modifier with the following payload. The following payload consists of a Product which is linked to the created Category earlier.

<Products>
	<Product>
		<ID>10003</ID>
		<Name>Product3</Name>
		<ReleaseDate>1992-01-01T00:00:00</ReleaseDate>
		<Rating>3</Rating>
		<Price>200</Price>
		<link>
			<Category>
				<Category>
					<ID>1001</ID>
				</Category>
			</Category>
		</link>
	</Product>
</Products>

 

Next we will model an OData Receiver adapter with the Create(POST) operation on the “Products” entity. We will also have to select the “Category” entity¬† to create a link fields between them.

You can verify the payload using a mapping step. For this please use the XSD generated by the OData Receiver adapter. The XSD can be placed just before the OData Receiver adapter in the target section of the mapping step. Map the fields of the “Product” entity and the link field of the entity to make sure that the incoming payload is valid.

 

Verifying the iflow

The whole process can be verified by querying the service.

After this step you can use the script above to query for the Category entity to verify that the entity has been updated with the third Product. The following OData adapter configuration can be used to read the created Category.

You can use the script provided above the save the output of the operation as an attachment. The attachment name can be changed accordingly.

 

So, the example iflow demonstrates a way to update a parent entity with the child entity using simple OData operations.

 

Be the first to leave a comment
You must be Logged on to comment or reply to a post.