function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Scott Janis 20Scott Janis 20 

USING LEN RIGHT LEFT etc......


I have this string that I need to parse out values using a formula field. I have working examples from values 1 , 2 and 3 but I can't see the pattern within these to extend to values 4 , 5 and 6 in the string. Would appreciate any kind of guidance even if its just breaking down the code so I can modify it that is , explaining what's happening.
Article | Onsite | Sponsorship | Editorial | TFK Kid of the year | Standard
Value 1
TRIM(LEFT(AB2__ABMediaElementPath__c , FIND("|", AB2__ABMediaElementPath__c )-1))
Value 2
LEFT(RIGHT(AB2__ABMediaElementPath__c , LEN(AB2__ABMediaElementPath__c ) - FIND ("|", AB2__ABMediaElementPath__c )), FIND("|" , RIGHT(AB2__ABMediaElementPath__c , LEN(AB2__ABMediaElementPath__c ) - FIND ("|", AB2__ABMediaElementPath__c )))-1)
Value three
LEFT(RIGHT(AB2__ABMediaElementPath__c , LEN(RIGHT(AB2__ABMediaElementPath__c , LEN(AB2__ABMediaElementPath__c ) - FIND ("|", AB2__ABMediaElementPath__c ))) - FIND ("|", RIGHT(AB2__ABMediaElementPath__c , LEN(AB2__ABMediaElementPath__c ) - FIND ("|", AB2__ABMediaElementPath__c )))), FIND("|" , RIGHT(AB2__ABMediaElementPath__c , LEN(RIGHT(AB2__ABMediaElementPath__c , LEN(AB2__ABMediaElementPath__c ) - FIND ("|", AB2__ABMediaElementPath__c ))) - FIND ("|", RIGHT(AB2__ABMediaElementPath__c , LEN(AB2__ABMediaElementPath__c ) - FIND ("|", AB2__ABMediaElementPath__c )))))-1)
Zachary SingerZachary Singer
Hi Scott,
The formula is repeating several different functions to get the specified value from between the pipes. Let me try to break down the functions and what each is doing using the first formula and “Article | Onsite | Sponsorship | Editorial | TFK Kid of the year | Standard” as the value in AB2__ABMediaElementPath__c

Value 1:
  • TRIM(): This just removes extra spaces once all the other functions have run
  • FIND(): This finds the first instance of the specified text and returns a number that tells its position. Finding the first instance of the pipe | returns 9, because “Article” has 7 letters, then there is a space (character 8) then the pipe (character 9).
  • Then there is a -1, giving a value of 8.
  • LEFT(): You provide this function a text value or field, and then a number, and it returns the specified number of characters from the start of the value.
  • As a result the formula is getting the first 8 characters from the string which is “Article “ and then TRIM removes the last space.
  • Right(): Not used in this first function, but does the same thing as LEFT() except it starts at the end of the value
  • LEN(): Returns the number of characters in a text string

Now where the formula suddenly increases in size for the other values is because you can nest functions within other functions. The text or character position you find in one function, you can then set as a value in another function

To summarize what's happening in Value 2:
1. LEFT(
2.    RIGHT(
3.        AB2__ABMediaElementPath__c , 
4.        LEN(AB2__ABMediaElementPath__c ) - FIND ("|", AB2__ABMediaElementPath__c )
5.    ),
6.    
7.    FIND("|", 
8.        RIGHT(
9.        AB2__ABMediaElementPath__c , LEN(AB2__ABMediaElementPath__c ) - FIND ("|", 
10. AB2__ABMediaElementPath__c )
11.        )
12.   )-1)

1. The LEFT() function wraps around all other functions
2-5 The RIGHT() function creates the text string that is passed as the first parameter to the LEFT() function. Remember this takes a text value and then a number of characters to return
3.  The RIGHT() function is passed the entire field value
4. The number of characters to return for the RIGHT() function. It is LEN (the length) of the entire field value MINUS the position of the first pipe. The FIND() function is passed a pipe to find in the field and it returns the position of the first one it finds. From the first Value we know the pipe is at position 9. The length of the entire string is 75 characters, minus 9 is 66. So this function is returning the first 66 characters of the string starting from the RIGHT. What this creates is the text value "Onsite | Sponsorship | Editorial | TFK Kid of the year | Standard"
5. Closes out this RIGHT() function.
7-10. This is basically identical to the previous section, except it's wrapping it all in the FIND() function to find the first pipe in the new text string that gets created. 

In summary, the first part starts with 
Article | Onsite | Sponsorship | Editorial | TFK Kid of the year | Standard
and generates
Onsite | Sponsorship | Editorial | TFK Kid of the year | Standard

The second part finds the position of the first pipe (just like we saw in Value 1). This is all passed to the LEFT() function and as a result it shows "Onsite".

The subsequent formulas just need to repeat this process in a nested format to find the subsequent pipes and return the correct number of characters.

I hope you see this and that it makes sense. 
Andi McLaughlin 12Andi McLaughlin 12
@Zachary Singer Can you tell me why my formula works for sample 1 and not for sample 2?  I am trying to grab the thrid value in both instances

Formula: 
LEFT(RIGHT(Current_Value__c, LEN(RIGHT(Current_Value__c, LEN(Current_Value__c) - FIND ("|", Current_Value__c))) - FIND ("|", RIGHT(Current_Value__c, LEN(Current_Value__c) - FIND ("|", Current_Value__c)))), FIND("|" , RIGHT(Current_Value__c, LEN(RIGHT(Current_Value__c, LEN(Current_Value__c) - FIND ("|", Current_Value__c))) - FIND ("|", RIGHT(Current_Value__c, LEN(Current_Value__c) - FIND ("|", Current_Value__c)))))-1)

Sample 1 (works)
null|Department Head|10-2020|null-null|Government|null|null|null|false

Sample 2 (doesn't work)
null|MO|US