We are going to learn about 2 important SFCC features, which allow us to persist and retrieve data from Business Manager. The first one is called Objects and we have 2 types: System and Custom. The second one is called Site Preferences.
How do you think that Business Manager stores Products, Catalogs or Categories? How does the API work with them?
Previously you were working with Controllers, calling some classes from the SFCC API to get data back from your Business Manager. These objects that you are manipulating are actually stored in Business Managers as tables.
The first type of object that can be saved into Business Manager is called System Object. To begin with, do you know are they called System objects? It’s simpler than you have been thinking: they come already out-of-the-box when you receive your sandbox.
Let’s have a look at the system objects that we are provided out-of-the-box: Go to Administration > Development > System Objects Types
Do you recognize the items pointed by the green arrows? They are some of the system object we have been working on previous chapters.
As you can see, the Product Object has almost 100 fields! But as far as we remember, we did not see that many fields when we were editing products a few modules ago. So what is going on?
Although your objects can many fields, to avoid polluting the screen we can limit which fields will be displayed when we create/edit an instance of that object. If you go back to the check any Product, you will notice that not even have of the fields we just saw are listed there.
So how do we decide which fields are displayed?
As you can see, this way we can customize all objects pages to show the fields that we want to and hide everything else that we don’t think it is necessary for that project.
One last important detail, as we mentioned at the beginning of this module that System Objects come out-of-the-box with your Business Manager. Which means you can’t create new System Objects Types. If you want a new type of Object, then we should see Custom Objects.
Custom objects enable you to extend the Salesforce B2C Commerce model to suit your business needs. They are basically a new table in the database where you specify the primary key and storage attributes (columns) that suit your business needs.
An example of Custom Object usage is a newsletter. Customers can sign up for it, but the platform does not have a system able to support it.
These subscriptions are intended for export since the platform should not be used for mass mailing campaigns. It is tempting to add the subscription data to the Profile system object, but this would imply that only registered users would be able to sign up.
To enable anyone to get a newsletter, you need to define a Custom Object to represent it.
This Newsletter Custom Object should not be replicable since subscriptions created in staging should not be copied to Production. But depending on the situation you want your Custom Object to be replicable.
You also need to consider how to clean up Custom Objects once they have been exported or after a certain expiration period. This means the creation of a cleanup batch job that should run on a schedule. You can either create custom objects using Business Manager or programmatically.
You use two Business Manager modules to define and manage your custom objects:
Before you can create a custom object instance you must first define the custom object data type in Business Manager.
Custom Objects can also store configuration parameters to integrate with external systems, avoiding the need to create multiple Site Preferences. These Custom Objects need to be replicable if the settings made in Staging are suitable for Production.
Salesforce B2C Commerce Cloud Script API provides the following classes in the dw.object package, among others:
Custom Objects persist in the database. Objects of these class types are saved in the database and can be extended to store extra attributes.
The following use of the CustomObjectMgr class enables the creation of an instance of Custom Objects by providing the Custom Object type and the primary key:
CustomObjectMgr.createCustomObject(“NewsletterSubscription”, UUIDUtils.createUUID());
One thing that we did not discuss in the System Objects section was Scope because it was not necessary at that point, but now is the moment for you to know about it.
Let’s first define what is scope. Consider Scope as the visibility of the object. Who or what can see and use it. We have here 2 options: Organization or Site
It means that Custom Objects can be created to be visible to all storefronts (Organization) or just for a specific Storefront (Site) in your sandbox. It will really depend on your project.
The Custom Object type itself is always available to the entire organization. Also, you can specify if you want Custom Object instances to be replicable.
Where you work with objects, be them System or Custom ones, there is one essential detail you must be aware of. You can retrieve objects as much as you want, but when you want to create, edit or delete an object, things are different.
A transaction provides a context for performing atomic changes to persistent business objects. Before a business object can be created, changed, or deleted, a transaction must be started and changes on the touched business objects will only be made durable when the transaction is committed.
Keep that in mind whenever you need to create, change or delete an object instance, you will need a transaction.
Transaction.wrap(function () {
...
}
We can have 2 types of transactions: Implicit and Explicit.
Implicit: It’s the example we used above. You just define the code that will be inside the transaction but without actually writing any begin or commit commands. If anything fails, the transaction will be rolled back automatically and the changes will be undone.
Explicit: On this type of transaction, you are the one responsible for defining where the transaction begins and ends.
Here you can find examples of Implicit and Explicit Transactions:
Site Preferences are another way to store and retrieve data from your sandbox and, in fact, are one of the System Object Types.
As you can see, it has many fields. A lot of things are saved as SitePreferences. Like you did with the Product System Object, try to create a new field for this object.
In order to retrieve any Site Preference using code, you only need to know the field’s ID and use the following code:
Unlock a FREE PDF with SFCC B2C Certification questions from our Udemy Course. Join our newsletter!
Check your email, you’ll receive a copy in a few seconds. If you don’t see it, please check your spam folder.
Do you like cookies? 🍪 We use cookies to ensure you get the best experience on our website. Learn more.