Skip to main content
Version: 7.0

New object (NEW)

The new object operator creates an action that creates objects of a specified custom class and optionally writes each created object to a data property. The target class must be a concrete custom class — an object cannot be added to the system without a known concrete class.

The operator has two forms.

  • In the bulk form, an object is created for every object collection for which some property (the condition) is not NULL. The created object can be written to a specified target data property with objects of that collection as its arguments. Each set of argument values of that property must correspond to exactly one created object. If no target is specified, the created object is not written anywhere.
  • In the block form, exactly one object is created and a body that follows the action has read access to the new object through a local name. This form is the natural way to create one object and initialize its properties in the same action. To retain a reference to the created object for subsequent actions, it can be written inside the block to a property, for example a local property.

Objects can also be created inside the loop action, which creates one object per loop iteration and exposes it to the loop body — see the loop article for the corresponding option.

Language​

To declare an action that creates objects, use the NEW operator. For loop-driven creation, see the NEW option of the FOR operator.

Examples​

// bulk form: create three Sku objects and write each one into addedSkus(i)
newSku () {
LOCAL addedSkus = Sku (INTEGER);
NEW Sku WHERE iterate(i, 1, 3) TO addedSkus(i);
FOR Sku s = addedSkus(i) DO {
id(s) <- 425;
name(s) <- 'New Sku';
}
}

// block form: create one Sku and initialize its properties
newSku () {
NEW s = Sku {
id(s) <- 425;
name(s) <- 'New Sku';
}
}

// block form: create a copy of a Sku, sharing the same id and name
copy (Sku old) {
NEW new = Sku {
id(new) <- id(old);
name(new) <- name(old);
}
}

// loop-driven creation: one OrderDetail per Sku where in(s) holds
createDetails (Order o) {
FOR in(Sku s) NEW d = OrderDetail DO {
order(d) <- o;
sku(d) <- s;
}
}