Sknoe / Notes
SFRA Models and ViewModels: How Data Gets Ready for ISML
Follow product data from a controller into a ViewModel, view data, and an ISML template—and learn where to debug when a value is missing.
When I moved from Angular and WordPress to Salesforce Commerce Cloud, one of the hard parts was figuring out where storefront data came from. In the first SFRA guide, I followed a request from the controller to pdict and ISML. That explained where the template gets its values, but left me with another question: who shapes those values along the way?
This article follows one piece of product data through that next part of the flow. We’ll look at what a model or ViewModel does, how its output becomes view data, and how to trace the value into an ISML template. You don’t need to know every SFRA convention first; we’ll follow the handoffs one at a time.
Why not pass the platform object straight to ISML?
A Commerce script API object is a platform object with platform-specific behavior. A storefront page usually needs a predictable set of display values: perhaps a product name, formatted price, image URL, and availability text. A model can turn platform data into a JavaScript object that describes what the page needs, while keeping the template focused on presentation.
This separation also gives you a useful debugging boundary. If the source product has a name but the template receives no `product.name`, inspect the transformation and the handoff to view data before changing the ISML expression.
Trace one product value through the route
A useful way to read an unfamiliar product page is to follow one field from end to end. Start at the route and find where it gets the product. Then find whether it constructs a model or ViewModel, what properties that object exposes, and how the controller adds it to view data. Finally, check which template renders and what property path the template reads.
Route handles request
A controller route receives the storefront request and coordinates its middleware.
Commerce data is retrieved
The route or a helper obtains the platform data needed for the page.
Model shapes values
A model or ViewModel selects and prepares data for storefront use, when one is used.
Controller sets view data
The response receives the page data, for example through res.setViewData().
ISML reads pdict
The selected template reads the values made available for rendering.
Server returns HTML
B2C Commerce renders the template and sends the resulting response.
The exact call sequence varies by route. The point of the diagram is to give you places to look, not to imply that every page uses the same model or helper.
A small example: shape data before rendering
This simplified JavaScript example shows the boundary between preparing page data and rendering it. `sourceProduct` stands in for platform data; it is not a complete B2C Commerce API example. Real SFRA models may call platform APIs, format values, and handle missing or unavailable data.
var sourceProduct = {name: 'Everyday Bag',ID: 'bag-001',available: true};function createProductViewModel(sourceProduct) {return {name: sourceProduct.name,sku: sourceProduct.ID,available: sourceProduct.available};}var productView = createProductViewModel(sourceProduct);var viewData = { product: productView };console.log(viewData.product.name);
This plain JavaScript example runs on its own; `sourceProduct` stands in for data retrieved by an SFRA route. The model returns only the values the page needs, and `viewData` puts that object under the `product` key. In an SFRA route, the controller can pass that data with `res.setViewData(viewData)` and render the template. ISML can then read `${pdict.product.name}`. If the model returned `{ displayName: ... }` instead, the template would need to read `pdict.product.displayName`; the names must agree across the handoff.
Under the hoodWhy models often create plain JavaScript objects
Looking closer
Salesforce describes SFRA models as converting objects returned by B2C Commerce script APIs into JSON-style data for the storefront, and notes that a ViewModel can combine data from multiple script objects. This makes the model a useful place to choose the fields a page depends on. It does not mean every route needs a separate model: simple routes may pass straightforward values directly. Keep platform access and any required business rules in the appropriate server-side code, and avoid assuming that a model is merely a renamed copy of the original object.
Where should you look when a template value is missing?
Suppose `${pdict.product.name}` renders empty. Work backward through the handoffs:
1. Confirm that the route renders the template you are inspecting.
2. Find where the route obtains the product data.
3. Inspect the model or ViewModel and confirm the returned object has a `name` property.
4. Check the key passed into `res.setViewData()` or `res.render()`.
5. Compare that object shape with the path used in ISML.
6. Check middleware steps that may add, replace, or merge view data before rendering.
Common questions about SFRA models and ViewModels
A model or ViewModel prepares data for the storefront; it does not replace the controller route or the ISML template. Keeping those jobs distinct makes the request flow easier to follow: the route coordinates, the model shapes data when needed, view data carries it to rendering, and the template presents it.
Continue tracing the SFRA request
Controllers, pdict, res.render(), and ISML
The request-to-render foundation
Start with the route, view data, template, and HTML response that this article builds on.
Controller middleware
Adds ordered steps to a route
See how middleware can retrieve or add view data before a response is rendered.
Cartridge paths
Affect which code and template implementation is resolved
Learn why the same relative template name can be provided by different cartridges.
ISML includes and decorators
Compose reusable page markup
Explore how a rendered page can be assembled from templates beyond one file.
When reading an SFRA page, pick one value the template needs and trace it backward. That habit connects controllers, models, view data, and ISML into one debuggable flow.
Did this click?
Tap the bulb if this explanation helped.
Further reading
Continue with a complete SFRA example
To see these data-preparation ideas used in a shopper-facing feature, read Build Add to Cart: A Backend-to-Frontend SFRA Journey.
Interview questions
What is a ViewModel in SFRA?
A ViewModel prepares data for rendering, often by converting one or more B2C Commerce script API objects into JavaScript data suited to the storefront. It may combine values and apply storefront logic; not every route needs a custom ViewModel.
Why does SFRA use models or ViewModels between platform data and a template?
They can select, combine, and shape platform data into a predictable object for the storefront, so the template can focus on presentation and the route can pass an explicit data shape.
How does data prepared by a model become available as pdict in ISML?
The route adds the prepared data to the response's view data or passes it when rendering. When the ISML template is rendered, the available view data is exposed through pdict, using the keys supplied by the route.
A model returns product.displayName, but ISML reads pdict.product.name. What should you check?
The property paths do not match. Check the model's returned object and the key passed into view data, then either expose the expected name property or update the template to read the intended displayName property.
Does every SFRA route need a custom ViewModel?
No. A custom model is useful when data needs to be transformed, combined, or given storefront-specific logic. A simple route can pass straightforward values directly when that keeps the code clear.
What is a good way to debug an empty value in pdict?
Trace the value backward from the ISML expression: confirm the rendered template, inspect the view-data key, check the model output, and find where the source value is retrieved. Also inspect middleware that can add or merge view data before rendering.