RSS

Category Archives: Visual Flow

Sending Values To A Subflow

Sending Values To A Subflow

What Kind of Info Can be Sent Into and Received From a Subflow?

All kinds!

  • Primitive data types like text, number, Boolean, currency, date, date time, and picklist values
  • Collections of primitive data types
  • sObject collections

So What’s So Interesting About That?

Here’s what’s interesting:

Subflows, when receiving information from or or sending information to a calling flow, must have access modifiers on those variables to expose to the calling source (Other Flows, Visualforce, and Apex).

Calling Flows, however, do not need access modifiers on variables used to send information to or receive information from a Subflow. In other words, calling Flows can keep all of their variables Private.


What’s An Access Modifier?

Access Modifiers

Access modifiers define where the item can be viewed and used. For Flow variables the access modifiers are:

  • Private
    • The variable can be used only within the Flow
  • Input only
    • The value of the variable can be input into the Flow
  • Output only
    • The value of the variable can be received by a calling Flow
  • Input and Output
    • The value of the variable can be input into the Flow and is also available for a calling Flow to receive back

Private sign

[As an aside, Apex access modifiers are slightly different: Private, Public, Global]


What Does This Mean?

  • Items without access modifiers (Constants, Text Templates, and Formulas) can be used to pass values to a Subflow
  • Variables without access modifiers can be used to receive values from a Subflow
  • Only variables that need to be exposed to an external source need to have an access modifier other than Private

Using Constants, Text Templates, and Formulas

Using items that do not have access modifiers to set values for a Subflow sort of makes sense because when you call a Subflow you can simply type a value into the Source field, which would sort of be the same as using a Constant.

Subflow typed input

Typing a value into the Source field

.

Subflow Input Using Variable

Using a Constant, Text Template, or Formula in the Source field


Keeping Subflows Easy To Use

Make it Easy

When working with a single Subflow things are pretty straightforward: The Input and Output variables you see in the Target picklists will pertain to that Subflow only.

However, when a Subflow in turns uses other Subflows, the visible variables can start to get a little confusing. Variables that have been marked as Input because they are receiving information from another Subflow are now exposed to the original calling Flow. They don’t really need to be initialized by the calling Flow, yet there they are in the picklist when you click on the Target field.

Here is an example of a Subflow that has several Target variables:

Lots of Input Options

If some of these variables can be marked Private the Target selection becomes much easier:

Simplified Input List

Simplified Target Variable Selection


There you have it. Simplification. It’s a beautiful thing.
Cheers!

Advertisements
 
Leave a comment

Posted by on October 18, 2017 in Flow Tips & Tricks, Visual Flow

 

Tags:

Visual Flow Error Handling Utility Flows

Visual Flow Error Handling Utility Flows

When I start a new project I often want to skip over the preparation stage and dive right into the deep end. For me that’s where the fun is! Along those same lines I have been known to drag my feet with finishing a project, but that’s a story for another time.

So ignoring setting up all the stuff needed to handle errors and immediately dig into the heart of the project often applies to me when I’m writing a new Flow.

And that’s exactly what happened again yesterday. So I asked myself, “Self, how can you make this easier?” (or something like that)

Here is what I came up with: two utility flows

  • One to retrieve the bits and pieces needed for error reporting (and some other stuff)
  • One to do the error reporting.

Happiness is a good report card


Two Utility Flows to Handle All Errors

  • Error Handler Items
  • Error Handler

Two Utility Flows to Handle Errors


The Error Handler Items Flow

Error Handler Items Flow

Variables:

Error Handler Items Variables

This flow does two things:

  1. Fetches and returns an email collection of Users who have been designated to receive error messages
    1. Retrieving Stick on Bridge
    2. I’ve added a custom Boolean field to the User object, “Is Flow Error Recipient,” and have set it to True for Users who should receive error messages
    3. If there are no Users with this field set to True, the Users assigned to the System Administrator profile will receive the error messages
    4. This collection is necessary in the Error Handler Flow if that Flow is to send an email
  2. Returns the name of the User who launched the Flow
    1. Get User Name Formula
    2. This information is not required in the Error Handler Flow, but is made available for the calling Flow to use

Of course if you wanted to make other items available to your calling Flow you could certainly do so. This could be a handy-dandy utility flow for fetching all sorts of things that you may need on a regular basis.


The Error Handler Flow

Error Handler Flow

Variables:

Error Handler Variables

This flow does also does two things:

  1. It constructs an email Subject and Body (that’s what the Text Templates do)
  2. It sends an email to the addresses in the

Parts of an email


What is Gained?

At first blush this solution looks pretty much like the same amount of work as my prior solution where I used a utility flow to fetch the collection of email addresses and a Send Email element to report the error.

Old Error Handling Method

And certainly the number of elements I’m dragging into the Cloud Flow Designer is the same.

Where it saves time and attention is in these areas:

  • The email Subject and Body are generated by the Error Handler flow so I don’t have to create formulas or text elements in each flow to do this
  • If I want to change how the generated email looks I have only one place I need to go (as opposed to every single Flow where I have a Send Email element)
    • This is huge for me as I’m all over the page here – having a single place where these items are structured will make for more consistent error reporting
  • If I want to display the User’s name in the Flow, Error Handler Items exposes that information so I don’t have to create a function to get it
    • If I want the way a User’s name displays to change I have only one Flow to update to make that happen across all Flows
  • If I want to use other items on a regular basis I can use the Error Handler Items Flow to expose them

Save Time Fuzzy Ball

As my knowledge of Visual Flow has expanded I have revamped my error handling a couple times and I can honestly say that just the second bullet point alone is well worth the trouble of revamping it again. So that is my task for today: revisit all the Flows where errors need to be handled and install this method. For me it’s a small price to pay to be able to quickly update my error reporting down the road.

Time to Revamp


Cheers!

 
Leave a comment

Posted by on October 18, 2017 in Flow Tips & Tricks, Visual Flow

 

Tags:

Calling Subflow’s That Do Not Need Input

Calling Subflow’s That Do Not Need Input

Did you know that you can create Subflows that doen’t require input? It seems so logical that this could happen I’m stunned that I didn’t figure this out a couple years ago!

ROFLMAO

There is just one simple trick to making this happen: when you call the Subflow, delete the row that is looking for input.

Here is what a call to a Subflow looks like:

Call to subflow

See the little trash can to the right of the Target and Source input fields?

Trash can icon

Click on it and the row disappears.

Subflow after row is deleted


So simple and so useful!

 
Leave a comment

Posted by on October 17, 2017 in Flow Tips & Tricks, Visual Flow

 

Tags: ,

Get Nth Element of Collection Flow

Get Nth Element of Collection Flow

A few days ago I read in Dan Appleman’s book, Advanced Apex Programming, that “all of your Apex code should be designed to handle bulk operations. All of it – no exceptions” which shot me off on a tangent yesterday to see if I could “bulkify” my Get RecordType Id Flow (Lookup RecordType Ids in Visual Flow – A “Bulkified” Approach). My solution ended up being a combination of three Flows, with the third one returning the Id at a specified index from a collection of Ids.

As I was putting the finishing touches on that project it dawned on me that the third flow could be a generic utility Flow that simply returns the nth element of any input collection. Instead of having a Flow that only works on Id (Text) collections (and is named in such a way that I may not think to use it in other situations), why not have one that will work on all sorts of collections?

tele tubby sunrise


Overview of the Flow

The Get Nth Element of Collection Flow

So far this Flow handles two types of collections: Text collections (elements are outlined in green) and Number collections. I really only need it to handle Text collections right now, but for the exercise of figuring out how to make it work I included handling Number collections.


More Specifics

Handling One Collection


Variables

In order for this Flow to handle different types of collections (Text, Number, SObject, etc) it must have input collection variables for each type. Since mine, so far, handles only two types of collections I have two input collection variables. You could add all sorts of collections to your Flow including collections of  SObjects, like Accounts and Contacts.

Input collection variables

Other than counters the Flow also will need an input variable to receive the requested index, output variables for each collection type and variables to use in your Loop elements.

Other variables


The Flow of the Flow

The first thing it does is count the number of elements in one of the input collections. The collection that the Flow starts with is arbitrary – mine just happens to start with the Text collection.

Get element count

If the Flow determines that there are no elements present it proceeds to the next collection.

If elements are present, there is a quick check to make sure that the requested index is within the number of elements in the collection.

Check for Valid Index

Valid Index Check

If the requested index is invalid the Flow assigns a message to an output variable that the calling Flow can choose to receive and check.

If valid, another loop parses through the collection and saves the nth element to a result variable.

Finding the Nth Element


Using the Flow

Here is an example of how you would use the Flow to retrieve the 2nd element of a text collection:

Asking for the 2nd element of a collection

And how you would get your result back:

Receiving the result

Optionally you can also ask for the number of elements in the input collection, and an error message:

Getting optional items


Some Additional Details

Element Count

For my purposes I’ve exposed the count of the number of elements in the collection to the calling Flow (ElementCount is an output variable). This is totally optional. I have another utility Flow that counts the number of elements in a collection that I could use but if there was ever a need to know the number of elements and retrieve a specific element this Flow would do both in one fell swoop.

Two birds with one stone

Alternate Ending

And here is an alternate way to handle the end of the flow for any given collection:

Alternate Ending

In this ending, as soon as the result is located the Flow exits. Unless you’re working with extremely large collections this ending probably won’t save any time, but for what it’s worth, it does increase the efficiency of the Flow some infinitesimal amount.

A word of caution about exiting a loop prior to parsing the entire loop. If you do this for a loop within a loop you’ll get some really inaccurate results. This is because Salesforce doesn’t begin looping through a collection at the beginning of it if you’ve previously exited it somewhere in the middle. This was a situation I discovered very early on in my Flow experience which gave my simple mind much grief at the time (Flow Loop Doesn’t Start at First Record)!


Cheers!

 
2 Comments

Posted by on October 12, 2017 in Flow Tips & Tricks, Visual Flow

 

Tags: , ,

Lookup RecordType Ids in Visual Flow – A “Bulkified” Approach

Lookup RecordType Ids in Visual Flow – A “Bulkified” Approach

I find myself feeling so excited when I learn new tricks that can elevate my prior and current work, and this is a new approach to retrieving RecordType Id’s in a “bulkified” manner (as opposed to a single record approach in my prior post Lookup RecordTypeId Subflow) that has done just that.


What is Bulkification?

Bulkification is the process of structuring code to handle the processing of multiple records (as opposed to a single record) for better code efficiency. On the Salesforce platform this is critical as it is a multi-tenant platform with shared resources. Dan Appleman, author of Advanced Apex Programming, has gone as far as to say “All of your Apex code should be designed to handle bulk operations. All of it–no exceptions.”

no exceptions.jpg

After reading that I began to think…hmmmm…I wonder where I can change some of my Flows to “bulkify” them to a greater extent. His quote does specify “Apex,” but what about Flows? In that light I set out to see what could be done, starting with my Get RecordType Id flow.

Now, as you’ll see below, the bulkified process is much more complex than simply calling a single record lookup Flow two or three times. And to be sure, there is a trade-off: Multiple DML statements versus multiple looping. So after completing this project I decided it would be prudent to keep the original flow in my tool box for those instances where I need a single RecordType Id, even though it isn’t “bulkified.” 🙂


Overview of “Bulkified” Get RecordType Ids Flow

The Bulkified Flow

How it Works

  • The Flow receives two collections
    • One collection has object names
    • One collection has developer names
    • Input collection variables
    • The names at each index in these collections must correspond – in other words, if the first object name is “Account” then the first developer name must be a developer name for an Account RecordType
  • It then looks up all active record types saving the following fields into a collection (you could include inactive record types as well, if you wanted to)
    • Id
    • DeveloperName
    • SObjectType
  • Now, transferring the requested RecordType Ids requires three loops: an outer loop and two inner loops within that outer loop
    • The first two loops (the outer loop and first inner loop) get a Developer Name – Object Name pair
  • The Outer Loop
    • The outer loop parses over the developer names collection
      • Loop thru developer names collection
      • Within this loop a count variable (count1) is incremented and another count variable (count2) is set to zero
      • Increment count1
      • (It was just arbitrary that I looped over this collection first and the object names collection second – you could choose to do it in the opposite order if you’re so inclined)
  • The Second Loop (First Inner Loop)
    • The second loop parses through the object names collection
      • Loop thru object names collection
      • Within this loop the count2 variable is incremented
        • When count2 is equal to count1 the object name is stored
      • At the end of the loop, control passes to the third loop
  • The Third Loop (Second Inner Loop)
    • The third loop parses through the record type ids collection
      • Loop thru record type ids collection
      • When the developer name matches the developer name of the first loop’s variable and the object name stored from the second loop the Id for that record is saved in a collection
      • Add RecordType Id to a collection
      • At the end of the loop, control passes back to the first loop where the next element in the developer names collection is then processed
  • The final output is a collection of RecordType Ids

Word of Caution

Be sure your inner loops always parse through the entire collection. Exiting a collection early in an inner loop can give you erratic results. Who needs that?! (See Flow Loop Doesn’t Start at First Record)


How to Call The Get RecordType Ids Flow

Since this Flow requires input of two collections I decided to create another Flow that constructs those collections and launches the one outlined above (I call it the Get RecordType Ids Launcher).

Get RecordType Ids launcher

This Flow does the following:

  • It receives the developer names and object names in separate input variables
    • wrapper variables
    • (Additional DeveloperName# and ObjectName# variables can be created if more than three RecordType Ids are needed)
  • Then it adds the names to collection variables
    • Adding names to collections
  • After the collections are built it launches the Get RecordType Ids Flow and receives back a RecordType Ids collection
  • Finally it returns the RecordType Ids collection to the calling routine

Getting a RecordType Id for a Specific Index

So how do you get an Id at a specific index within a collection? In order to do that I wrote a third Flow (I call it my Get RecordType Id by Index Flow).

Get recordtype ids by index

  • This Flow receives a collection of RecordType Ids and an Index number
  • It starts by doing a quick check to make sure the Index is within the number of elements of the RecordType Ids collection
    • Index error check
  • Then it loops through the Id collection
    • Loop thru Id collection
    • For each element a count variable is incremented
    • If the count is equal to the Index the RecordType Id is stored
  • Finally it returns the stored RecordType Id

Putting it All Together

Putting it all together

When this solution is put to work the following happens:

  • A call to the Get RecordType Ids Launcher Flow retrieves a collection of RecordType Ids for the object name – developer name pairs entered into that Flow
    • Calling the Get RecordType Ids Launcher Flow:Call to Get RecordType Ids Launcher
    • Getting a collection of RecordType Ids back:

      Getting RecordType Ids collection

      Getting a Collection of RecordType Ids Back

  • Now, for each RecordType Id that is needed, use the Get RecordType Id by Index Flow to retrieve the Id
    • Calling the Get RecordType Id by Index Flow:Calling the Get RecordType Id by Index Flow
    • Getting the RecordType Id backGet RecordType Id back

thoughts.jpg

A Couple More Thoughts

  1. By all intents and purposes you could choose to combine the “launcher” Flow with the Get RecordType Ids Flow. I chose to break these processes apart to keep increase the number of options I have for retrieving RecordType Ids.
  2. The Flow for returning a specific RecordType Id by index could be (and probably should be) a generic Flow that receives a collection variable (containing any sort of information) and returns the value from the collection that corresponds to the specified index. After this occurred to me I wrote a more generic version of it: Get Nth Element of Collection Flow.

So there you have it. In my box of Flow tools I now have a Flow that will lookup a single RecordType Id, and a set of three Flows that I can use when I need multiple RecordType Ids but I need to conserve DML calls.

Cheers!


heavy weight

 
Leave a comment

Posted by on October 12, 2017 in Flow Tips & Tricks, Visual Flow

 

Tags: , ,

Regex in Salesforce

Regex in Salesforce

(Pardon the dust. 🙂 This post is still a work in progress!)

Regex is a confusing language to me. But even though I find it confusing, I recognize it’s power to verify input. Here are my (ongoing) notes on Regex.


What is Regex?

Regex is a sequence of characters that defines a string pattern. Huh? Let’s break that down:

  • Regex is short for “regular expression”
  • A “regular expression” is a sequence of characters
  • That sequence of characters may look more like hieroglyphics than anything else
    • regex
  • Those hieroglyphics define a string pattern
  • A string pattern is a string of characters, such as a username or an email address

So Regex is hieroglyphics that define a string. 😀


Regex Names, Engines, and Flavors

Other Namesflavors

Regex is also known as Regexp.

Engines & Flavors

Regex is processed by software called an Engine. And to complicate things, there are lots of different engines, each with its own unique way of interpreting the regular expression. The syntax and behavior of a particular engine is called a regular expression flavor. What this means is that if you’re using Regex in Java you’ll be using one flavor, and if you’re using Regex in Python you’ll be using another flavor.

What Flavor Does Salesforce Use?

Salesforce uses the Java flavor (see Java.Util.Regex Class Pattern).


Where Is Regex Used In Salesforce

Salesforce has a REGEX function that is available in field validation formulas, Visual Flow, and Apex. The REGEX function is not available in formula fields or in Visualforce. Boo.


The Salesforce Regex Function

The Salesforce Regex Function looks like this:

REGEX(the_string_to_check, the_regular_expression)


Using REGEX In Validation Rules

Validation rules look for errors. If an error is found (the validation rules evaluates to True), an error message is displayed and the user must correct their input before the record can be saved.

This means that when you’re writing REGEX that matches what you want in the field you’ll need to negate the final result like this:

NOT( REGEX(Zip__c, “[0-9]{5}(-[0-9]{4})?”)


Getting To The Nitty-Gritty Of Understanding Regex

nitty grittyOkay, so how do you start to understand Regex gobbly-gook? You take it just one little bite at a time!

So here we go!

gobbledygookHow characters are represented in Regex

Other than special characters, what you see is what the Regex formula is looking for. In this example the formula is looking for the letter “u” and only the letter “u”. If anything else is present the formula will evaluate to false:

REGEX(String, “u”)

A set of characters, or Character Class, is a group of characters between brackets. Character classes allow for several different characters to be present at a given location in the string. In this example the formula is looking for a single character, the letter “a”, the letter “f”, or the digit “2”:

REGEX(String, “[af2]”)

To include all the characters between two given characters you can use a dash. In this example the formula is looking for a single character between “t” and “x” or between “R” and “Y”:

REGEX(String, “[t-xR-Y]”)

Negation

To exclude characters you put a carat (^) at the beginning of the character class. In this example the formula is looking for a single character other than “g”, “e”, or “n”:

REGEX(String, “[^gen]”)

Note: the carat is a special character that has dual meaning. When it is used within a character class it means negation. When it it used outside of a character class it signifies the beginning of the string (see further down in the post for this).


Grouping

grouping

Characters can be grouped together by including them between parentheses. In this example the string “bouncy ” is grouped and the entire function matches the string “red bouncy ball”:

REGEX(String, “red (bouncy )ball”)

 


Wildcard Characters and Repeating Pattern Quantifiers

Wildcard-80lv

When you need to match any character or a given token zero or more times you can use wildcard characters and repeating patterns:

  • . (period) – Match any character except a line terminator
  • ? (question mark) – Match the preceding token 0 or 1 times
  • * (asterisk) – Match the preceding token 0 or more times
  • + (plus sign) – Match the preceding token 1 or more times
  • {n} – Match the preceding token n times
  • {n,} – Match the preceding token n or more times
    • Note: {0,} is equivalent to * and {1,} is equivalent to +
  • {n,m} – Match the preceding token at least n times and no more than m times
    • Note: {0,1} is equivalent to ?

Examples:

  • REGEX(String, “pe.k”) will match “peak”, “peek”, “pe2k”, etc.
  • REGEX(String, “goat(ee)?”) will match “goat” and “goatee”
  • REGEX(String, “red (bouncy )?ball”) will match “red ball” and “red bouncy ball”
  • REGEX(String, “tiger9*”) will match “tiger”, “tiger9”, “tiger99”, “tiger999”, etc.
  • REGEX(String, “(lily)+”) will match “lily”, “lilylily”, “lilylilylily”, etc.
  • REGEX(String, “(boo){2}”) will match “booboo”
  • REGEX(String, “7{2}”) will match “77”, “777”, “7777”, etc.
  • REGEX(String, “xo{3,4}”) will match “xoxoxo” and “xoxoxoxo”

“Greedy,” “Lazy,” and “Possessive” Quantifiers

When an engine parses a string with quantifiers (things that multiply characters) there are ways to help the engine be more efficient. This is where “greedy,” “lazy,” and “possessive” quantifiers come. While it’s mostly not necessary to know the difference, it is helpful when reading someone else’s Regex to know what these things mean.  See this site for an excellent explanation of quantifiers.

Greedy Quantifiers

The special characters ?, *, +, {n}, {n,}, and {n,m} are called “greedy quantifiers” because they match as many characters as possible.


Lazy Quantifiers

Quantifiers combined with the question mark create what are called “Lazy Quantifiers.”

  • ?? – once or not at all
  • *? – zero or more times
  • +? – one or more times
  • {n}? – exactly n times
  • {n,}? – at least n times
  • {n,m}? – at least n times and up to m times

At first blush at appears that there isn’t any difference. The difference lies in how the engine parses the string to look for a match. With greedy quantifiers the engine will try to match as many instances of the quantified token as possible. With lazy quantifiers the engine will try to match as few as needed.


Possessive Quantifiers

Quantifiers combined with the plus sign create what are called “Possessive Quantifiers.”

  • ?+ = once or not at all
  • *+ – zero or more times
  • ++ – one or more times
  • {n}+ – exactly n times
  • {n,}+ – at least n times
  • {n,m}+ – at least n times and up to m times

These quantifiers are helpful when you want a match must fail quickly if it doesn’t follow a particular pattern. Using possessive quantifiers can increase the efficiency of your Regex function.


Special Characters and Metacharacters

SesameCharacters

Special characters are characters that have a special meaning to the regex engine. Metacharacters are special characters used within a character class.

Special Characters

These are the special characters in Regex:

  • \ (backslash) – is combined with another character to mean something else
  • ^ (carat) – 1) denotes the beginning of a line or 2) negates a character class
  • $ (dollar sign) – denotes the end of a line
  • . (dot or period) – represents any single character except a line terminator
  • | (pipe or vertical bar) – is used as an “OR” operator
  • ? (question mark) – multiplies a token 0 or 1 time
  • * (asterisk) – multiplies a token 0 or more times
  • + (plus sign) – multiplies a token 1 or more times
  • – (dash or hyphen) – used for indicating a sequence (range) of characters
  • () (parentheses) – enclose character groups
  • [ (open bracket) – enclose character classes
  • { (open curly brace) – used when multiplying a token a specified number of times

Metacharacters

Any of the special characters except these, the metacharacters, can be used within a character class (a group of characters within brackets) without escaping them. Metacharacters have special meaning within a character class depending on where they are used. If used in their special places, they have special meaning and you would need to escape them in order to test for the actual character. If used in their not-so-special places, they do not need to be escaped.

These are the metacharacters in Regex:

  • ] (closing bracket)
    • it’s special place is anywhere after the first position in the character class or after the negating carat
    • Example: “[]xyz]” tests for a closing bracket and the letters “x”, “y”, and “z”
      • This can also be written “[xyz\]]”
    • Example: “[^]867]” tests for anything that is not a closing bracket or the digits “8”, “6”, or “7”
    • Example: “[tip\]] tests for a closing bracket and the letters “t”, “i”, and “p”
      • This can also be written “[[tip]”
  • \ (backslash)
    • the backslash must always be escaped within the character class if it’s a character you’re testing for
    • Example: “[+-$*\\]” tests for +, -, $, *, and \
  • ^ (carat)
    • it’s special place is right after the opening bracket. Placed anywhere else it is just a character
    • Example: “[^pex]” – Here the carat means “not”. This would test for anything except the characters “p”, “e”, and “x”
    • Example: “[This^Too]” – Here the carat is nothing more than a character
  • – (dash or hyphen)
    • The hyphen has three special places:
      • right after the opening bracket
      • right before the closing bracket
      • right after the negating carat
    • used anywhere else it is just a character

Escape Character

The escape character is the backslash (\) character. This means to represent any special character in a string you would need to put the backslash in front of it like this:

  • \\\\ – the backslash character
  • \^ – the carat character
  • \$ – the dollar sign character
  • \. – the period character

Backslash – Character Combinations

Certain characters combined with a backslash represent certain non-alphanumeric characters:

  • \\t – tab character
  • \\n – new line (linefeed) character (this is a line terminator character)
  • \\r – carriage return character (this is a line terminator character)
  • \\f – form feed character
  • \\a – alert (bell) character
  • \\e – escape character
  • \\cx – the control character corresponding to “x” (ctrl-B would be “\\cB”)

Predefined Character Classes

The following are predefined character classes you can use:

  • . (period) – matches any character except a line terminator
  • \\d – matches any digit (shorthand for “[0-9]”)
  • \\D – matches any non-digit (shorthand for “[^0-9]”)
  • \\s – a white space character (space, tab, new line, form feed, carriage return, and \\x0B)
  • \\w – a “word character” (shorthand for “[a-zA-z_0-9]”)
  • \\W a non-word character (shorthand for “[^a-zA-z_0-9]” or “[^\w]”)

Boundaries

fence

Boundaries are things like the beginning or end of a line or a word. These are the boundary characters you can use:

  • ^ – the beginning of a line
  • $ – the end of a line
  • \\b – a word boundary
  • \\B – a non-word boundary
  • \\A – the beginning of input
  • \\G – the end of the previous match
  • \\Z – the end of the input except for a final terminator
  • \\z – the end of the input

Some Sample Regex Formulas

  • California Drivers License
    • REGEX(Drivers_License__c, “([A-Z]\\d{7})?”)
    • Example matches:
      • C7768934
      • nothing (the ? means 0 or 1 item)
    • Checks for a capital letter followed by 7 digits
  • Credit card number
    • REGEX(Credit_Card__c, “(((\\d{4}-){3}\\d{4})|\\d{16})?”)
    • Example matches:
      • 1234-1234-1234-1234
      • 1234123412341234
      • nothing (a blank field)
  • Email address
    • email regex
  • Number between 100 and 99999
    • \\b[1-9][0-9]{2,4}\\b
  • US Phone Number
    • REGEX(Phone, “((\\([2-9]\\d{2}\\) ?[2-9]\\d{2}-\\d{4})|(([2-9][0-9]{2}-){2}\\d{4})|(([2-9][0-9]{2}\.){2}\\d{4})|([2-9]\\d{2}){2}\\d{4})?”)
    • Example matches:
      • (223)456-7890
      • (223) 456-7890
      • 223-456-7890
      • 223.456.7890
      • 2234567890
      • nothing
    • Interesting note: the area code and “exchange” code (the first three digits after the are code) cannot start with 0 or 1 (these numbers are reserved for special purposes). All the other digits can be any number from 0 to 9.
  • Social Security Number
    • REGEX(SSN__c, “((\\d{3}-\\d{2}-\\d{4})|\\d{9})?”)
    • Example matches:
      • 123-45-6789
      • 123456789
      • nothing
  • US Zip Code
    • REGEX(BillingPostalCode, “\\d{5}(-\\d{4})?”)
    • Example matches:
      • 95610
      • 84328-4484

Some Regex Resources

regex

  • Regular-Expressions.info – This site is a good place to learn more about Regex. Their tutorial jumps around a little bit, but for the most part is laid out fairly linearly.
  • Java.Util.Regex Class Pattern – This is the Regex pattern that Salesforce uses with the one exception that the backslash character must be escaped with a backslash since it’s a special character in Salesforce. This means that instead of writing \d to indicate a digit from 0 to 9 you would need to write \\d (the first backslash tells Salesforce to use the second one for just what it is, a backslash character and not a special character).
  • Regex101.com – This site is great for testing your Regex. You enter the expression in the top space and in the Test String space you place some strings. The window to the right of these spaces will tell you how the expression worked.
  • Regexr.com – This is another site for testing your Regex.
  • RegexLib.com – Browse for useful expressions and test your own expressions.
  • Regex Quantifiers – This site is an excellent tutorial on quantifiers and much, much more
 
1 Comment

Posted by on June 8, 2017 in Formulas, Visual Flow

 

Tags: ,

Visual Flow to Return Letters Only

Visual Flow to Return Letters Only

Have you ever wanted to extract just the letters out of a string? Here is a super quick Flow that you can use to do that. And with just a little tweaking you can adapt it to return additional characters to your liking!


Return Letters Only Flow

Letters Only Flow


Variables

Variables

  • count is initialized to zero

Formulas

Formulas

  • CurrChar = MID({!InputString}, count, 1)
  • IsLetter = REGEX({!CurrChar}, “[a-zA-Z]”)
  • LenInputStr = LEN({!InputString})

Flow Logic

  1. Check the character count against the length of the input string. If it is less than the length, proceed to next element
  2. Increment the character count variable
  3. Check the character in the input string in the count position with a REGEX formula to see if it’s a letter
    1. IsLetter
  4. If it’s a letter, add it to the output string

That’s it! Super simple, but could be really handy.


Other Possibilities

Regex is super cool. I know almost nothing about it, but am learning, and am putting together a little write-up to explain its basics, so stay tuned. In the meantime, here are some other possibilities:

  • Check for digits and letters:
    • REGEX(String, “[a-zA-Z0-9]”)
  • Check digits only:
    • REGEX(String, “[0-9]”)
    • This can also be written: REGEX(String, “\\d”)
  • Check for letters, period, space, hashtag, dash, comma, slash:
    • REGEX(String, “[a-zA-Z. #\\-,/]”)

Cheers!

 
1 Comment

Posted by on June 7, 2017 in Flow Tips & Tricks, Visual Flow

 

Tags: ,