Simple Approach - find/get
→ if (missing)
→ create
At first it seems like the logic to branch and create a “record” should be simple and straightforward as:
- Find (REST GET)
- if exists, continue, else create then continue
Challenges in n8n
- how to retain the input-item details (from before the REST call and/or found vs. created branches)
- how to keep the items in the original order
- how to wait until all items have either been found or created before continuing
Why so complex?
- Sometimes, it can be simple, if you are using a DB node or REST API that is designed to always give you a response (output item) whether or not it finds something, but if an HTTP node returns a
404 - not found
the output item count is different. - If the workflow then branches to the true/false outputs of an
If
node, timing and processing order might yield output items in a different order, making it necessary to re-correlate items on some key value.
Solution (one at least)
After struggling for a while with this task to import products and categories into Wordpress / WooCommerce, I reached a somewhat reliable approach to “conditionally create” things before continuing to create other dependent things. This workflow demonstrates the main elements, which are:
- All original items, are forwarded to a final merge to be “enriched” by other found (existing) or created info. This preserves the original item order and retains all items.
- Items “not found” using the GET REST call are “output anyway” from an
HTTP Request
node by using the “Never Error” option (and dealing with the actual non-404 errors separately). Edit Fields (set)
nodes are used to homogenize the output field names from GET vs. POST REST API calls, AND to forward a field/value (using item-linking) that can be used to correlate to the original input items.