Creating Custom Business Objects with Scripts

You can add custom business objects through B2C Commerce JavaScript. This method of adding custom business objects offers the most flexibility and control.

Use the dw.object package, which contains these classes:

  • CustomObject
  • CustomObjectMgr
Note: Refer to the API documentation for further details.

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.


WARN: Invalid access to orm object PK{ 
com.demandware.beehive.core.capi.domain.PersistentObjectPOKey[com.demandware.beehive.xcs.internal.product.ProductPO] 
[cdtVV9aacbTPYaaacOLbFjKMNQ]} from thread 
{Thread[RequestHandlerServlet|19915763|Sites-SiteGenesis-Site|TestAPP15854- 
Test|PipelineCall|D1uLw5zjszPGltCRMgqKwpyctSD1ayaYcfM=,5,main]} : 
ORMObject context is invalid.

The second function, executeX(), doesn't cause an error.

/** 
* @input inProduct             : Object
* @output outProduct           : Object
*/    
      
importPackage( dw.system );
importPackage( dw.value );
importPackage( dw.util );
importPackage( dw.catalog );

//
// this function doesn't work, because it uses a wrapper object
//
function execute( pdict : PipelineDictionary ) : Number
{
    var PIPELET_NAME : String = "TestAvailability.ds";
    try
    {	
        var container = pdict.inProduct;
        
        if (empty(container))
        {
            var product = ProductMgr.getProduct("000906014545");
	        var container = new containerObject();
	        container.setProduct(product);            
        }
        
        var availability = container.getProduct().getAvailabilityModel().getAvailabilityStatus();
        pdict.outProduct = container;
    }
    catch (e)
    {
    	Logger.error("foo: " + e);
        return PIPELET_ERROR;
    }
	
    return PIPELET_NEXT;
}

function containerObject()
{
   var _product = null;
   
   this.setProduct = function(product)
   {
      _product = product;
   }	
   
   this.getProduct = function()
   {
       return _product;
   }
}

//
// this function works fine, because it doesn't use any wrapper object
//
function executeX( pdict : PipelineDictionary ) : Number
{
    var PIPELET_NAME : String = "TestAvailability.ds";
    try
    {	
        var product = pdict.inProduct;
        
        if (empty(product))
        {
            var product = ProductMgr.getProduct("000906014545");            
        }
        
        var availability = product.getAvailabilityModel().getAvailabilityStatus();
        pdict.outProduct = product;
    }
    catch (e) 
    {
	return PIPELET_ERROR;
    }
	
    return PIPELET_NEXT;
}

Creating Custom Objects

To use this structure, you would first import the package:

importPackage( dw.object )

Then you would access the contents of the package, including all classes and methods as follows:

Var co : CustomObject = CustomObjectMgr.getCustomObject(type, keyValue)

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.

static createCustomObject(type : String, keyValue : Number) : CustomObject
Note: The type declaration is used for code completion within the Studio environment, and ignored in the code.