Intellij SFCC ships the B2C Commerce Script API as TypeScript typings. The IDE reads them like any other library, so every dw class, method and property has completion, parameter info and quick documentation, with no download and no node_modules
require('dw/…'), then the required class types every member call after itThe B2C Commerce API library
When a cartridge is linked, the plugin adds a library called B2C Commerce API to the project. You see it under External Libraries in the Project view
| File | Covers |
|---|---|
dw.d.ts | The Script API: every dw.* package, the script globals and top-level functions |
ocapi.d.ts | OCAPI document types such as BasketDocument and OrderAddressDocument, for OCAPI hook scripts |
sfraServer.d.ts | The SFRA server module: server.get, server.post, server.use, server.append, server.prepend, server.replace, server.extend, and the req, res and next of a route |
The typings cover the Script API up to version 24.4. Newer classes or methods that are not in the typings show as unresolved, but they still run on the instance
Require dw classes
Type require('dw/ and completion lists the Script API packages and classes. Each item shows its package on the right, for example dw.catalog
var ProductMgr = require('dw/catalog/ProductMgr');
var Transaction = require('dw/system/Transaction');
var product = ProductMgr.getProduct('25502228M');
// product is typed as dw.catalog.Product, so product.getAvailabilityModel() completesThe variable gets the class type, so every call after it completes with parameter info. Cmd+Click on the path opens the class in the typings, with the Salesforce description of each member
Script globals
These names work in every server-side script without a require. Completion marks them (global)
| Global | Type or use |
|---|---|
session | dw.system.Session |
request | dw.system.Request |
response | dw.system.Response |
customer | dw.customer.Customer |
slotcontent | dw.campaign.SlotContent, in slot templates |
webreferences, webreferences2 | SOAP web references, see below |
empty | Checks for null, undefined, an empty string, array or collection |
PIPELET_NEXT, PIPELET_ERROR | Return values of a pipeline script |
module, exports, require | CommonJS, including module.superModule, see Module Resolution |
The legacy importPackage, importClass and importScript functions are typed too, so older DemandwareScript code doesn't show them as unknown
Document your types
The IDE infers most types by itself. Where it can't, for example a parameter of a helper function, tell it with JSDoc. Use the full dw. name of the class, not a free-form name
/**
* @param {productSearchModel} productSearch - Product search instance
* @param {Category} rootCategory - Catalog's root category
* @return {string} - Sort rule ID
*/
function getSortRuleDefault(productSearch, rootCategory) {
var category = productSearch.category ? productSearch.category : rootCategory;
return category.defaultSortingRule ? category.defaultSortingRule.ID : null;
}/**
* @param {dw.catalog.ProductSearchModel} productSearch - Product search instance
* @param {dw.catalog.Category} rootCategory - Catalog's root category
* @return {string} - Sort rule ID
*/
function getSortRuleDefault(productSearch, rootCategory) {
var category = productSearch.category ? productSearch.category : rootCategory;
return category.defaultSortingRule ? category.defaultSortingRule.ID : null;
}With the dw. types in place, productSearch. and category. complete, and custom attributes complete after .custom., see SFCC Contracts
SOAP web references
Scripts reach a SOAP service through webreferences2.<name>, where <name> is a WSDL file in cartridge/webreferences2. The instance generates the Java API for a WSDL the first time it is used. To get that generated API into your project:
Select the WSDL
Right-click one or more .wsdl files in the Project view and choose Download SOAP Web Service API , or Download SOAP Web Service API to Folder to pick the target folder
Let it download
The plugin asks the active connection for the generated archive, downloads it and unpacks it. Without a folder it unpacks two levels above the WSDL, which is the cartridge folder for a file in cartridge/webreferences2
If the instance hasn't generated the API yet, an error explains that you need to run webreferences2.<name> on the instance once, with a link to the Salesforce SOAP web services guide. The same works for legacy webreferences
Troubleshooting
| Symptom | What to do |
|---|---|
| B2C Commerce API is missing from External Libraries | Link a cartridge. The library is added only to projects with linked cartridges |
| A new Script API method shows as unresolved | It is newer than the bundled typings. The code still runs on the instance |
require('dw/...') lists nothing | Check the license and that the file is server-side code inside a cartridge folder |
| A parameter's members don't complete | Add a JSDoc @param with the full dw. type |
| Download SOAP Web Service API is missing | Every selected file must be a .wsdl file |