function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Margot Fabre 16Margot Fabre 16 

Find out if a string as "At least 5 numbers"

Hi 
I need to check a checkbox field "Has Phone" if the phone number field as at least 5 digits 
( The reason for 'at least 5' rather than just 'not blank' is to avoid including "1" and the reason for  '5 numbers' rather than '5 characters' is to avoid including words like "no number") 

We're just trying to check if the contact has a valid phone number but we want to leave the sales people the ability to write anything in the field like reception, ext., etc ... 

Any ideas ? 
Thanks a lot 
Best Answer chosen by Margot Fabre 16
Abhishek BansalAbhishek Bansal
Hi Margot,

Please find the updated code below:
trigger updatePhone on Contact (before insert, before update) {
    Integer countPhone;
    Integer countMobile;
    Integer countDDI;
    for(Contact con : trigger.new) {
        if(con.Phone != null) {
            countPhone = 0;
            for(Integer i=0; i<con.Phone.length(); i++) {
                if(con.Phone.subString(i, i+1).isNumeric()) {
                    countPhone++;
                }
            }
        }
        if(con.MobilePhone != null) {
            countMobile = 0;
            for(Integer i=0; i<con.MobilePhone.length(); i++) {
                if(con.MobilePhone.subString(i, i+1).isNumeric()) {
                    countMobile++;
                }
            }
        }
        if(con.DDI__c != null) {
            countDDI = 0;
            for(Integer i=0; i<con.DDI__c.length(); i++) {
                if(con.DDI__c.subString(i, i+1).isNumeric()) {
                    countDDI++;
                }
            }
        }
        if(countPhone >= 5 || countMobile >=5 || countDDI >=5) {
            //Replace Has_Phone__c with API name of field
            con.Has_Phone__c = true;
        }
        else {
            //Replace Has_Phone__c with API name of field
            con.Has_Phone__c = false;
        }
    }
}

Thanks,
Abhishek Bansal.​​​​​​​

All Answers

Abhishek BansalAbhishek Bansal
Hi Margot,

You can use the below formula:
AND(
ISNUMBER(Phone),
LEN(Phone) <= 5
)

It will work when you have numeric value in Phone.

Thanks,
Abhishek Bansal.​​​​​​​
Margot Fabre 16Margot Fabre 16
Hi 
Thanks for the quick answer :)  
But not this does not work
I have "05968474893 -ext 1343" in my phone field then the ISNUMBER formula doesn't work. 

Any other ideas 

Thanks 


 
Abhishek BansalAbhishek Bansal
Hi Margot, 

I think your requirement is to check 5 numbers in the Phone field and along with that it can also have alphabets or symbols also. So for this you need to write the logic in trigger as there is no formula to handle this.
Let me know if you need any help with the trigger code.

Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102
Margot Fabre 16Margot Fabre 16
Abhishek, 
This is what I was afraid of ... 

I'd love some help in writting the trigger, I have never done it before :)

Thanks again ! 
Abhishek BansalAbhishek Bansal
Hi Margot,

Please find the trigger code below:
trigger updatePhone on Contact (before insert, before update) {
    Integer count;
    for(Contact con : trigger.new) {
        if(con.Phone != null) {
            count = 0;
            for(Integer i=0; i<con.Phone.length(); i++) {
                if(con.Phone.subString(i, i+1).isNumeric()) {
                    count++;
                }
            }
        }
        if(count >= 5) {
            //Replace Has_Phone__c with API name of field
            con.Has_Phone__c = true;
        }
        else {
            //Replace Has_Phone__c with API name of field
            con.Has_Phone__c = false;
        }
    }
}

Please take care of the comments and replace the field API name. Let me know if there is an issue.

Thanks,
Abhishek Bansal.
Margot Fabre 16Margot Fabre 16
Hi 
Thank you so much for this ! 

I have forgotten to mention that it should be based on 3 different phone fields DDI__c, MobilePhone and Phone 
Would you mind adding the "OR logic" into the trigger ? 

Thank you 
Abhishek BansalAbhishek Bansal
Hi Margot,

Can you please provide some more details? Do we need to check each field separately or combine the value from all the three and then check if the cobined string has 5 numbers. Please clarify.

Thanks,
Abhishek Bansal.
Margot Fabre 16Margot Fabre 16
Abhishek, 

If any of these fields has at least 5 number , then the "Has phone" field has to be checked 
Ex 1 : 
MobilePhone = 05968474893 -ext 1343
Phone = "no phone"
DDI__c = blank 
--> Has phone = check 

Ex 2 : 
MobilePhone = 123
Phone = "no phone"
DDI__c = blank 
--> Has phone = Uncheck 

Hope this makes sense 

Thanks 
Abhishek BansalAbhishek Bansal
Hi Margot,

Please find the updated code below:
trigger updatePhone on Contact (before insert, before update) {
    Integer countPhone;
    Integer countMobile;
    Integer countDDI;
    for(Contact con : trigger.new) {
        if(con.Phone != null) {
            countPhone = 0;
            for(Integer i=0; i<con.Phone.length(); i++) {
                if(con.Phone.subString(i, i+1).isNumeric()) {
                    countPhone++;
                }
            }
        }
        if(con.MobilePhone != null) {
            countMobile = 0;
            for(Integer i=0; i<con.MobilePhone.length(); i++) {
                if(con.MobilePhone.subString(i, i+1).isNumeric()) {
                    countMobile++;
                }
            }
        }
        if(con.DDI__c != null) {
            countDDI = 0;
            for(Integer i=0; i<con.DDI__c.length(); i++) {
                if(con.DDI__c.subString(i, i+1).isNumeric()) {
                    countDDI++;
                }
            }
        }
        if(countPhone >= 5 || countMobile >=5 || countDDI >=5) {
            //Replace Has_Phone__c with API name of field
            con.Has_Phone__c = true;
        }
        else {
            //Replace Has_Phone__c with API name of field
            con.Has_Phone__c = false;
        }
    }
}

Thanks,
Abhishek Bansal.​​​​​​​
This was selected as the best answer
Margot Fabre 16Margot Fabre 16
This is perfect 

Thank you ! 
Margot Fabre 16Margot Fabre 16
Me again ...
I have deployed the trigger and it works well on edit of a contact 
I have used data loader to mass update all contacts (using a custom checkbox that I checked to mass update)  but for some reason it has not been picked up 
Aren't trigger running on mass update with data loader ? Any idea on how I can mass update my 22K contacts ? 

Thanks a lot 
Margot
Abhishek BansalAbhishek Bansal
You can use batch class to update contact records.
Margot Fabre 16Margot Fabre 16
Hi - 
Sorry not technical enough to get that - I understand the logic but I don't know how to do it 
Can you help me with this please too ? 

So it's normal the records have not been updated with PB ? Aren't the triggers launched on edit of the contact ? So If I edited them all with my PB it shouldn't have worked ? Is this specific to trigger ? 
Just trying to understand what is happening here :) ! 

Thanks
Margot