Script to Change the Value of a Colored Image Based on the Last Two Characters of a Route Code
Description
You may have a requirement to place a different colored shape (i.e., circle, square, etc.) on a label to provide a quick method for sorting product simply by looking at the color on the label. In this example, the last 2 characters of a 10 character Route Code indicate what color circle should be placed on a label. For example, if the last two characters are "01" print a red circle. If the last two characters are "02" print a blue circle, etc. The Script shown in this article will perform this task.
How to change the value of a colored image based on the last two characters of a Route Code
- The sample label shown below has a variable image on the label with the Field Name "Color". At the bottom of the label is a field named Script containing the Script which will retrieve the value of the field named Route_Code and based on the last two characters will place the correct colored circle on the label. If the last two characters don't match any assigned color, then no color image will print.
- The Loftware Labeling\Images folder contains the colored images that will be placed on the label when the script executes. The following two circles are simple circles filled with the desired color and named Red.png and Blue.png:
- The script is listed below. Although only values of "01" (red) and "02" (blue) are checked, the script could be "fleshed out" to test for up to 100 different values ( 00 - 99 ).
//Define a variable to receive the value from the Route_Code field
var input = String(label.fields.field.(@name == 'Route_Code').@data);
//Get the last two characters of Route_Code
var last2 = input.slice(-2);
//Depending on the value of the last two characters select the appropriate color image to print. If the last two characters don't match any assigned color then place no value in the Color field so no image will print.
switch (last2) {
case '01': label.fields.field.(@name =='Color').@data = "Red.png";
break;case '02': label.fields.field.(@name =='Color').@data = "Blue.png";
break;default: label.fields.field.(@name =='Color').@data = "";
} - The following labels were generated by supplying values of 1122334401, 2233445502, and 5566778805 (no match).
Article Number
2020.19449
Versions
LLM & LPS 12.x and later
Environment
All supported LLM and LPS environments