Split a Value into Two Fields¶
Legacy (Process Designer)
This page describes a script for the Modify Your Metadata activity of Process Designer. It was replaced by the Custom Step in Workflow. The script may not run as a Custom Step without changes. For new work, use the Custom Step reference.
In many instances, we've seen users need to separate a single piece of metadata into multiple fields. This can be first and last name, an invoice number from a prefix or suffix, or various other cases.
Below is an example of how we might separate out a value gathered by a barcode, called PageBarcode in this example by using a hyphen as a delimiter of the two values using the Modify Your Metadata activity.
Full Script¶
var pattern = /\S{3}-\S{5}/
batch.Documents.forEach(function (doc) {
var barcodeValue = doc.Metadata["Index"]["PageBarcode"];
var valid = pattern.test(barcodeValue);
if (valid) {
var delimiterPosition = barcodeValue.indexOf("-");
var branchCodeValue = barcodeValue.substring(0, delimiterPosition);
var documentNumberValue = barcodeValue.substring(delimiterPosition + 1);
builder.SetDocumentMetadata(doc.DocumentId, "Index", "BranchCode", branchCodeValue);
builder.SetDocumentMetadata(doc.DocumentId, "Index", "DocumentNumber", documentNumberValue);
}
});
Walkthrough¶
- First, we will use var pattern = /\S{3}-\S{5}/ to set the regular expression we will use to validate these barcodes are in the correct format.
-
Second, we will loop through each document using a ForEach to determine if the value PageBarcode is equal to our pattern set in step 1.
-
Then, if the pattern validates, we will set the characters before the hyphen to a value called branchCodeValue and after the hyphen to documentNumberValue.
-
Now that we have our variables set for each document, we can set the BranchCode and DocumentNumber fields in each document to these variables.