Custom Objects
Defining Custom Objects
You will learn how you can store data in a custom object so it can be persistent.
Custom objects (COs) extend the Demandware data model: they are basically a new table in the database where you specify the primary key and storage attributes (columns) that suit your business needs.
IMPORTANT NOTE
You should always consider first if a Demandware System object (Product, Catalog, etc) can be used instead of creating a custom object. Please do not abuse (滥用) the fact that you can create custom objects: they are best used to store static data (like configuration parameters), not for uncontrolled amounts of data (like analytics). Custom objects searches can be slow if the data is large. You should consider data growth and cleanup in your COs. Demandware Platform Governance has quotas (配额) around custom object API usage and data size which will be enforced in the future.
Custom object data types are created at the organization level and are therefore available for use in all storefronts within the organization. You use two different Business Manager modules to define and manage your custom objects:
Custom Object Definitions: allows naming, primary key and column specification.
Located under Administration ⇒ Site Development
Custom Object Editor: allows instance creation and editing.
Located under Site - <site> ⇒ Custom Objects ⇒ Custom Object Editor.
When defining the CO you specify the storage scope of the instances: site or organization. Organization custom objects can be used by any site, whereas(然而) site custom objects are created by one site and cannot be read by another. The CO type itself is always available to the entire organization. Also, you can specify if you want CO instances to be replicable: this means they can be copied from Staging to Production as part of the replication process.
How-To : Create Custom Object Types
-
Log into Business Manager.
-
Click Administration => Site Development => Custom Object Definitions
-
Click the New button to create a new custom object type.
-
Fill in the required fields for the custom object type
-
Click Apply.
-
Click the Attribute Definitions tab. You will notice default values created with your custom object type. These values cannot be changed once they are created
-
Create the attributes (values you wish to capture in the table) by clicking the ‘New’ button.
-
Specify a unique name in the ID field and then select the type of data being entered in the attribute from the Value Type drop-down menu
-
Click the Apply button.
-
When you are finished, click the ‘Back’ button to add another attribute.
-
When you are finished adding attribute definitions, you will need to create an Attribute Group. Click the ‘Attribute Grouping’ tab.
-
Enter a name for your grouping in the ID field and a name in the Name: field.
-
Click the Add button. You will need to add the field attributes next.
-
Click the Edit link to add field attributes to the group.
-
Click the 省略号 next to the ID: field to select field attributes.
-
Select the attributes you wish to add from the list by clicking in the checkbox next to each one. Then click the ‘Select’ button.
To create a custom object instance manually in BM:
-
Log into Business Manager.
-
Click the site you wish to manage the custom objects for.
-
Click Custom Objects -> Custom Object Editor.
-
At the next window, select the custom object type you wish to manage from the drop-down list.
-
To create a new custom object, click the New button.
-
Enter data in each of the required fields and then click the Apply button.
-
You have now created a custom object. Click the Back button to exit the custom object editor.
Using Demandware Script to Create Custom Objects
The Demandware Script API provides the following classes in the dw.object package:
CustomAttributes: attributes defined by a user in the Business Manager to extend a system object or CO. Accessible via the syntax: co_instance.custom.attribute
CustomObject: represents an instance of a CO
CustomObjectMgr: allows the creation of CO instances
PersistentObject: allows persistent (持久) storage
ExtensibleObject: allows custom attributes to be added (可扩展)
This is the inheritance tree for the CustomObject type:
Object –> dw.object.PersistentObject -> dw.object.ExtensibleObject -> dw.object.CustomObject (or dw.object.SystemObject)
This inheritance tree means that COs are persisted in the database and can have custom attributes added by an administrator or programmer in Business Manager. As you inspect (观察) the Demandware documentation you will see that commonly used classes like dw.catalog.Product, dw.system.SitePreferences and many others share this inheritance tree: objects of these class types are saved in the database and can be extended to store extra attributes.
The following usage of the CustomObjectMgr class allows creation of an instance of a CO by providing the CO type and the primary key:
CustomObjectMgr.createCustomObject("NewsletterSubscription", UUIDUtils.createUUID());
This will create an instance with a system generated, unique PK. You could also use:
CustomObjectMgr.createCustomObject("NewsletterSubscription", args.email));
This assumes that the args.email value should be a unique string every time a CO is created. Otherwise, a duplicate PK error will occur.
Implicit Database Transaction Handling (隐式数据库事物处理)
Database transaction handling in Demandware is handled by one of two ways: (隐式和显式)
Implicit – a transactional pipelet automatically begins a transaction. The transaction is automatically committed to the database when the pipelet returns PIPELET_NEXT, otherwise the transaction is rolled back.
Explicit – the transaction is controlled via properties of the transition nodesin the pipeline.
For implicit transactions to work, a pipelet that performs changes to the database must set its Transactional property equal to true. This becomes visible as a black "T" on the pipelet node in the pipeline.
If such a transactional pipelet is executed, a database transaction will be started automatically and will be committed implicitly at the end of this pipelet’s execution if the execution is successful.
Create a custom object programmatically
-
Create a custom object type in BM before creating a custom object programmatically. If you have not already done so, create your 2 custom object type as defined in the previous process step.
-
Create a script that uses the dw.object.CustomObjectMgr class to create a custom object:
importPackage( dw.system );
importPackage( dw.object );
function execute( args : PipelineDictionary ) : Number
{
var co : CustomObject =
CustomObjectMgr.createCustomObject("NewsletterSubscription", args.email);
co.custom.firstName = args.firstName;
co.custom.lastName = args.lastName;
args.subscription = co;
return PIPELET_NEXT;
}
Notice the use of the custom qualifier for all the custom attributes that you defined in the CO definition: without this qualifier the code will fail since the class dw.object.CustomObject does not have any standard attribute named firstName.
-
Edit the properties of the pipelet:
-
) Make it Transactional so it commits to the database.
-
) Assign all the corresponding form values to the pipelet inputs.
-
) Assign the subscription output to a Subscription object.
-
) Verify that all properties are correctly defined:
-
Show Confirmation to the User
-
Modify the subscription confirmation template to use the Subscription object from the pdict:
${pdict.Subscription.custom.firstName}
Troubleshooting
-
Check to see if you imported dw.object package
-
Check to see if you have marked the script node as Transactional
-
Re-check the configuration of the script in Step 3d
-
Check if theNewsletterSubscription Custom Object type exists in the BM (‘N’ capital, ‘l’ small, ‘S’ capital).
-
Also check if string attributes “firstName”, “lastName” and “email” exist in it and are a part of an Attribute group.
Custom Logging 自定义写日志
The Demandware platform supports custom logging using log categories and severity levels as defined by the Apache log4j open source project
Log4j supports multiple severities(多重严重性) and categories of logging to allow the developer to capture debug messages at different levels of granularity(粒度). The severity levels are:
Debug < Info < Warn < Error < Fatal
If custom logging is enabled for a certain severity level, then it is enabled for higher severity levels as well. Fatal and Error are always enabled and cannot be turned off
As for categories, the programmer can define as many levels of categories and subcategories as needed. Demandware does not impose(强行) a certain categorization; you decide how you want to organize your logging categories. For example:
-
product
-
product.import
-
product.import.staging
If logging is enabled for a category (say product), all its subcategories will also be enabled.
Examples
If Warn logging is enabled for "product":
-
Warn, Error and Fatal errors are logged for "product" and all its sub-categories.
If Warn logging is enabled for "product" and Debug for "product.import"
-
Warn, Error and Fatal messages are logged for "product" and all its sub-categories.
-
Debug and Info are logged for "product.import" and all its sub-categories.
To write to a custom log, you will need to use the
dw.system.Logger.getLogger() factory method. This method creates a Logger object for a specified category
var logger : Logger = Logger.getLogger( “category" );
logger.debug("Input params received in pipelet firstName: {0}\n lastName: {1}\n email: {2}",
args.firstName, args.lastName, args.email);
try {
… do something…
return PIPELET_NEXT;
} catch (e) {
logger.warn(“error description: {0}", e.causeMessage );
return PIPELET_ERROR;
}
You can use the Logger object to write a message for a specific severity level: Logger.error(String msg).
The message uses the Java MessageFormat API, so you can specify placeholders. Typically these messages are not localized since they are read internally by site administrators, but they can be.
Enabling Custom Logging
In order to write to log files you will need to enable Custom Log Settings under Administration => Operations in BM:
Enable Custom Logging in Business Manager
-
Log into Business Manager.
-
Click Administration -> Operations -> Custom Log Settings.
-
Create a log category: type it on the field under a given severity(严重性), then, click Add.
-
Enable the checkbox next to the log category you wish to write to.
-
Click the Apply button.
-
Click Log Debug to File to allow debug messages to be written to a log for the next 10 minutes. Usually Debug and Info messages get written to memory only, and are visible via the Request Log tool.
-
Run the pipeline you wish to debug
-
Review the custom log file in Business Manager by clicking
Administration –> Site Development -> Development Setup -> Log Files.
-
Open the log file that was just created. Search for the file by date. The custom log file will be named something like
‘customdebug-177.aaaq.demandware.net-appserverxxxx.log’ .
相关推荐
Master Data Governance for Custom Objects自定义对象的主数据管理 Master Data Governance for Custom Objects是SAP S/4HANA系统中的一个重要组件,负责管理自定义对象的主数据。它提供了一个集中化的平台,用于...
IBM FileNet 讲义 CE API 系列之 Custom Objects.ppt
10 creating custom objects: Having It Your Way with Custom Objects ————449 11 kill bugs dead: Good Scripts Gone Wrong ————485 12 dynamic data: Touchy-Feely Web Applications ————537
Chapter 41: Functions and Custom Objects. Chapter 42: Global Functions and Statements. Part V: Putting JavaScript to Work. Chapter 43: Data-Entry Validation. Chapter 44: Scripting Java Applets and...
components, such as custom objects and fields, workflow rules, Visualforce pages, and Apex classes and triggers, using the point-and-click tools of the Web interface. Simple changes can be implemented...
- **Data Components**: Custom objects, fields, and relationships are central to storing and organizing data. - **Business Logic Components**: Formula fields, validation rules, workflow rules, and ...
- **Extended Object Detection:** Beyond face detection, the framework supports detecting other objects like barcodes, text, and even custom objects defined by the developer. #### PDFKit - **PDF ...
##### 1.1 Implementing and Using Custom Objects(实现和使用自定义对象) 本章讲解了如何创建自己的类,并实现自定义的对象。这包括了如何继承自已有的类,以及如何覆盖父类的方法来实现特定的功能。此外,本章...
2. 生成和管理用户自定义对象(User Defined Objects 或 Custom Objects),包括管理它们与 U 对象的相关性,提供一种刷新和显示用户自定义对象的方法; 3. 提供反映第三方应用软件的 UG 图形界面本地化方法; 4. ...
- **Understanding Relationship Names, Custom Objects, and Custom Fields**:自定义对象和字段在关系查询中的作用。 - **Understanding Query Results**:理解查询结果可以帮助更好地分析数据。 - **Lookup ...
4. **Custom Objects**:讲解如何定义和操作自定义对象,以满足特定业务需求,如用户定义类型和元数据模型。 5. **Documents**:深入讨论文档对象的管理,包括文档的上传、下载、更新及删除。 6. **Properties**:...
4. **Custom Objects**:介绍如何定义和操作自定义对象,以适应特定的业务逻辑和数据模型。 5. **Documents**:详细阐述文档的创建、修改、查询和版本控制,以及如何处理文档生命周期的不同阶段。 6. **Properties**...
- **Custom Objects**:讲解了如何创建自定义对象来扩展 ASP 的功能。 - **Components**:讨论了如何使用组件(如 COM 组件)来增强 ASP 的能力,这些组件可以提供复杂的功能和服务。 ##### 4. **Application ...
• Using Parallel LINQ to Objects (PLINQ) to optimize queries for multi-core processors, and how to build custom parallel query operators • Integrating the best LINQ to Objects patterns into your day...
Custom Objects Sample Content Sample Overview QuickBlox is Communication as a Service provider. The platform provides chat using the XMPP protocol, WebRTC signalling for video/voice calling and an ...
领域对象是业务逻辑中的核心实体,它们直接对应于 Salesforce 中的 Custom Objects 或标准 Objects。DTOs,另一方面,是为了在系统之间传递数据而创建的简单模型,通常不含任何业务逻辑,只是数据的容器。SF-Apex-...