🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators transform
This page was created by Lars.olofsson on 2019-02-16. Last edited by Wikiadmin on 2026-07-29.

You can use transform in an EAL action to copy a source object into an existing destination object by using a ViewModel as the mapping template.

Syntax

sourceObject.transform('ViewModelName', destinationObject)

The operator returns a Boolean value:

Return value Meaning
true The transform was full: the values described by the ViewModel were stored in the destination structure.
false The transform was not full. A destination attribute or link may be missing, or a destination attribute may be read-only.

How transform works

transform uses the source object as the root of the named ViewModel. The ViewModel describes the attributes and links to read from that source structure. MDriven then uses the ViewModel as a template to copy values and create the corresponding destination structure.

The source and destination objects can belong to different classes. Mapping is based on matching attribute and link names. This cross-class mapping based on structural similarity is known as duck typing.

For example, if a ViewModel rooted at Självfaktura contains attributes and links whose names also exist on OutgoingEmail, transforming a Självfaktura into an OutgoingEmail copies those matching values and can create the related destination objects described by the ViewModel.

Create and populate an outgoing email

Use this pattern when an action must collect values from several objects and create an item in an email queue.

  1. Create the destination object.
  2. Call transform with the ViewModel that defines the source data to copy.
  3. Keep the new object only when the transform returns true.
  4. Delete the new object when the transform returns false, so that a partially populated queue item is not retained.

The following EAL action on Självfaktura creates an OutgoingEmail and fills it through the TR_SjälvfakturaSkickaPDF ViewModel:

let oe = OutgoingEmail.create in
(
  if self.transform('TR_SjälvfakturaSkickaPDF', oe) then
    true
  else
    oe.Delete
  endif
)

In this example, the ViewModel maps matching attributes and links from Självfaktura to OutgoingEmail. It also creates EmailAttachment objects when those objects are included in the ViewModel structure.

Populate a multi-link from one value

A multi-link needs a collection. If Självfaktura has a single Pdf attribute but the destination uses the multi-link EmailAttachments, use self->asSet in the ViewModel to provide a collection rather than a single link value.

This lets the ViewModel use the single PDF value when filling the destination attachment collection.

Transform association classes

An association class is a link object that carries its own data. Consider this structure, where C is the link object between A and B:

A *--------* B
            |
            C

You may want to create new A and C objects while retaining the reference to the existing B object.

  1. Focus the ViewModel on A and C.
  2. Do not include B in the ViewModel.
  3. Transform the source object.

Because C is a link object, transform discovers the original B reference and reuses it on the new C.

If you instead need new B objects, include the details for B as a nested part of the ViewModel. Transform then has the information required to create the new B structure.

Validate duck-typed mappings

Name matching makes a transform sensitive to model changes. For example, renaming a destination attribute can cause a transform to return false if the ViewModel still expects the old name.

In the ViewModel Editor, configure the ViewModel's DuckType class so that normal model validation can type-check the transform at design time. This helps identify incorrectly named attributes and links before the action runs.

Diagnose a false result

A false result means that the transform was not full. Check the following:

  • The destination class has attributes and links with the names expected by the ViewModel.
  • Destination attributes that must receive values are writable.
  • The ViewModel contains the required nested structure for objects that must be created.
  • The ViewModel's DuckType class is configured so model validation can detect mismatched names.

There is currently no log that identifies the exact reason why a transform returned false. Handle the Boolean result in the action, as in the email example, and use DuckType validation to find mapping errors during design.

Transform and deepclone

Use transform when you already have a destination object, including when the source and destination classes differ. Use deepclone when you need a new unsaved object of the same class as the source; deepclone is a special case of transform and uses a ViewModel to define the copied depth and structure.

See also