Ads

Tuesday 20 August 2013

Phone Number validation In Salesforce?

Converting 10-Lines of Apex code to a 1-line Validation Rule Formula
Code clean-up is what I'm doing these days ... lots of code clean-up. One of our Salesforce.com orgs (we have sixteen of them) currently has 72% test coverage in production. I'm not sure how the previous administrators were able to install code below the 75% threshold, but they managed. I'm tasked with getting that code cleaned up, so I can deploy a new release.

While looking for areas to improve code coverage, I stumbled upon this trigger:

trigger checkAccountPhoneNumberBiBu on Account (before insert, before update) {
   for (Account account : Trigger.new) {
      if (account.Phone==null) continue;
      Pattern p = Pattern.compile('[-() ]');
      String sPhone = p.matcher(account.Phone).replaceAll('');
      // check length without punctuation
      if (sPhone.length() != 10) account.Phone.addError(' Phone number must have 3 digit area code and 7 digit number');
      p = Pattern.compile('\\d');
      sPhone = p.matcher(sPhone).replaceAll('');
      if (sPhone.length() > 0) account.Phone.addError('Phone number must be formatted as (999)999-9999');
   }
}

This trigger looks at the value entered in the "Phone" field before an Account record is inserted or updated; if the phone field is not in the (999)999-9999 format, it errors out and notifies the user to enter the phone # in the proper format.

In addition to this Apex code, the developer also had to write a testmethod to ensure coverage of the trigger. His code was only getting 67% test coverage (which is what brought the trigger to my attention in the first place).

As I started looking at what I needed to add to the testmethod to ensure 100% coverage, I realized it would be easier to just get rid of the trigger altogether, and replace it with a Validation Rule. That 10 lines of Apex code was reduced to a 1-line formula in a validation rule:

NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))

88 comments:

  1. Awesome post, phone verification is trusted form of validation process used now. Thanks for sharing working code on trigger concept.cloud computing training centers in chennai

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

  3. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. I ‘d mention that most of us visitors are endowed to exist in a fabulous place with very many wonderful individuals with very helpful things.
    Authorized Dot Net training in chennai
    Advance Digital Marketing Training in chennai– 100% Job Guarantee

    ReplyDelete
  6. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.

    sap abap online courses

    ReplyDelete
  7. Very good brief and this post helped me alot. Say thank you I searching for your facts. Thanks for sharing with us!

    python training in chennai | python training in bangalore

    python online training | python training in pune

    python training in chennai

    ReplyDelete
  8. I prefer to study this kind of material. Nicely written information in this post, the quality of content is fine and the conclusion is lovely. Things are very open and intensely clear explanation of issues

    java training in chennai | java training in bangalore

    java online training | java training in pune

    ReplyDelete
  9. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    rpa training in Chennai | rpa training in pune

    rpa training in tambaram | rpa training in sholinganallur

    rpa training in Chennai | rpa training in velachery

    rpa online training | rpa training in bangalore

    ReplyDelete
  10. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
    Python training in marathahalli
    Python training in pune
    AWS Training in chennai

    ReplyDelete
  11. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    java training in chennai | java training in USA

    selenium training in chennai

    ReplyDelete
  12. I'm here representing the visitors and readers of your own website say many thanks for many remarkable
    python training in tambaram
    python training in annanagar
    python training in Bangalore

    ReplyDelete
  13. I have read your blog its very attractive and impressive. I like your blog salesforce Online Course

    ReplyDelete
  14. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.is article.
    Data Science course in rajaji nagar | Data Science with Python course in chenni
    Data Science course in electronic city | Data Science course in USA
    Data science course in pune | Data science course in kalyan nagar


    ReplyDelete
  15. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    Best Selenium Training in Chennai | Selenium Training Institute in Chennai | Besant Technologies

    Selenium Training in Bangalore | Best Selenium Training in Bangalore

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    ReplyDelete
  16. Outstanding blog thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us.
    Air Hostess Training in Chennai | Air Hostess Training Institute in Chennai | Air Hostess Academy in Chennai | Air Hostess Course in Chennai | Air Hostess Institute in Chennai

    ReplyDelete
  17. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
    CCNA Training in Chennai
    DevOps Training in Chennai
    DevOps certification
    DevOps Training
    Best CCNA Training Institute in Chennai
    CCNA certification in Chennai

    ReplyDelete
  18. It is an interesting post, helps me for my work. I like it, Thank you for sharing with us.

    C++ Training | C Language Training | C++ Programming Course | C Training in Adyar | C Training in Velachery | C Training in Tambaram

    ReplyDelete
  19. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    fire and safety course in chennai

    ReplyDelete
  20. Howdy, would you mind letting me know which web host you’re utilizing? I’ve loaded your blog in 3 completely different web browsers, and I must say this blog loads a lot quicker than most. Can you suggest a good internet hosting provider at a reasonable price?


    Best AWS Training Institute in BTM Layout Bangalore ,AWS Coursesin BTM


    Best AWS Training in Marathahalli | AWS Training in Marathahalli

    Amazon Web Services Training in Jaya Nagar | Best AWS Training in Jaya Nagar


    ReplyDelete
  21. Thanks for making me this article. You have done a great job by sharing this content in here. Keep writing article like this.

    Cloud Training
    Cloud Training in Chennai
    Cloud Training in OMR
    Cloud Training in Velachery
    Cloud Training in Thiruvanmiyur

    ReplyDelete
  22. Wonderful article, very useful and well explanation. Your post is extremely incredible. I will refer this to my candidates...
    Devops Training courses
    Devops Training in Bangalore
    Best Devops Training in pune
    Devops interview questions and answers

    ReplyDelete
  23. Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage
    contribution from other ones on this subject while our own child is truly discovering a great deal.
    Have fun with the remaining portion of the year.

    Selenium training in bangalore | best selenium training in bangalore

    ReplyDelete
  24. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
    rpa training in bangalore
    best rpa training in bangalore
    rpa training in pune

    ReplyDelete
  25. I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone
    Data Science Training in Chennai
    Data Science training in kalyan nagar
    Data science training in Bangalore
    Data Science training in marathahalli
    Data Science interview questions and answers
    Data science training in jaya nagar
    Data science training in bangalore

    ReplyDelete
  26. Do you have a spam issue on this website; I also am a blogger, and I wanted to know your situation;
    fire and safety course in chennai

    ReplyDelete
  27. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
    Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete
  28. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete
  29. Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me.
    Microsoft Azure online training
    Selenium online training
    Java online training
    uipath online training
    Python online training

    ReplyDelete
  30. Thanks For sharing the Information The Information Shared Is Very valuable please Keep Updating Us The InFormation Shared Is Very Valuable Python Online Training Hadoop Online Training <a href="https://nareshit.com/data-science-online-training/>DataScience Online Training</a>

    ReplyDelete
  31. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.Prathima Infotech training center bangalore

    ReplyDelete

  32. Thanks for sharing this blog. This very important and informative blog.Become a Trainer

    ReplyDelete
  33. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    business analytics course

    data analytics courses

    data science interview questions

    data science course in mumbai

    ReplyDelete
  34. Attend The Data Science Course Bangalore From ExcelR. Practical Data Science Course Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Science Course Bangalore.
    ExcelR Data Science Course Bangalore
    Data Science Interview Questions

    ReplyDelete
  35. Really awesome blog!!! I finally found a great post here.I really enjoyed reading this article. Thanks for sharing valuable information.
    Data Science Course
    Data Science Course Training in Bangalore

    ReplyDelete
  36. Thank you for sharing such a nice and interesting blog with us regarding Java. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  37. This is a real good and well written article about the best forex trading robot to trade the market with. In fact, I think that you should really take a look at this, since I think it'll really help you out when it comes to trading. Thank you for writing this article, I am indeed looking forward to your articles as I've learned a lot from it. Indeed I hope you enjoyed this article and I hope you take what you have learned here and use it to make yourself successful in the forex trading market.
    Buy old tinder accounts

    ReplyDelete
  38. Salesforce Knowledge offers four unique channels utilizing which brands can share their articles and the four distinct channels incorporate their own site as well. CRS info solutions Noida

    ReplyDelete
  39. virtual event platform According to a recent virtual edge survey of event planners, 67.4 percent said they will make a digital strategy part of live events going forward, indicating strong confidence in the hybrid future. virtual conference survey questions, sample invitation letter for an event and best event registration

    ReplyDelete
  40. That is nice article from you , this is informative stuff . Hope more articles from you . I also want to share some information about Signova

    ReplyDelete
  41. Casino Site - LuckyClub.live
    Find out everything you need to know about the Casino Site including promotions, luckyclub payment methods and customer support. Register a new account today  Rating: 3.8 · ‎Review by LuckyClub

    ReplyDelete
  42. Great post. keep sharing such a worthy information.
    AWS Training in Chennai

    ReplyDelete
  43. Upgrade your security with Invigilo's cutting-edge Video Surveillance Management Systems - a watchful eye you can trust!

    ReplyDelete
  44. "I found your blog post to be incredibly insightful! Your points about [topic] really resonated with me, and
    Solve Disconnected From Game Server LC 208 Error
    Solve Disconnected From Game Server LC 208 ErrorI appreciate how you explained [specific point] in such a clear manner. Looking forward to reading more of your content!"

    ReplyDelete
  45. Hi,
    Impressive code optimization! Transitioning from a 10-line Apex trigger to a concise 1-line Validation Rule formula is not only efficient but also enhances maintainability. Kudos on streamlining the Salesforce codebase!
    Data Analytics Courses in Nashik

    ReplyDelete
  46. Hello,
    Congratulations to the author for this excellent code cleanup! Transforming a 10-line Apex code into a concise 1-line Validation Rule Formula is not only efficient but also a great example of simplifying processes. Great job!
    Is iim skills fake?

    ReplyDelete
  47. Fantastic post! Today, phone verification is a reliable method of validation. I appreciate you sharing the trigger concept's working code.
    Data Analytics Courses in Agra

    ReplyDelete
  48. good blog
    Data Analytics Courses In Vadodara

    ReplyDelete
  49. Kudos to the author for providing such practical and actionable guidance!"
    Digital Marketing Courses in Hamburg

    ReplyDelete
  50. I'm grateful for these valuable materials, and I'm thankful for your willingness to provide them to the audience on this platform.
    daa Analytics courses in leeds

    ReplyDelete
  51. Very helpful post! Your explanation of phone number validation in Salesforce is clear and concise, emphasizing the importance of data accuracy.
    Digital marketing courses in woking

    ReplyDelete
  52. Fantastic insights into setting up phone number validation in Salesforce!
    Digital Marketing Courses in Italy

    ReplyDelete
  53. really an insightful blog post, really a great content
    Digital marketing business

    ReplyDelete
  54. Thank you for sharing detailed overview and insights on Phone Number validation In Salesforce.
    Adwords marketing

    ReplyDelete
  55. The blog post provides great and knowledgeable insights on Phone Number validation In Salesforce.
    Investment banking training Programs

    ReplyDelete
  56. Thanks for simplifying the Salesforce code! Your concise validation rule is a game-changer, making maintenance a breeze. Much appreciated!
    Investment Banking Industry

    ReplyDelete
  57. The blog covers user-friendly error messaging and considerations for international phone numbers. very useful and informative blog. Thank you for sharing.
    Data analytics framework

    ReplyDelete
  58. I was struggling with this exact issue in Salesforce. Thanks for this really useful article.
    Investment banking analyst jobs

    ReplyDelete