Sidebar

Best way to remove extension in PID-13.7 and PID14.7 when length can vary?

0 votes
645 views
asked May 2, 2014 by rdzanski-1356 (210 points)
The below works, but is there a better way?

if(message.checkNodeExists('PID-13.7')){
var hmphn = source.getNode("PID-13");
hmphn = hmphn.substring(6, 14);
message.setNode("PID-13.7", hmphn);
}

1 Answer

0 votes
//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);
answered May 2, 2014 by gary-t-8719 (14,860 points)
...