Kodall
Workflow Language

Working with entities

This chapter covers the syntax and examples for working with entities. In Workflow language entities are first class citizens.

This chapter covers the syntax and examples for working with entities. In Workflow language entities are first class citizens.

Creating entities

CREATE [entityName]

Creates and returns a new entity. The entity will have a key only after the PUT statement.

Here is an example on how to create an entity:

ONE Workflow
//Create the entity
var c = CREATE color;

//Set the properties
c.code = "#ff2390";

//Save the entity
PUT c;

CREATE CHILD [entityVariable] [relationshipName]

Creates a new child entity and adds it to the named relationship on the parent entity.

Here is an example on how to create an entity with children:

ONE Workflow
//Creates an instance of the 'invoice' entity
var i = CREATE invoice;

//Set the properties
i.number = "1234";
 

// Create one child on the 'i' variable
// (which is an instance of the 'invoice' entity) for the
// 'invoice_invoice_item' relationship
var item = CREATE CHILD i invoice_invoice_item;

//Set the child properties
item.id_product = 100;
item.quantity = 50.4;
//Notice that we do not use PUT for the child entity
//Save the entity
PUT i;

Reading entities

GET [entityName] (key as int)

Reads an entity.

Here is an example on how to read & update an entity:

ONE Workflow
//Create the entity
var c = GET color(1);

//Read the properties
var colorCode = c.code;

//Set the properties
c.code = "#ff2390";

//Update the entity
PUT c;

CHILD [entityVariable] [relationshipName]

Here is an example on how to iterate over the children of a given relationship:

ONE Workflow
//Read the entity
var c = GET color(1);

//Iterate over the children
var count = 0;
foreach p in CHILD c color_part {
    //Set the properties on the child
    p.index = count;
    count = count + 1;
    //Notice that we do not use PUT for the child entity
}

//Set the properties
c.code = "#ff2390";

//Update the entity
PUT c;

Tracking changes

ORIGINAL [entityVariable].[propertyName]

Returns the original value of the property (before it was updated).

The original value of a property can be read as follows:

ONE Workflow
//Read the entity
var c = GET color(1);

//Set the properties
c.code = "#ff2390";

//This variable will contain the value of c.code before it was modified
var originalCode = ORIGINAL c.code;

The ORIGINAL keyword is especially useful when the update on data occurs in the interface:

ONE Workflow
// This method can be called via the workflow listener for any event
method main(c as ENTITY color) {
    //Read the original property value
    var originalCode = ORIGINAL c.code;

    //Check if the value has changed
    if (originalCode != c.code) {
        //Reject changes to this property
        validation->reject("color", "code", "Color code cannot be changed.");
    }
}

CHILD DELETED [entityVariable] [relationshipName]

Here is an example on how to iterate over the deleted children of a given relationship:

ONE Workflow
//Iterate over the deleted children collection
var count = 0;
foreach p in CHILD DELETED c color_part {
    count = count + 1;
}

Deleting entities

DELETE [entityVariable]

DELETE [entityName] (key as int)

DELETE CHILD [entityVariable] [relationshipName] [childEntityVariable]

Deletes an entity.

This example deletes the color entity, by first reading it:

ONE Workflow
//Read the entity
var c = GET color(1);

//Delete the entity
DELETE c;

This example deletes the color entity directly, by key:

ONE Workflow
//Delete the entity
DELETE color(1);

This example deletes the color_item child entity:

ONE Workflow
//Read the entity
var c = GET color(1);

// Copy all the children to a new list, this is necessary because you cannot
// delete items from a list while iterating over the same list
var toDelete as list of ENTITY color_item;
foreach ci in CHILD c color_color_item {
    toDelete[] = ci;
}

//Delete all the children of color(1) on relationship color_color_item
foreach ci in toDelete {
    DELETE CHILD c color_color_item ci;
}