Tuesday, January 28, 2014

Formula How To's: How do I extract an exact value from a comma separated list?

How do I extract an exact value from a comma separated list?

Recently, I was presented with the challenge of trying to extract an exact value from a comma separated list. In my scenario, I was trying to identify that one of the values in the list, was an exact match to a specific value on the current user's record. 

Why would I want to do this? Specifically, this allows me to use a "True" or "False" value in a list view, report or dashboard to display records. This presents the idea of a "dynamic" list, report or dashboard that is specific to the user viewing the list, report or dashboard without changing the data or the parameters thereof. 

Scenario:

As previously stated, I had a field that was a concatenated comma separated list. In my scenario this field was being set by a trigger, but regardless of how the data gets in the field the idea is the same. Next, I had a field on the user record that may have been a value in the comma separated list at anyone time on any given record. 

Formula:


AND(
If( 

/*Checking to see if the initials are here at all*/
Contains(Field_Im_Looking_at__c,$User.User_Field_value__c)
, True, False),
OR(

/*Checking for an exact match in the first set of values*/
IF(Find($User.User_Field_value__c+',', Field_Im_Looking_at__c)>0, TRUE, False),

/*Checking for an exact match somewhere in the middle. */
IF(Find(','+$User.User_Field_value__c+',', Field_Im_Looking_at__c)>0, TRUE, False),

/*Checking for an exact match at the end of the string */
IF(TRIM(RIGHT(Field_Im_Looking_at__c, LEN($User.User_Field_value__c))) = $User.Field_value__c, TRUE, False)
))

Break it down:

Ok, so breaking down this formula, I've changed the field names to "Field I'm Looking At" and "User Field Value" for simplicity sake. In the "AND" statement, I am looking to see if the value even exists within my set of values, and then the second condition contains my "OR" statement which searches for my exact value. You might not need the "AND" statement at all, and could probably do this with just the "OR" statement, but this worked for me and so I kept it like that.

Within the "OR" statement, I am checking for three conditions. They are:
  1. Is my value the first value in the list?
  2. Is my value a middle value in the list?
  3. Is my value the last value in the list?
The first two values are fairly easy to search for because I know that my value will look like: "VALUE," or ",VALUE," it's important to recognize the commas here. They are what ensure you are getting the exact value and not a partial value.

Note: If you have the scenario where a partial is possible with the end of the value (or in other words the first value in your list has a potential for being a partial), you might want to modify the first if Statement to be something like:

IF(Trim(Left(Field_Im_looking_at__c, LEN($User.User_Field_value__c))) = $User.User_Initials__c, TRUE, False)

In my scenario this wasn't possible and so I did not account for it.
What does that do? Well, simply it takes the value from the user record and trims everything but the number of characters for the value you are looking for. It then will compare the two values to see if they are a match. If they are, then you get a "True", if not you get a "False".

Application for use:

Now that I have this formula, I inserted it into a Formula Check box field. Now my users can leverage this field in list views simply looking for a "True" or "False" value to know if the records are applicable to them. This cuts down on a lot of complex reports and filtering for my end users. I hope it's helpful to you and as always this is one more thing that you can do in clicks and not code.

No comments:

Post a Comment