RSS

Subflow to Convert Text to Proper Case

10 Dec
Subflow to Convert Text to Proper Case

Different users have different typing styles, and if you’re like me, you like your database to be nice and tidy, with everything typed out as similarly as possible. In other words, you don’t want shouting (ALL CAPS), or whispering (all lower case) in certain fields, like names and addresses.

In Excel there is a handy function called PROPER that handles this. Unfortunately Salesforce doesn’t have an equivalent function.

Here is a little subflow you can use for converting the case of a text field to proper case.

June, 2017 Update: I recently augmented the flow with the following features:

  • Return a 1- or 2-character input string in all upper case
  • Allow for exceptions for little words that are not usually capitalized in a title (The Chicken Crossed the Road), or parts of a person’s name that are not traditionally capitalized (Chicken Little von Housen)
    • To do this the Flow looks for spaces. Each word is saved in lower case to a variable. At the end of the word (when a space is encountered) it is checked against a list of exceptions and if it is in the list the end of the output string is replaced with the lower case word.
  • Capitalize the first character after certain characters, like an apostrophe or dash
    • Example: O’Brien, Jules-Jules

The ProperCase Flow

ProperCaseFlow

 


Constants and Variables

 

  • AutoCapChars are the characters that indicate the next letter should be capitalized, like an apostrophe or dash
    • Examples: O’Leary, Walla-Walla
  • counter is incremented each time through the loop and compared with StringLen to see if the entire string has been examined. Counter is given a default value of 1, and StringLen is given a default value of 0.
  • CurrentWord is the string of characters between spaces. Every time a space is encountered the current word is inspected to see if it is an exception.
  • InputString is an Input variable that receives the string to be converted to proper case. It is given a default value of EmptyString.
  • OutputString is the final result. It is given a default value of EmptyString.
  • IsFirstChar is used to indicate if the character being examined is the first character of a word. It is given a default value of True.
  • IsPersonName is an optional input variable for the calling flow to set to true (its default value is false). This is used to determine what list of exceptions to compare each word to.
  • MidString holds each character of the string one-by-one as the string is parsed

Text Templates

Exception Lists

Text templates are used to store a list of words that are typically left in lower case, such as “von” in a person’s name or wee-small words in a title (“the”, “an”, etc.). These templates are used in a CONTAINS function and when that function receives a string that has the colon character (:) it perceives the colon as a separator between sub strings. That means that “von:van:de” is seen as three separate words by the CONTAINS function.

Constants or Variables could be used in lieu of Text Templates.

Exception Lists


Formulas

Formulas

  • GetCurrWordFirstChar: IF(count=1, UPPER(MidString), MidString)
  • GetNextChar: MID(InputString, counter, 1)
  • GetStringLen: LEN(InputString)
  • LenTrimMidStr: LEN(SUBSTITUTE(MidStr, ” “, “”))
  • PrepString: LOWER(TRIM(InputString))
  • ReplaceOutputStrLastWord: LEFT(OutputString, LEN(OutputString) – LEN(CurrentWord)) + CurrentWord
  • SetToUpper: UPPER(MidStr)
  • WordInExceptionList: IF(IsPersonName, CONTAINS(NameExceptions, CurrentWord), CONTAINS(TitleExceptions, CurrentWord))

Flow Logic

Check Length of Input String

The first steps are to trim excess spaces out of the input string and set it all to lower case. After that, check the resulting length. If it is zero, then exit the flow.

I’ve also decided that if the trimmed input string is 1 or 2 characters long that I’ll capitalize the entire input string, then exit the flow. This is done so that situations where a first name is an abbreviation, like “TJ”, are accommodated.

Parse Input String

The rest of the flow parses the input string one character at a time. The first assignment puts the character at the position of the count variable into the MidStr variable.

Get Next Char

A three-way decision is then made.

  • If it is the first character it is saved in lower case in the CurrentWord variable and it in upper case to the MidStr variable.
    • First Character Assignment
  • If the character is contained within the AutoCapChars string (like an apostrophe or dash) then the AutoCap variable is set to True
    • AutoCapAssignment
  • If it’s not the first character and not a space, then one of two things can happen:
    • 1. It’s not an auto-cap character so it is saved in lower case to both the CurrentWord and MidStr variables
      • Not First Character
    • 2. It is an auto-cap character and is saved to both the CurrentWord and MidStr variables in upper case (and AutoCap is set to False)
  • If it is a space then IsFirstChar is set to true and CurrentWord is compared with a list of  words that should remain in lower case.Word in exception List
    • If CurrentWord is in the exception list, then the end of OutputString is replaced with CurrentWord.Replace End of Output String

Finally, the MidStr is appended to OutputStr, the counter is incremented, and we’re taken back to the start of the loop.


AutoCap

‘Hope this helps someone.

Cheers!

 

 

Advertisements
 
Leave a comment

Posted by on December 10, 2015 in Visual Flow

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: