You are currently viewing documentation for Linnworks Desktop, if you are looking for Linnworks.net documentation, click here.






Examples

Overview

Below are some detailed examples of using the rules engine.

Example 1: Assigning Postal Service Method via Extended Properties

In this example the rules engine is used to validate Stock Item Extended Properties for a match. This only works when mapping has been set up so that when orders are downloaded they automatically map to a Linnworks Inventory Items

  • This rule checks two conditions, and ends with an action to set the postal service method for an order (Fig 1.1)
  • 1. The rule checks the current postal service name of the order to see if it meets the codition equals RM 1st Class (Fig 1.2)
    • This is to ensure that if the Postal Service has already been changed by another Rule that it not affected by this Rule
  • 2. If a match is found the rules moves onto the next set of conditions in the row to validate against the Extended Properties of the inventory items that are mapped to the Order Items. The first of these is to test the Property RM Mail Type to see if any of the items have one that is set to Letter (Fig 1.3)
Fig 1.1   Fig 1.2  Fig 1.3 
  • If this condition is met, the condition will be followed by an action.
  • If not, then it will move to the next condition below it (Fig 1.4)
  • Once the appropriate condition is met an action is used to assign a postal service method to the order
  • eg 1st Class Letter - STL to the order (Fig 1.5)
Fig 1.4  Fig 1.5 

Example 2: Using Regex to validate UK Postcodes

Regex is short for Regular Expression, and is used to check text for patterns. More information can be found here here

In this example RegEx is used to see if the post code save with the Linnworks Order matches the required format for a UK Postcode. It does not guarantee that the actual post code supplied is valid or if a courier will deliver to the location, just that the format is valid and the structure makes sense.

  • Use RegEx to check if order Postcode is not a valid UK Postcode format, and if so assign the orders  to a folder so it can be manually verified (Fig 2.1)
Fig 2.1 
  • As we are checking UK post code formats, the first thing to do is make sure the rule is based upon orders being shipped to the United Kingdom. This is achieved by checking if the Shipping country equals United Kingdom (Fig 2.2)
  • Next the rule uses RegEx to check if the postcode format is invalid. 
  • It does this by using the following regex code (Fig 2.3)
    • ^([A-Za-z]{2}[\d]{1,2}[A-Za-z]?)[\s]*([\d][A-Za-z]{2})$
  • Finally, if the postcode format is invalid, the action to set to assign the order to a folder (Fig 2.4)
Fig 2.2   Fig 2.3  Fig 2.4 

Understanding the Regex used

  • The RegEx code can be split into three parts, using the postcode PO19 8DJ as an example
    • 1. Check for PO19
    • 2. Checks for any spaces between part 1 and 3
    • 3. Check for 8DJ (Fig 3.1)
Fig 3.1 

More Regex Examples

The example used above checks if a postcode is valid, but further RegEx could be used to determine if the postcode is for Mainland UK or not.
To do this, we can write more expressions to see if the postcodes match any known oversea postcodes. Examples of these expressions can be found below.

Postcode Area Postcode Range Required RegEx

Inverness
Belfast
Guernsey
Outer Hebrides
Isle of Man
Jersey
Kilmarnock
Kirkwall
Paisley
Perth
Portsmouth
Truro
Lerwick

IV1 - IV56
BT*
GY*
HS1 - HS9
IM*
JE*
KA27 - KA28
KW1 - KW4 & KW15 - KW17
PA20, PA34, PA37 - PA39, PA41 - PA48 and PA60 - PA78
PH19 - PH26, PH30 - PH44, PH49, PH50
PO30 - PO41
TR21 - TR25
ZE1 - ZE3

^IV([1-9]|[1-4][0-9]|5[0-6])[\s]*([\d][A-Za-z]{2})$
^BT[0-9]{1,2}[\s]*([\d][A-Za-z]{2})$
^GY[0-9]{1,2}[\s]*([\d][A-Za-z]{2})$
^HS[1-9][\s]*([\d][A-Za-z]{2})$
^IM[0-9]{1,2}[\s]*([\d][A-Za-z]{2})$
^JE[0-9]{1,2}[\s]*([\d][A-Za-z]{2})$
^KA(27|28)[\s]*([\d][A-Za-z]{2})$
^KW([1-4]|1[5-7])[\s]*([\d][A-Za-z]{2})$
^PA(20|34|(3[7-9])|4[1-8]|(6[0-9]|7[0-8]))[\s]*([\d][A-Za-z]{2})$
^PH((19|2[0-6])|(3[0-9]|4[0-4])|49|50)[\s]*([\d][A-Za-z]{2})$
^PO(3[0-9]|4[01])[\s]*([\d][A-Za-z]{2})$
^TR2[1-5][\s]*([\d][A-Za-z]{2})$
^ZE[1-3][\s]*([\d][A-Za-z]{2})$

The above examples work based on the rules for particular areas. A generalised rule for UK Post Codes would normally be that the initial Group would be 2 letters, followed by 1 or 2 numbers, so ^(AANN)[\s]*([\d][A-Za-z]{2})$ where AA represents the two letters and NN is the number or range of numbers. However, this doesn't account for post Codes such as SW1Y4PH, which is a valid London Postcode. Therefore, the correct RegEx to use would be:

^[A-Z][A-Z]?[0-9][0-9]?[A-Z]?[\s]*([\d][A-Za-z]{2})$

  • To use all the above Regex in the same condition, the condition will need to be structured so that it checks if the postcode matches one of the expressions used.
    • You can check against multiple expressions using the 'matches one of (regex)' conditions. Each expression can be added in the same condition (Fig 4.1)
    • You could not add a new condition for each expression. This is because the condition will be trying to check for multiple matches, and the same postcode will not match every expression. The confuguration in Fig 4.2 will not work. (Fig 4.2)
Fig 4.1    Fig 4.2 

Useful links