Example Trim Field to Max Length but Keep Last Word Complete

Document created by chris_stevens Employee on Sep 17, 2015Last modified by dave_lesshafft on Mar 2, 2016
Version 2Show Document
  • View in full screen mode
Use Case: You have a field that requires data to be X characters in length, but you want to maintain only complete words/sections.
For example, your input is:
<root><field1>This is an example sentence. This is what will be trimmed.</field1></root>
But your destination field is limited to a field length of 20 characters. Normally, a trim would simply push "This is an example s" to your destination, but this allows you to push "This is an example" to your destination instead.
For this example process, our process is simple, and looks like such:
Normally, you'll use a connector to get your data, but we're passing it through a message, just for this example. The message shape contains the following:
<root><field1>This is an example sentence. This is what will be trimmed.</field1></root>

Next, you'll need to add your map shape, which contains the same XML Profile for both the input and destination. This XML profile contains a 'root' element and a child element below that, called 'field1', as so:


Once back in your map, you'll need to create a map function, which contains 1 input and 1 output field. From the input field, map this to a Right Character Trim shape, which is your absolute maximum field length. In our case, it is 20 characters. From here, map that result to a Custom Scripting Shape, where this shape has 1 input and 1 output field. The script within the shape looks like so:

if (inputString1.contains(" ")) {     outputString1 = inputString1.substr(0, inputString1.lastIndexOf(" ")); } else {     outputString1 = inputString1; }
This is what your map function should look like:


Configure your map accordingly, and map the output from the function to the output on your map.


Save your process, and your output should produce the correct results:
Input = This is an example sentence. This is what will be trimmed.
Output = This is an example