When iterating over objects, make sure that you retrieve the object as part of the same transaction that acts on the object. Otherwise, B2C Commerce returns an error to prevent you from accidentally overwriting the object with stale data.
If a node has
the Transactional property set to True, it executes as a
separate transaction. Script nodes, by default, have the Transactional
property set to true
.
To successfully update objects, you can use one of the following methods:
Method 1: commit each object in a separate Transaction
Identify all objects to update, retrieve each object one at a time, and then update each object in a separate transaction. We recommended this method for updating objects.
dw.catalog.ProductMgr.queryAllSiteProducts()
To_0
products
CurrentProduct
and the iterator key is
products
CurrentProduct.ID
to retrieve a
Product
Product,
and commits the transaction.Method 2: commit in a single Transaction
Update all objects in a single transaction. Use this method if you have only a few objects to update and you want changes to be made to all or none of the objects.
dw.catalog.ProductMgr.queryAllSiteProducts()
To_0
products
product,
updates it, and commits the transaction.As a best practice, do not
reuse the same iterator in multiple pipelets in the same pipeline. Doing
so can cause an IllegalStateException: Iterator is
invalid
error.
Iterators in this state don't return accurate results. Any attempt to use them results in a WARN
log message that says Iterator is invalid
. Scripts and pipelines that
generate this error should be rewritten to ensure that they generate correct and complete
results.
The correct way to reuse iterators is to repeat the SearchSystemObject step so that the second pipelet gets a fresh iterator.