ワンクリックで
sfcc-script-evaluation
Guide for using the evaluate_script tool to execute JavaScript on SFCC instances via the script debugger
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Guide for using the evaluate_script tool to execute JavaScript on SFCC instances via the script debugger
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
Guide for creating and maintaining user-facing agent skills
Mandatory preflight for MCP server design/review/audit tasks. Defines production best practices for architecting, securing, testing, and operating TypeScript MCP servers, including transport design, schema validation, error handling, and deployment.
Register SFCC hooks via cartridge package.json and hooks.json. Use when adding hooks or troubleshooting hook registration.
Guide for implementing logging in Salesforce B2C Commerce scripts
Unified caching playbook for SFCC (page cache vs custom cache vs service response cache). Use this when improving performance, reducing external calls, designing cache keys/TTLs, or debugging stale cache behavior.
Guide for creating, configuring, and deploying custom SFRA cartridges in Salesforce B2C Commerce. Use this when asked to create a new cartridge, set up a cartridge structure, or work with cartridge paths.
| name | sfcc-script-evaluation |
| description | Guide for using the evaluate_script tool to execute JavaScript on SFCC instances via the script debugger |
This skill documents how to use the evaluate_script tool effectively for executing JavaScript code on an SFCC instance via the script debugger.
The evaluate_script tool allows you to execute arbitrary JavaScript code on an SFCC sandbox instance. It works by:
| Parameter | Required | Default | Description |
|---|---|---|---|
script | Yes | - | The JavaScript code to execute |
siteId | No | RefArch | The site ID (accepts either RefArch or Sites-RefArch-Site) |
locale | No | default | Storefront locale segment used when triggering the controller (tries without locale first, then retries with /{locale} if needed) |
timeout | No | 30000 | Maximum execution time in milliseconds |
breakpointFile | No | Auto-detected | Custom controller path for breakpoint |
breakpointLine | No | 1,10,20,30,40,50 | Specific line for a single breakpoint; if omitted, strategic lines (1,10,20,30,40,50) are used |
triggerUrl | No | Auto-detected | Custom storefront URL or path to trigger; site-relative paths are resolved to https://{hostname}/s/{siteId}/... |
The last expression in your script is returned as the result.
// Simple expressions
1 + 1
// Result: 2
dw.system.Site.current.ID
// Result: "RefArchGlobal"
return at the top level// WRONG - causes compilation error
return 1 + 1
// Error: "invalid return"
var assignments expecting the variable// WRONG - var declarations return null in debugger context
var result = {}; result.foo = 'bar';
// Error: Cannot set property "foo" of null
// CORRECT - inline object literal wrapped in JSON.stringify
JSON.stringify({siteId: dw.system.Site.current.ID, locale: request.locale})
// Result: {"siteId":"RefArchGlobal","locale":"en_GB"}
// CORRECT - IIFE pattern for multi-step logic
(function() {
var p = dw.catalog.ProductMgr.getProduct('25518704M');
return JSON.stringify({id: p.ID, name: p.name, online: p.online});
})()
// Result: {"id":"25518704M","name":"Pull On Pant","online":true}
require() statements// WRONG - require() returns null in debugger context
var CustomerMgr = require('dw/customer/CustomerMgr');
CustomerMgr.getSiteCustomerList()
// Error: Cannot call method "getSiteCustomerList" of null
dw.* namespace directly// CORRECT - access SFCC APIs via global dw namespace
dw.customer.CustomerMgr.getSiteCustomerList().ID
// Result: "RefArch"
// Current site ID
dw.system.Site.current.ID
// All sites
(function() {
var sites = dw.system.Site.getAllSites();
var result = [];
for (var i = 0; i < sites.size(); i++) {
result.push(sites[i].ID);
}
return JSON.stringify(result);
})()
// Site preferences (list all custom preference keys)
JSON.stringify(Object.keys(dw.system.Site.current.preferences.custom))
// Get specific site preference value
dw.system.Site.current.preferences.custom.myPreferenceName
// Get product by ID
dw.catalog.ProductMgr.getProduct('productId')
// Get product details
(function() {
var p = dw.catalog.ProductMgr.getProduct('25518704M');
if (!p) return 'Product not found';
return JSON.stringify({
id: p.ID,
name: p.name,
online: p.online,
brand: p.brand
});
})()
// Product custom attributes
(function() {
var p = dw.catalog.ProductMgr.getProduct('25518704M');
return JSON.stringify(Object.keys(p.custom));
})()
// Site customer list
dw.customer.CustomerMgr.getSiteCustomerList().ID
// Get customer by ID (requires customer number)
dw.customer.CustomerMgr.getCustomerByCustomerNumber('00001234')
// Search orders (always close the iterator!)
(function() {
var orders = dw.order.OrderMgr.searchOrders(
'orderNo != {0}',
'creationDate desc',
''
);
var result = [];
var i = 0;
while (orders.hasNext() && i < 5) {
var o = orders.next();
result.push({
orderNo: o.orderNo,
status: o.status.value,
total: o.totalGrossPrice.value
});
i++;
}
orders.close(); // IMPORTANT: Always close iterators
return JSON.stringify(result);
})()
// Current request locale
request.locale
// Result: "en_GB"
// Session ID
session.sessionID
// Current customer (if logged in)
session.customer
SFCC collections need special handling - they're not JavaScript arrays.
// Using size() and index access
(function() {
var sites = dw.system.Site.getAllSites();
var result = [];
for (var i = 0; i < sites.size(); i++) {
result.push(sites[i].ID);
}
return JSON.stringify(result);
})()
// Using Iterator pattern
(function() {
var iter = dw.catalog.CatalogMgr.getSiteCatalog().getRoot().getSubCategories();
var result = [];
while (iter.hasNext()) {
var cat = iter.next();
result.push({id: cat.ID, name: cat.displayName});
}
return JSON.stringify(result);
})()
// Arrays work normally, use JSON.stringify for readable output
JSON.stringify([1, 2, 3].map(function(x) { return x * 2; }))
// Result: [2,4,6]
// Arrow functions are supported
JSON.stringify([1, 2, 3].map(x => x * 2))
// Result: [2,4,6]
// Strings, numbers, booleans return directly
'hello'
// Result: "hello"
42
// Result: 42
true
// Result: true
// Raw objects return [object Object]
({foo: 'bar'})
// Result: [object Object]
// Use JSON.stringify for readable output
JSON.stringify({foo: 'bar'})
// Result: {"foo":"bar"}
// SFCC objects return their string representation
dw.catalog.ProductMgr.getProduct('25518704M')
// Result: [Product sku=25518704M]
// Access properties directly or use JSON.stringify
dw.catalog.ProductMgr.getProduct('25518704M').name
// Result: "Pull On Pant"
// WRONG - throws error if product doesn't exist
dw.catalog.ProductMgr.getProduct('invalid').name
// Error: Cannot read property "name" from null
// CORRECT - check for null
(function() {
var p = dw.catalog.ProductMgr.getProduct('invalid');
return p ? p.name : 'Product not found';
})()
// Result: "Product not found"
(function() {
try {
var p = dw.catalog.ProductMgr.getProduct('25518704M');
return JSON.stringify({id: p.ID, name: p.name});
} catch (e) {
return 'Error: ' + e.message;
}
})()
return at top level - use expression evaluation insteadrequire() - use global dw.* namespaceJSON.stringify() for object/array outputget_sfcc_class_info tool before calling unknown methodsdw.system.Site.current.ID
JSON.stringify({
siteId: dw.system.Site.current.ID,
locale: request.locale,
customerList: dw.customer.CustomerMgr.getSiteCustomerList().ID
})
(function() {
// Your multi-step logic here
var result = [];
// ... build result ...
return JSON.stringify(result);
})()
| Error | Cause | Solution |
|---|---|---|
invalid return | Using return at top level | Remove return, use expression or IIFE |
Cannot call method of null | Using require() | Use dw.* global namespace |
Cannot set property of null | var x = {} returns null | Use IIFE or inline object literal |
Timeout waiting for breakpoint | Wrong siteId or instance unreachable | Verify siteId, check instance connectivity |
Unexpected token '<' | Auth/connectivity issue | Check dw.json credentials, verify instance is accessible |
Unauthorized / 401 | Username case mismatch | Username is case-sensitive - match exactly from BM user |
No compatible storefront cartridge | Missing SFRA or SiteGenesis | Deploy app_storefront_base or specify custom breakpoint |
Before using evaluate_script:
dw.* namespace, not require()JSON.stringify()app_storefront_base (SFRA) or app_storefront_controllers (SiteGenesis) deployed