Posted by: eliz_beth | November 2, 2017

Checking for a Palindrome Declaratively in Salesforce

I am definitely more the declarative developer in the Salesforce world.  Today I was asked how I would check to see if a name was a palindrome.  I love palindromes- they’re special to me, like a word version of a prime number, perfect unto itself.  Challenge accepted!

My first thought was okay, I just need to reverse the name and compare it to the original.  That’s easy enough, right?  Well, there’s no “reverse” formula function that I’m aware of in Salesforce.  Darn.  I then considered LEFT() and RIGHT() and some complicated way of comparing those and I didn’t even want to try working that out.  There had to be a more relatively simple way to do it that I was overlooking!

I thought through other functions and remembered one I’ve not used very often, MID(fieldname, place#, #toreturn).  MID() works by returning the character(s) identified in the field counting from the left with to the place#, and the final component telling how many characters including that place to return.  So if the palindrome name is Salesforce, to return the letter “f’” in Salesforce, you would write: MID(Palindrome_name__c, 6, 1).  I could leverage MID() to rebuild the name from right to left and compare that to the original for a true or false result.  So for this case, let’s start with a 10 character text field palindrome_name__c field.  Whatever the name we want to check to see if it is a palindrome, we would enter here.

Next we need to generate the palindrome.  So we create a new custom field, for simplicity sake named “palindrome_right__c”, type formula, returning text with the following formula:

Mid( Palindrome_name__c ,10,1)+Mid( Palindrome_name__c ,9,1)+Mid( Palindrome_name__c ,8,1)+Mid( Palindrome_name__c ,7,1)+Mid( Palindrome_name__c ,6,1)+Mid( Palindrome_name__c ,5,1)+Mid( Palindrome_name__c ,4,1)+Mid( Palindrome_name__c ,3,1)+Mid( Palindrome_name__c ,2,1)+Mid( Palindrome_name__c ,1,1)

So our formula above pulls the characters from the field and reverses their order. You could do this for any length, you just have to follow the pattern.  Keep it within reasonable limits in terms of length to begin with as you’ll run into compile length issues for anything extremely long.  My example above needs to be cleaned up, but in it’s current state compiles at 567 characters, which is under the 5000 character limit for formula fields.  My example will allow for roughly 84 characters to return, if you need to check more, you could just split it into different fields and combine the results in the formula test field for the comparison.

So now you have the name (palindrome_name__c), the reverse of the name (palindrome_right__c),  you just have to compare them.  This should be simple, right?  For the most part, yes!

Create another custom formula field called palindrome_test__c and compare the two with the following formula and return the result of True or False:

If(LOWER(Palindrome_name__c)=LOWER(Palindrome_right__c),”True”,”False”)

That’s it!  If it is the same forward and backward, it will return True, otherwise False.

But I said “For the most part” so what is the catch?

I’m assuming two things:

1- my palindrome_name__c field is written in traditional English style, left to right.
2- my requirement is not concerned with case sensitivity.

Not all languages read left to right, so you would need to be aware and adjust accordingly.  If your user typed in “Anna” for the palindrome_name__c, the formula for palindrome_right__c would return “annA”.  “Anna” and “annA” are not equal, but using either UPPER() or LOWER() function to eliminate case in the test formula allows the palindrome_name__c “Anna” to be recognized as a palindrome.

I hope you liked this exercise, I certainly enjoyed the challenge!  Do you have a better way of approaching this without resorting to code?  Have another aspect to consider I’ve not covered in my assumptions?  Share your approach and thoughts by commenting below.

 

 

 

 


Responses

  1. Thank you for sharing great article related to salesforce.


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 )

Facebook photo

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

Connecting to %s

Categories

%d bloggers like this: