//Get the phone number and remove all non numerical values
var phoneNum = source.getNode("PID-14").replaceAll('\\D+', '');
//Parse the phone number into the individual elements
var areaCode = phoneNum.substring(0,3);
var prefix = phoneNum.substring(3,6);
var number = phoneNum.substring(6,10);
var ext = phoneNum.substring(10);
//Determine the phone number length and set back into message with appropriate formating.
if (StringUtils.length(phoneNum) === 7) {
message.setNode('PID-14', '(' + areaCode + ')' + prefix + '-' + number);
} else if (StringUtils.length(phoneNum) === 10) {
message.setNode('PID-14', '(' + areaCode + ')' + prefix + '-' + phoneNum.substring(6,10));
} else if (StringUtils.length(phoneNum) > 10) {
//Use the next line if you do not want the extention
message.setNode('PID-14', '(' + areaCode + ')' + prefix + '-' + phoneNum.substring(6,10));
//Use the next line if you want to include the extention instead of the one above.
//message.setNode('PID-14', '(' + areaCode + ')' + prefix + '-' + phoneNum.substring(6,10) + ' x' + ext);
}
//If you don't want to include the visual characters in the phone number you would just use the areaCode + prefix + number variables.
// e.g. message.setNode('PID-14', areaCode + prefix + number);