πŸ”₯πŸ’£πŸ”₯ Salesforce B2C Commerce Developer Certification Exam Practice Test Buy

System's data with SFCC Digital Script API

Introduction

All requires we have seen in previous chapters of SFRA course that starts with dw/ means that are part of the Salesforce Commerce Cloud API. we don't have access to the source code. In case you need details, functions, attributes about any of them, refer to the official documentation.

DW Requires example

The use will them to access data about the system, such as: products, catalogs, prices, etc. You can access the API from both controllers and ISML templates for expressions or inside <isscript> tags.

API Packages

The Digital Script API is organized in packages, just like Java. Unlike Java, inheritance is not possible from these classes or packages when you create a script. You can only use the properties and methods of these classes in your scripts.

In this API, the TopLevel package is the default package. It is similar to java.lang in Java. It does not need to be imported into scripts. It provides standard ECMAScript classes and extensions, such as: Error, Date, Function, String, Math, Number, XML.

The TopLevel.global class contains many of the common constants and properties used in scripts. Some properties are: customer, request and session.

In these packages there are many classes that end with Mgr: These classes, for example, dw.catalog.ProductMgr, retrieves instances of business objects related to the package they belong to.

For example, use ProductMgr.getProduct(String id) to get a product using a unique identifier. The method returns a Product instance which you can use to find information about the product. This pattern is repeated for all Managers.

The table below shows the description of some of these packages. You don’t need to remember everything, just use it as a reference for future needs:

eCommerce API PackagesDescription
dw.campaignFor campaign and promotions.
Classes: PromotionMgr, Campaign, Promotion, SourceCodeGroup, etc.
dw.catalogFor catalog, product, and price book.
Classes: CatalogMgr, Category, Product, Recommendation, PriceBook, etc.
dw.contentFor non-product content management.
Classes: ContentMgr, Content, Folder, Library, etc.
dw.customerFor customer profile and account.
Classes: CustomerMgr, Customer, Profile, ProductList, OrderHistory, etc.
dw.orderFor orders, including: basket, coupons, line items, payment, shipment
Classes: Basket, Order, ProductLineItem, ShippingMgr, TaxMgr, etc.

Generic API PackagesDescription
dw.cryptoEncryption services using JCA; DES, Triple-DES, AES, RSA, etc.
Classes: Cipher, MessageDigest
dw.ioInput and output.
Classes: File, FileReader, CSVStreamReader, XMLStreamReader, etc.
dw.objectSystem base classes and custom objects.
Classes: PersistentObject, ExtensibleObject, CustomObjectMgr, etc.
dw.rpcWeb services related APIs.
Classes: WebReference, Stub
dw.systemSystem functions.
Classes: Site, Request, Session, Logger
dw.utilSimilar to the java.util API: collections, maps and calendar classes.
dw.valueImmutable value objects.
Classes: Money, Quantity
dw.webWeb-processing.
Classes: URLUtils, Forms, Cookie, HttpParameterMap, etc.

You can also embed SFCC Script API calls into ISML by using the <isset> tag as you can see below:
<isset name="PaymentInst" value="${require('dw/order/PaymentInstrument');}" scope="page" />

Inside of the <isscript> tag, you can fully qualify every class you want to use or you can import any packages at the top of the script:

<isscript>
    var CatalogMgr = require('dw/catalog/CatalogMgr');
    var siteCatalog = CatalogMgr.getSiteCatalog();
    ...
</isscript>

Be aware, this practice of adding logic inside ISML templates is NOT RECOMMENDED, so use it with moderation. Instead, always try to add any logic in the Controller.

πŸ“ Exercise: Practical Exercises

Create a Controller that requires needed customer Digital Script API and send to a new template.isml if current customer is authenticated or not.

Digital Script API. Customer package
πŸ”₯πŸ’£πŸ”₯ Salesforce B2C Commerce Developer Certification Exam Practice Test Buy