I wrote code to format the text of opportunities, cases, and contact names into title case. It also takes into account some known acronyms that should be all caps. I have the web resource launching onchange, and it works perfectly in the web browser. It fails with an error message in Outlook. I have no idea why. See sample code below:
function setTitleCase(executionContext) { var attribute = executionContext.getEventSource(); if (attribute != null) { var str = attribute.getValue(); var lc, lowers, uc, uppers, surnames; var pieces = str.split(" "); lowers = ['a', 'an', 'the', 'and', 'but', 'or', 'for', 'nor', 'as', 'at', 'by', 'for', 'from', 'in', 'into', 'near', 'of', 'on', 'onto', 'to', 'with']; uppers = ['USA','US','U.S.','AL','AK','AZ', 'AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS', 'KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH', 'NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN', 'TX','UT','VT','VA','WA','WV','WI','WY','GU','PR','VI','MEX','CDN', 'USDOJ','DOJ','DOC','AIC']; surnames=['macd','mcd','mack','o\'b','mcke', 'o\'c','o\'d','o\'f','o\'g','o\'h','o\'j','o\'k', 'o\'l','o\'m','o\'n','o\'p','o\'q','o\'r','o\'s', 'o\'t','o\'u','o\'v','o\'w','o\'y','o\'z']; for (var i = 0; i < pieces.length; i++) { lc=pieces[i].toLowerCase(); uc=pieces[i].toUpperCase(); if(lowers.indexOf(lc)>=0) { pieces[i] = lc; //if the word is in the lowers array, then make it lowercase } else if(uppers.indexOf(uc)>=0) { pieces[i] = uc; //If the word is in the uppers array, then make it uppercase } else if(pieces[i].charAt(0)=="(") { // ignore the things in () } else if(pieces[i].charAt(pieces[i].length-1)==")") { // ignore the things in () } else if(surnames.indexOf(lc.substring(0, 4))>=0) { //Check if it's a MacDonald, MacKenzie type of name var j = pieces[i].charAt(0).toUpperCase(); //M******** j = j + pieces[i].charAt(1).toLowerCase(); //Ma******* j = j + pieces[i].charAt(2).toLowerCase(); //Mac****** j = j + pieces[i].charAt(3).toUpperCase(); //MacD***** pieces[i] = j + pieces[i].substr(4).toLowerCase(); //MacDonald } else if(surnames.indexOf(lc.substring(0, 3))>=0) { //Check if it's an O'Brien type of name var j = pieces[i].charAt(0).toUpperCase(); //O******** M******* j = j + pieces[i].charAt(1).toLowerCase(); //O' Mc****** j = j + pieces[i].charAt(2).toUpperCase(); //O'C****** McD***** pieces[i] = j + pieces[i].substr(3).toLowerCase(); //O'Connnor McDonald } else { var j = pieces[i].charAt(0).toUpperCase(); //Make the first letter uppercase pieces[i] = j + pieces[i].substr(1).toLowerCase(); //Make the rest lowercase } } pieces[0] = pieces[0].replace(/^\w/, c => c.toUpperCase()); //Make the first letter uppercase attribute.setValue(pieces.join(' ')); }
}