Be careful when iterating over objects to make sure that you retrieve the object as part of the same transaction that acts on the object. Otherwise, Salesforce 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. This is the recommended 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. This method must only be used if you have a reliably small number of objects to update and you only want changes to be made to all objects or none of them.
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 will result in a WARN log
message that says Iterator is invalid
. Scripts and
pipelines that generate this error should be rewritten to ensure they
generate correct and complete results.
The correct way to do this is to repeat the SearchSystemObject step so that the second pipelet gets a fresh iterator.