Matching Phone Area Codes to Region
![](https://s3-us-west-2.amazonaws.com/media.forumbee.com/i/c0f38f4a-d57c-4460-94cc-181f6598a9c3/h/547.png)
So, for a lot of Data I receive, I don't have any location material besides one's phone area code. I know that the area code is not the most accurate in todays cell-phone age, however it gives me an idea. So, I already have a Phone field that is autoformatting my phone #'s to (xxx) xxx-xxxx format. I have also created a separate table for Area Codes with their matching region and time zone. Now I need a formula field that will check the (xxx) in my phone field, cross reference it with my area code table and input the corresponding region. Also, same thing for a formula field that will display time zone. Any help would be greatly appreciated. My main table is Leads. Phone field is Leads.Phone. 'Area Codes' is a separate table with 'Area Code' being the xxx field, 'Region' obviously being the region field, and 'Timezone" being the timezone field.
32 replies
-
Create 2 formula fields in your Leads table with folowing code:
Region:
let a := substr(Phone, 1, 3); first(select 'Area Codes' where 'Area Code' = a).Region
Timezone:
let a := substr(Phone, 1, 3); first(select 'Area Codes' where 'Area Code' = a).Timezone
Sample database:
-
It doesn't appear to be a relationship problem either, because your data model has no relationships...hmmm.
-
You could change the formula like this:
let a := substr(Phone, index(Phone, "(") + 1, index(Phone, ")") - 1); first(select 'Area Codes' where 'Area Code' = a).Region
And:
let a := substr(Phone, index(Phone, "(") + 1, index(Phone, ")") - 1); first(select 'Area Codes' where 'Area Code' = a).Timezone
Which does look only for the numbers between the (xxx)
-
RoSoft, so that didn't appear to be the problem. It's not updating the new Region and Time Zone fields with phone numbers that are already in the DB. It's only doing it on newly created phone numbers.
-
would I need something like this that checks the table "for x in select Table do". If so, what could be the revised formula?
-
Since I use formula fields, they are automatically filled. If you want to do this with static fields, you can use this in a Button or in the console to update all the static fields (here I use AreaStatic and TimezoneStatic for the two static fields):
for i in select Leads do let a := substr(i.Phone, index(i.Phone, "(") + 1, index(i.Phone, ")") - 1); i.(AreaStatic := first(select 'Area Codes' where 'Area Code' = a).Region); i.(TimezoneStatic := first(select 'Area Codes' where 'Area Code' = a).Timezone) end
-
That's weird. Can you post a sample database? Or give some screenshots of your table? You may also invite me to your team (as admin) to have a look at it if you want.
-
Using a Select statement in a formula field can be problematic. It needs a reason to trigger and so won't be updated in your existing records.
-
can you post a sample of your DB?
-
Sadly I can not, due to just being on the starter plan. I'm on that until my company agrees to pay for a premium subscription.
-
said:
yeah, like I said. The region and timezone fields only gets updated if it's a brand new record and I'm adding the phone number for the first time. I am on their shared cloud.Looking at your DB, in the SPE Leads table under the all table view I see that most of the records with Letter of Intent status do not have a region.
When looking at all records I see that many do have Region and Time Zone filled out.
So something is wonky. Since there is only two lines:
let a := substr(Phone, index(Phone, "(") + 1, index(Phone, ")") - 1); first(select 'Area Codes' where 'Area Code' = a).Region
It is easy to troubleshoot.
I took the first line and put it in a new formula field, that is at the end of the form view in a field called Formula
let a := substr(Phone, index(Phone, "(") + 1, index(Phone, ")") - 1); a
You will see that the ones that do not have Region filled out the is because "a" returns bad information. It is returning the last ")" of the area code. Thus it can't find anything in your Area Code table that has a ")" in it.
With a bit of more investigation I find that there is blank space at the beginning of certain phone numbers. Once you remove the space the code can now run properly. The code did not take into account the possibility of a space at the beginning. If that is a common occurrence then it will have to be updated to account for it.
-
said:
Anyway, it can do the formula once, and permanently fill in another field to save on resources?Ninox can do that. If we take your previous example of Region. The code looks like:
let a := substr(Phone, index(Phone, "(") + 1, index(Phone, ")") - 1); first(select 'Area Codes' where 'Area Code' = a).Region
Let us create a new text field called Region. Since the above code is no longer in a formula field we need to find a place to put it so it gets triggered. Maybe one place is after the phone number is entered then the code is triggered to input data into the new Region field. Another way would be to use a button. Since we are testing let us create a button as that gives us control of the execution.
Put the following into a button:
let a := substr(Phone, index(Phone, "(") + 1, index(Phone, ")") - 1); Region := first(select 'Area Codes' where 'Area Code' = a).Region
If that works as expected, then you can copy the code into the Trigger after update section of the Phone field.
Now every time a phone number is entered then the trigger will run and update the Region field.
-
said:
Now, I have never used the console before, so how do I convert this over to be used in the console for a one time deal to get the fields updated?I guess you can use the console, but I don't so I can't give any hints there.
You can use the Bulk Edit function. It is under the gear icon in your table view.
You find your field, then select Assign Calculated Value then you can put the part after the :=.
Content aside
- 1 yr agoLast active
- 32Replies
- 279Views
-
4
Following