Factories

For each package defined in the business object model in the project, or in any projects upon which the project depends, there is an instance of a factory available in the form script editors. These are accessed via the available factory variable in each script.

New instances of complex types are created via the use of these factories. Each factory has a set of static methods that can be used to create instances of the classes defined within that package.

The factory for each package is referenced via an instance variable of the form: factory.<business-object-model-name>, where <business-object-model-name> is the fully qualified BOM package name, with "." replaced by an underscore "_" and its first character forced to uppercase.

A create method for each class is provided with the signature:

<class-name> create<class-name>([json | object])
<class-name> listCreate<class-name>([json | object])

For example, suppose the package com.example.customer contains classes for Customer and Address. We would create instances of each of these objects using the following:

var address = factory.com_example_customer.createAddress();
var customer = factory.com_example_customer.createCustomer();

A factory is only available for packages that directly contain class definitions. For example, there would not be a factory for the com.example package if there were no classes defined directly in that package.

The content assist in any script editor displays the available factories after typing factory. Only factories for packages in the current project or referenced projects is displayed.

Method Return Value Description
create<bom-class-name>([json | object])
An instance of the given class

If no argument is provided to the method, a new object of the given type is returned.

You can provide an optional argument that provides either a JSON string representation of an object of the given type, or a JavaScript object containing the same data. If you provide a wrong type of JSON or an array, the method throws a runtime exception.

listCreate<bom-class-name>([json | object])
A List instance that contains objects of the given class

If no argument is provided to the method, an empty list is returned. If you provide a JSON specification for a single object, the method returns that object wrapped in a list.

You can provide an optional argument that provides either:
  • a JSON String representation of an object of the given type
  • a JavaScript object containing the same data
  • a JSON String representation of an array of objects of the given type
  • a JavaScript array of objects representing the JSON data

If you provide a wrong type of JSON, the method throws a runtime exception.

Note: In the above methods, <bom-class-name> is the fully-qualified, dot-separated name of the BOM class to be instantiated. For example, to instantiate a class MyClass in the BOM package com.example.test, you need a "$type":"com.example.test.MyClass" attribute in the JSON, along with the values of any attributes defined for class MyClass.