Create custom attributes for an existing system object. For example, create an object type of ProcessingDays to hold an integer that represents the number of days required to process the order.
If your data is especially complex, create a custom business object to handle the transaction. Creation of custom business objects can significantly impact overall performance, is typically unnecessary. Avoid creating a custom business object if there’s another possible solution.
Define custom business objects that require no explicit database schema changes. B2C Commerce uses the concept of metadata, where you first declare the data element, and then access it.
There are two parts to adding custom business objects to the B2C Commerce environment: definition and creation. You must use Business Manager to define a custom object. Use Business Manager, B2C Commerce JavaScript, or import pipelets to create the data for the custom object.
Create a Custom Business Object Definition
Use Business Manager to create a custom business object definition. A custom object definition is available for use in all storefronts defined for the organization.
Click App Launcher and then select Administration > Site Development > Custom Object Types.
To add a definition, click Add.
Define your custom object and a key attribute.
The attribute stores a unique key for every custom object of this type.
For example, create a ProcessingDays object to inform customers how long it takes for their order to ship. Add a ShipmentNo key attribute.
Click the Attribute Definitions tab.
The system automatically creates three attributes for you: UUID, creationDate, and lastModified. These attributes store system data and can’t be removed.
To add more attributes, click New.
For example, add the attributes OrderProcessing, Shipping, and TotalShippingTime.
To save your changes, click Apply.
To return to the previous page, click Back.
To define attribute groups, click the Attribute Grouping tab.
Attribute groups are shown in Business Manager and let you create or edit objects created through scripts.
To add an attribute group, click New.
For example, create an attribute group called ShipmentTime.
To add attributes to the group, click Edit.
For example, add the attributes OrderProcessing, Shipping, and TotalShippingTime.
Create a Custom Object Instance with Business Manager
Use Business Manager to create and manage an instance of a custom object. Instances of a custom object can be site-specific or organization-wide.
Store a custom object instance globally to make it available to the entire organization and all sites. Store instances locally per site. The custom object type definition is always available to the entire organization (to all sites).
Click App Launcher and then select Merchant Tools > site > Custom Objects > Custom Object Editor.
Create an instance of a custom object.
Assign values to the attributes.
For example, assign OrderProcessing a value of 5, Shipping a value of 6, and TotalShippingTime a value of 11.
Creating Custom Business Objects with Scripts
Add custom business objects through B2C Commerce JavaScript. This method offers the most flexibility and control.
Use the dw.object package, which contains these classes:
CustomObject
CustomObjectMgr
Refer to the API documentation for further details.
Note
Sometimes you want to extend or modify data for an existing B2C Commerce object, such as a product, within a script using a wrapper object that includes a reference to a B2C Commerce object. If you want this information to persist in a pipeline after an interaction continue node, store it in a custom attribute for the object or a separate custom object. The context of the object is changed after an interaction continue node or interaction node and any custom script object you create, even one stored in the Pipeline Dictionary, doesn’t let you access any objects referenced as part of that custom script wrapper object.
Script Function with Reference to Product Object
The following script shows two functions. The first function, execute(), causes an error if the custom wrapper object (containerObject()) is stored in the Pipeline dictionary before an interaction continue node and then the product reference is accessed after the interaction continue node. The error is similar to the following.
1?WARN: Invalid access to orm object PK{2com.demandware.beehive.core.capi.domain.PersistentObjectPOKey[com.demandware.beehive.xcs.internal.product.ProductPO]3[cdtVV9aacbTPYaaacOLbFjKMNQ]} from thread4{Thread[RequestHandlerServlet|19915763|Sites-SiteGenesis-Site|TestAPP15854-5Test|PipelineCall|D1uLw5zjszPGltCRMgqKwpyctSD1ayaYcfM=,5,main]} :6ORMObject context is invalid.
The second function, executeX(), doesn’t cause an error.
1/**2 * @input inProduct : Object3 * @output outProduct : Object4 */56importPackage(dw.system);7importPackage(dw.value);8importPackage(dw.util);9importPackage(dw.catalog);1011//12// this function doesn't work, because it uses a wrapper object13//14function execute(pdict: PipelineDictionary): Number{15 var PIPELET_NAME: String = "TestAvailability.ds";16 try{17 var container = pdict.inProduct;1819 if(empty(container)){20 var product = ProductMgr.getProduct("000906014545");21 var container = new containerObject();22 container.setProduct(product);23}2425 var availability = container.getProduct().getAvailabilityModel().getAvailabilityStatus();26 pdict.outProduct = container;27}catch(e){28 Logger.error("foo: " + e);29 return PIPELET_ERROR;30}3132 return PIPELET_NEXT;33}3435function containerObject(){36 var _product = null;3738 this.setProduct = function(product){39 _product = product;40};4142 this.getProduct = function(){43 return _product;44};45}4647//48// this function works fine, because it doesn't use any wrapper object49//50function executeX(pdict: PipelineDictionary): Number{51 var PIPELET_NAME: String = "TestAvailability.ds";52 try{53 var product = pdict.inProduct;5455 if(empty(product)){56 var product = ProductMgr.getProduct("000906014545");57}5859 var availability = product.getAvailabilityModel().getAvailabilityStatus();60 pdict.outProduct = product;61}catch(e){62 return PIPELET_ERROR;63}6465 return PIPELET_NEXT;66}
Creating Custom Objects
To use this structure, you would first import the package:
1importPackage(dw.object);
Then you would access the contents of the package, including all classes and methods as follows:
In this case, getCustomObject is the method used by the CustomObjectMgr class to return a new custom object where the type (“ExampleType”) is a string and the keyValue (123) is a number.
The following script returns a new custom object instance of the specified type, using the given key value.