Snap code in Epicor CPQ consists of individual blocks which are connected to other blocks to construct algorithms, expressions, settings, or operations. Here are some examples of some simple blocks you may find in Snap:
Clockwise from top-left: A literal string block, if block, math operation block, and a loop block
Block Types
Blocks have different types of connectors, and there is an example of each type above.
Output connectorsare on the left side of the block and imply that the block has some kind of return value that will feed into the logic expression of the block it is connected to.
Value connectors expect a block or chain of blocks that will return some kind of value. Value connectors mate with Output connectors.
Statement connectorsimply a context of some sort (like curly braces in many code languages) Statement connectors mate with previous connectors.
Next & Previous connectors are for sibling blocks and control the imperative flow of the language.
In addition, some blocks can also be mutated to fulfill more functionality. For instance, in the "if" block above, the + button can be clicked to add more statement connectors for "else if" and "else" conditions.
Nesting Blocks
In the above example, blocks have operations nested, which will all result in a single value for the left-most block to receive. Execution is performed from the inner-most nested outward. The further nested a block is, the lighter color it is shaded to aid the programmer visually. In this case, there are two blocks, 3 and 4, which are the most nested.
The mathematical expression within this line of Snap code is equivalent to 21 / (3 + 4).
Manipulating Blocks
Blocks can be manipulated in a number of ways by right-clicking and using the context menu.
- Duplication Blocks and their directly connected children can be duplicated by right clicking and selecting "Duplicate" or by holding the Ctrl key while clicking on them.
- Disabling If you disable a block, it will not execute. This may be useful in case you wish to temporarily see what happens if those blocks are not present in the rule for the purposes of testing or troubleshooting, without fully deleting them.
- Copy to Json Blocks are stored on the server using a text format called JSON. By copying to JSON, you can take all of the selected blocks and copy them in this format, and then paste them into another rule if you so desire.
- Set Variable / Get Variable Some blocks introduce a variable into the workspace. By right clicking on the block which introduces this variable, you can click the "Set Variable" or "Get Variable" menu items as a shortcut to manifest the blocks which lets you set or get the variable's value. [[Variables|More on Variables]]
- Cast to Expected Type If a block contains an error stating that it connected to another block which is expecting a different type than what is provided, this option will attempt to "cast" the block into the expected type. [[More on types]].
-
Splicing Out Normally when you grab a block of snap code to move it, the blocks below it move as well. If you hold the shift key while moving a block (or selection of blocks) however, it will "splice out" or
leave the blocks below and attach them to the rest of the code that is not being moved.
-
Filtering Blocks You can filter blocks in the snap toolbox by typing in the search box at the top.
- Help Each block contains documentation to explain its purpose and how to use it. Clicking help will expand the right pane to provide this information.
-
Go To Definition Some blocks that refer to a specific table, safe function, or global type also show a link which will open that specific object in a new window.
The Start Block
All workspaces start with a single green block, usually the Start block. All blocks must be either directly or indirectly connected to this block to be run. Blocks which are floating in the workspace, not connected to the structure attached to the initial block, are ignored. Disconnected, "floating" blocks are translucent in appearance.
The Toolbox
To the left of the workspace is the toolbox: a colorful menu containing all the blocks available in the current workspace. The toolbox is organized into categories. Certain categories are only available in certain types of rules. You wouldn't see a configurator block in a workflow rule, for example, and visa versa. Most workspaces allow the use of the fundamental blocks listed below:
Logic
Logical blocks deal with conditional expressions to control the flow of code. The most simple kind of logical block is the If block, which will execute code based on a condition that is either true or false.
Values
Blocks can convey "hard coded" values, such as literal strings, numbers, and booleans.
Variables
Variables store values to be referenced later down in code. This is essential for maintainable and readable code, as it lets you specify a value once, and allows you to use that value without repeating it everywhere it's used. You can also change the value of a variable later on, based on conditions or the result of a function.
Numbers
Blocks pertaining to numbers and math. These blocks provide a wide array of possible operations, from simple arithmetic operations to more complex geometric and trigonometric operations which may be helpful in 3D logic.
Text
Blocks pertaining to text. Text can be formatted and manipulated using these blocks.
Loops
Loops are pieces of code which are run multiple times until a certain condition has been met. A common use of a loop is to perform operations on each item within an array.
Arrays
Arrays are values which contain a list of more values. They can be used to be manipulated, sorted, or filtered.
Dates
Blocks pertaining to dates and calendar logic. Dates also include the time of day.
Functions
Functions and subroutines are pieces of code which can be specified once, and executed by reference. Like variables, functions and subroutines can be very useful to keep your code concise. If a lot of code is doing much of the same basic logic, you can factor that out into a function which is called from each of the places where this code is executed. Functions and subroutines can take what are called arguments if you need to input values into that function.
More on Functions and Subroutines
Definitions
Definitions are special blocks which define functions, subroutines, and Types.
Debug
Debugging blocks are meant only for troubleshooting purposes. You can set what are called breakpoints which can pause the code while it is executing for you to analyze. You can also write to logs which will appear inside the admin logs page (or in the browser itself in developer tools if it run on the client side). Commenting is also available in this, which allows you to decorate your code with a description of what the code is doing for other developers to read.