I have a custom object which stores the ranges for the mobile numbers and fixed line numbers. Mobile = 477.100.001 - 477.100.999 - This is one single record with FirstPhoneNumber & LastPhoneNumber ranges (I have such 2000 records) Fixed = 525.100.001 - 525.100.999 (2000 records for this too)

I have 2 custom fields on Account object, Mobile & Fixed, so I need to validate when the user enters out of the range.

The problem now is the Sales reps are entering the mobile in Fixed and the Fixed in Mobile, so need to validate and display an error.

I need to handle this in a trigger (should handle bulk data loads too), so first thoughts for me is implementing it using a MAP and iterating over the MAP.

Any good suggestions / advice here?


  • If you're storing these as records, why not use a VLOOKUP in a regular validation rule? See na14.salesforce.com/help/doc/en/… for examples.
    – Mike Chale
    Jul 11, 2013 at 16:57
  • Vlookup does an exact match, but I am just storing ranges and not the exact numbers. Will Vlookup help here then? Jul 11, 2013 at 17:19

3 Answers 3


Iterating over the map could cause you to run out of script statements. Can you create a map that looks like:

Map<Integer,Range> mobPrefixToRangeMap;
Map<Integer,Range> fixedPrefixToRangeMap;
private class Range{
  Integer low;
  Integer high;
  public Range(Integer low, Integer high) {
     this.low = low;
     this.high = high;

And then read all the custom object rows, populating the two maps (loop of 2000 calls each to the inner class constructor).

Your validation merely needs to take the incoming trigger row's prefix and do a statement like this (verifying a mobile number):

if (mobPrefixToRangeMap.contains(pfxIn) && 
    mobPrefixToRangeMap.get(pfxIn).low <= numberIn &&
    mobPrefixToRangeMap.get(pfxIn).high >= numberIn) { /* valid */
   /* invalid */

I haven't researched validating phone numbers so the above may need adaptation. The goal is to avoid running out of script statements as the trigger will have up to 200 rows so you can't do heavy work in validation of each row; do the script intensive stuff once in the setup

  • Yes, iterating over maps did cause run out of script statements. The above approach you mentioned makes more sense. Thanks Jul 13, 2013 at 5:56

Why are you keeping the valid ranges in a single record on a custom object? You can use custom settings for that. On the custom setting, make the FirstPhoneNumber and LastPhoneNumber number fields. So maybe create a hierarchy custom setting, with four fields Mobile Start Number, Mobile End Number, Fixed Start Number, and Fixed End Number.

Then do this in the trigger: strip out all the punctuation, etc from the number that the user entered, so if they enter 477.100.001 do this in the trigger:

string userEnteredMobile = record.Mobile.replace('.','');//do other replacing here too

That way you're left with 477100001. Convert this to an Integer.

Now you can just compare this to the custom settings:

if (userEnteredMobile < custom_setting__c.mobile_first_number__c  || userEnteredMobile > custom_setting__c.mobile_last_number__c) {
  • Valid ranges are not just 2 records, they are 2k records with fields firstphonenumber and lastphonenumber, so custom settings won't work here. Jul 12, 2013 at 1:39

Assuming that you enforce consistent formatting for all your phone number fields, the following query in your trigger should do the trick:

[SELECT COUNT() from Custom_Object__c where contactPhone > FirstPhoneNumber && contactPhone < LastPhoneNumber];

If this return 1 or more, the entered phone number is within range, else throw an error.

  • This answer isn't bulk friendly, though.
    – sfdcfox
    Jul 12, 2013 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .