Salesforce-B2C-Commerce-Cloud-Developer Practice Test

Salesforce Spring 25 Release
202 Questions

A developer uses hooks for an extension point. Which n true for running multiple hooks for an extension point?

A. It is possible to register multiple modules to call for an extension point in a singlehooks.json file

B. It is possible to control the order in which registered modules are called.

C. If you call multiple modules, only the first hook called returns avalue

B.   It is possible to control the order in which registered modules are called.

Explanation:

In Salesforce B2C Commerce, when using hooks for an extension point, A is true: multiple modules can be registered to call for an extension point in a single hooks.json file. This allows different cartridges to contribute to the same extension point. However, the order of execution is not guaranteed or controllable (B is false), and all registered hooks can return values unless overridden, so only the first hook returning a value is not inherently true (C is false).

Reasoning:

A. It is possible to register multiple modules to call for an extension point in a single hooks.json file.
True. In B2C Commerce, the hooks.json file in a cartridge (e.g., cartridge/scripts/hooks.json) can define multiple entries for the same extension point.
For example:
{
"hooks": [
{
"name": "dw.order.calculateTax",
"script": "custom/taxCalc1"
},
{
"name": "dw.order.calculateTax",
"script": "custom/taxCalc2"
}
]
}

This registers taxCalc1 and taxCalc2 to handle the dw.order.calculateTax extension point, allowing multiple modules to contribute.

B. It is possible to control the order in which registered modules are called.
False. The order of hook execution is not deterministic and depends on the cartridge path and registration order in hooks.json. B2C Commerce does not provide a built-in mechanism to enforce or control the sequence of hook calls for the same extension point. Hooks are executed in the order they are encountered, but this is not guaranteed or configurable.

C. If you call multiple modules, only the first hook called returns a value.
False. When multiple hooks are registered for an extension point, all hooks are called, and each can return a value. The dw.system.HookMgr.callHook() method aggregates the results (e.g., into an array), and the calling code can process all returned values. The first hook’s return value does not inherently override others unless the implementation is designed to stop processing after the first non-null result (which is not a default behavior).

Implementation Note:
To use multiple hooks, ensure each module (e.g., taxCalc1.js, taxCalc2.js) exports the required function:
// custom/taxCalc1.js
module.exports = {
calculateTax: function (basket) {
return { tax: 10 }; // Example return value
}
};

// custom/taxCalc2.js
module.exports = {
calculateTax: function (basket) {
return { tax: 5 }; // Additional return value
}
};

Calling dw.system.HookMgr.callHook('dw.order.calculateTax', 'calculateTax', [basket]) will return an array of results (e.g., [{tax: 10}, {tax: 5}]), allowing the calling code to handle multiple outputs.

Reference:
Salesforce B2C Commerce Documentation: Hook Manager details multiple hook registrations and result aggregation.
Trailhead Module: “B2C Commerce Developer” module on “Customizing Business Logic” covers hook usage.

Exam Tip:
For the Salesforce B2C-Commerce-Developer exam, understand that hooks.json supports multiple registrations for an extension point, but the order is not controllable, and all hooks can contribute values. Be cautious of options suggesting strict ordering or single-value returns, as these misrepresent hook behavior.

Salesforce-B2C-Commerce-Cloud-Developer Practice-Test - Home Previous
Page 34 out of 202 Pages