Factories

At runtime, when a new object needs to be created to represent a particular Business Object (instance of a BOM class) or other variable, a Factory method needs to be used to create the object instead of using the new keyword that is usually used in JavaScript.

Only values for attributes of the following types do not need to be created with Factories:

  • Boolean
  • Text
  • Integer (Signed integer sub-type)
  • Decimal (Floating point decimal sub-type)

The primitive field types that represent measurements of time are created by DateTimeUtil factory methods:

  • DateTimeUtil.createDate() 
    DateTimeUtil.createTime()
    DateTimeUtil.createDatetime()
    DateTimeUtil.createDatetimetz()
    DateTimeUtil.createDuration()

To create fixed integer and fixed decimal object instances, the following two ScriptUtil factory methods are used:

  • ScriptUtil.createBigInteger()
    ScriptUtil.createBigDecimal()

Boolean fields can be assigned with the keywords true and false, or the result of an expression (such as (2 == value)). However, if you want to convert a text value true or false to a Boolean, then the ScriptUtil.createBoolean() can be used.

The factory methods for BOM classes can be found in Factory classes whose names matches the package name of the BOM, for example, for a BOM with a package name com.example.ordermodel the factory class would be called com_example_ordermodel_Factory, and there would be methods in the factory called createClassname for each class in the BOM. For example if the BOM contained Classes called Order, OrderLine, and Customer, there would be the following factory methods:

  • com_example_ordermodel_Factory.createOrder()
    com_example_ordermodel_Factory.createOrderLine()
    com_example_ordermodel_Factory.createCustomer()

If the scriptingguide BOM contains a sub-package called ordersystem, there would be a factory created for the classes in that sub-package. Creating objects for classes in the sub-package would be done in a similar way to creating objects in the top-level package, for example:

order = com_example_scriptingguide_ordersystem_Factory.createOrder();

the name of the factory contains the package and sub-package hierarchy in its name, separated by "_".