This topic provides an overview of how Integration API persistence works, and how POST and PATCH request payload arrangements affect both persistence and the request result.
The sections that follow expand on the following key points:
Elements of the payload are persisted in the order in which they appear in the payload: From top to bottom, and from outer elements to the deepest inner elements.
While processing POST requests, Integration API searches for an item in the system that corresponds to the payload element based on the key attributes present in the payload. If the element is detected it's updated with the content from the payload. If it's not found, a new item is created if it's the root element in the payload or a nested element and that element is nested into an attribute that has autoCreate=true in the Integration Object definition, or if that attribute has partOf=true in the type system. Otherwise, an error is returned.
How a Payload is Processed: Persistence Flow
Here is an example POST Product payload:
The above payload is processed in the following way:
The Product key is derived from the payload attributes (Product.code + CatalogVersion.version + Catalog.id. If key attributes are not present or empty, the missing_key_attribute error is returned. For error code message descriptions, see Error Messages.
The key attributes search for the Product item in the platform. If found, the existing item is used and its attributes are populated with values from the payload. If the item doesn't exist, it is created and then populated.
Once the item is resolved its attributes start populating. From the example above, the first attribute code is assigned the value Men's Beret, then the next attribute catalogVersion is populated, etc.
catalogVersion refers to a nested item, so the key attributes search for CatalogVersion.version and Catalog.id. If found, catalogVersion is updated with attribute values in the payload first. Only then it is assigned to the Product's catalogVersion attribute. If not found and the attribute is autoCreate=true, then it's created automatically. If not found and it's not an autoCreate attribute, the missing_nav_property error is returned. For this example, the CatalogVersion item was found.
The CatalogVersion.catalog attribute is populated similarly to how catalogVersion is populated.
The CatalogVersion.version attribute is assigned the primitive value 2020.
The Product.unit attribute is populated by searching for then creating the Unit item similarly to how the catalogVersion and catalog attributes are populated.
Once all of the payload attributes have been applied, all updated and created items are saved in a single transaction. For example, if the unit attribute isn't populated, then changes to Catalog, CatalogVersion, and Product made up to this point are ignored (rolled back).
When a Payload Contains References Betweeen Items
It's especially important to understand the persistence flow when a payload contains references between items. Using the example POST Product payload above, when defining the Integration Object for Product, make sure that the Integration Object Item is defined before it's referenced from an Integration Object Item attribute's returnIntegrationObjectItem element. For example:
In the example above, each item is defined before it's used in an attribute: Catalog before CatalogVersion.catalog attribute is defined, CatalogVersion before Product.catalogVersion is defined, etc. Unit can be defined before or after Catalog and CatalogVersion because it's not referenced from those types, but it's important to define Unit before Product.unit attribute is defined.
Cyclical References
Sometimes item dependencies in an Integration Object form a graph, not a tree. That is the case when two or more items reference each other. Most often, such references are expressed as relations in the type system's items.xml. For example, Customer (subtype of User) and Address form such cyclical reference through this relation in core-items.xml:
If you try to expose this relation in an Integration Object: If you first define Address then the Address.owner attribute fails because Customer doesn't exist yet. If you first define Customer, then the Customer.address attribute fails because Address doesn't exist yet. To solve this problem, the item definitions must be split into two parts: First, an item is defined without the referenced attribute; second, the other item can be defined with attributes referencing it, then the first item can be defined again with the reference attributes. Below are examples illustrating different approaches to solving this problem for Customer and Address types.
Adress - Customer - Address: In this payload, the Address definition is split into two parts.
Customer - Address - Customer: In this payload, the Customer definition is split into two parts.
Address - Customer - Address - Customer: In this payload, all items are first defined without attributes, then defined with attributes, The Customer and Address definitions are split into two parts.