{ensureitem:existing_id:template_long_item_id:field_1:new_value_1:...}

Description

Returns the ID of an existing item, or creates a new item and returns its ID. The first argument is an existing item ID: if it is a valid 32-character item ID, ensureitem returns it unchanged and does nothing else - the template and any field/value pairs are ignored, and no item is created or modified. If the first argument is empty or not a valid ID, ensureitem behaves exactly like newitem: it copies the template item (given by its ID) into a new item, applies the field/value pairs, and returns the new ID. This is the lookup-or-create pattern: pass a found ID (for example from an ids lookup) as the first argument so an item is reused when it already exists and created only when it is missing. The template must live in a slice related to the current slice, the new item is set active with an empty seo field, single values are plain text and multi-values are JSON arrays. ensureitem never updates an existing item - use updateitem for that. Because it touches the database it is never cached.

Parameters

existing_id required default (empty string)

An existing item ID to reuse. If this is a valid 32-character item ID, ensureitem returns it unchanged and stops - nothing is created or modified, and the remaining arguments are ignored. If it is empty or not a valid ID, ensureitem creates a new item from the template instead. Typically you pass the result of an ids lookup here so the item is reused when found and created when missing.

template_long_item_id optional default (none)

ID of the template item to copy when a new item must be created (used only when existing_id is empty or invalid). The template is an ordinary item - you may keep it in a Holding or Trash bin - and it must live in a slice related to the current slice. The new item inherits the template field values except seo (cleared) and status_code (set active). If the template ID is missing or not found, no item is created and the result is empty.

field_id optional default (none)

A field ID (16 characters, for example headline........) whose value to set on the newly created item. Append a plus sign to add a value instead of replacing it (relation........+). Field/value arguments come in pairs and repeat; they apply only on the create branch and are ignored when existing_id already points to an item.

new_value optional default (none)

The value for the preceding field_id. A single value is plain text, multiple values are written as a JSON array, for example ["enviro","social"]. Paired with field_id and repeatable; ignored when an existing item is returned.

Examples

test[{ensureitem:5726c2c6b035d7aab450d1794e9e90d7:any-template:headline........:Ignored}]
Expected[5726c2c6b035d7aab450d1794e9e90d7]
Actual[5726c2c6b035d7aab450d1794e9e90d7]
ensureitem never updates an existing item. With a valid existing ID the field/value pairs (here headline........) are skipped and the original ID is returned unchanged. Use updateitem to change a found item.
test[{ensureitem:5726c2c6b035d7aab450d1794e9e90d7:any-template}]
Expected[5726c2c6b035d7aab450d1794e9e90d7]
Actual[5726c2c6b035d7aab450d1794e9e90d7]
When the first argument is a valid item ID, ensureitem returns it unchanged. The template (second argument) is ignored and no item is created.