Pipeline Scripting Quick Start Example

The following example demonstrates in a step-by-step approach the necessary tasks to run a simple JavaScript script in a Salesforce B2C Commerce pipeline. The example prints "Hello B2C Commerce!" via a script into an HTML page.
Note: This example creates a module
  1. Create a Custom Cartridge.
    1. From the Studio menu, click File > New > Cartridge.

      The Cartridge dialog box opens.
    2. Enter the name of your new cartridge (for example, MyCartridge).
    3. In the Attach to B2C Commerce Servers section, select one or more B2C Commerce Servers and click Finish.
  2. Create a new Pipeline.
    1. Expand your cartridge in the Cartridge Explorer. Right-click Pipelines and select New > Pipeline.

    2. Enter the pipeline name: MyScript.
    3. Add a Start node by dragging it from the palette.
    4. Create a scripting pipelet called pipeletScript by dragging a Script Node from the palette.

      You can select a script file or enter a name to create a new file.
    5. Enter the script name: pipeletScript.ds and click OK. The script will open in the editor.

    6. Modify the script to look as follows (and as shown above):
      /**
      * B2C Commerce script File
      * To define input and output parameters, create entries of the form:
      *
      * @<paramUsageType> <paramName> : <paramDataType> [<paramComment>]
      *
      * where
      *   <paramUsageType> can be either 'input' or 'output'
      *   <paramName> can be any valid parameter name
      *   <paramDataType> identifies the type of the parameter
      *   <paramComment> is an optional comment
      *
      * @output Output1 : String the greeting to display
      */
      importPackage( dw.system );
      importPackage( dw.customer );
      function execute( pdict : PipelineDictionary ) : Number
      {
          // write pipeline dictionary output parameter
      var output1 : String = "Hello B2C Commerce!";  
      pdict.Output1 = output1;
          return PIPELET_NEXT;
      }
    7. Save your changes and return to the MyScript pipeline in the editor.
    8. Select the pipelet you just created and In the Properties view, set the pipelet alias to the Output1 value (In Dictionary Output set Output1 to Output1). The default is null, meaning that the value isn't written to the Pipeline Dictionary..
    9. Add an Interaction node to the pipeline by dragging it from the palette, to beneath the script node. Drag a transition from the palette to connect the script node to the interaction node.
    10. Add an Interaction node to the pipeline by dragging it from the palette, to beside the script node. Drag a transition node from the palette to connect the script node to the interaction node, which will rotate to adjust to the transition. The Pipeline should look like this:

  3. Now we will add two new templates for error processing: success.isml and error.isml.
    1. In the Navigator view, select templates > defaultfor your cartridge.
    2. Right-click New > Template. The Create Template dialog box appears (for each template creation).

    3. Select the parent folder: Template and Create two files, one at a time in the default directory: success and error (the .isml extension will be added automatically).
  4. Open the file "success.isml" and enter the following ISML code:
    	<html>
    		<head>
    		</head>
    		<body>
    			<h1>Success - hey it works!</h1>
    			${pdict.Output1}
    			<isprint value="${Output1}">
    		</body>
    	</html>
  5. Open the file "error.isml" and enter the following ISML code:
    	<html>
    		<head>
    		</head>
    		<body>
    			<h1>Error in Script</h1>
    			<pre>
    			<isprint value="${ScriptLog}">
    			</pre>
    		</body>
    	</html>
  6. We now can configure the pipeline and refer to the script file and the ISML files.
    1. Open the pipeline MyScript that you just created.
    2. Select the Interaction node. Within the Properties view, click the template element. Browse to find the success template. Click OK.
    3. Select the Interaction Continue node. Within the Properties view, click the template element. Browse to find the error template. Click OK.

    If you configured your cartridge correctly, your cartridge, pipeline, templates and script should be uploaded to the server.

  7. In the browser, specify the following URL to call your new pipeline:
    http://<your demandware ip>
    /on/demandware.store/Sites-YourShopHere-Site/default/MyScript-Start

    For example:

    http://name.eval.dw.demandware.net/on/
    demandware.store/Sites-Mycartridge-Site/default/MyScript-Start

    The first time you call the pipeline, it might take a while because the server needs to preprocess and cache the new files.

    You see Success - hey it works! and then Hello B2C Commerce! in the browser window.