A funny thing happened….

A funny thing happened to me on the way to my next blog post. I was planning to write about wearing many hats that I alluded to in the last post but something a lot neater came along. I was stuck on a problem with a field validation rule and was getting nowhere with the solution I wanted / needed.

So where do you go when the great Google fails?…the community!…So off I went to the Who owes me a beer?!? user group (one of many I belong to).

The question I posted was pretty straight forward:

“I have a text field that is 6 characters long and I want to prevent users from entering a leading 0, like 012345. I Googled and read as much as I thought it would help so now I come to ask the real experts. I’m assuming RegEx is the way to go? And just what may that way look like. REGEX(Number__c, What goes here?)”

The first response came within two hours from Jeremiah Dohn. He more than addressed my question. His reply came with not only an answer; it also came with a link to a great presentation that he had given previously. It does a wonderful job of explaining RegEX.

“You could do this with:
NOT(ISBLANK(Number__c)) &&
NOT(REGEX(Number__c, “[1-9]\\d{5}”))

This is assuming that you are requiring that there are exactly 6 numbers. If you want to have less numbers required, you can use\\d{1,5}, which assumes that you have two total digits, counting the initial one…“

I’ve attached the link to the presentation so if you have a Salesforce login you can access it. This is a good read to get more familiar with the topic.

The second suggestion came from John Sadler that used a much simpler but equally effective solution. It was a bit of a head-smacker where I read it.

“If struggling with REGEX you could go with a much simpler solution LEFT(Number__C,1)=”0″. “0” because this is a Text Field. If the ” ” are left off you will get a syntax error because the rule is expecting text. Only works for Leading 0, so it will allow a 0 elsewhere in your text field….”

I want to thank both of them for their help and for giving me something fun to write about. What made this fun? In my last post I said “I’m not saying it’s the right answer or wrong answer but it is an answer that I arrived at. That’s the wonderful thing about SF.com’s user community – we love to share.” This was such a great real world example of how two equally valid solutions to the same question came from two people willing to take the time to help out another person and share the benefits of their knowledge. I hope you enjoyed the story.


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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s