Announcement

Collapse
No announcement yet.

Retrieve type from phone number

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Retrieve type from phone number

    Hello everyone,
    does anyone have any idea how I can read the type of phone number from the contacts?

    The idea behind it:
    I want to create a whatsapp button in the contact entity, but only if the phone number is a cell phone number.
    If it is a cell phone number, then a formula should write the link in the button.
    If it is another number, e.g. a landline number, then it should not write anything.
    I use the link button extension from Kharg .

    With this formula it only writes the first number:
    ifThenElse(phoneNumber != null, whatsapp=string\concatenate('https://wa.me/', phoneNumber), whatsapp = null);​
    Last edited by ChrisSka83; 04-15-2024, 12:01 AM.

  • #2
    Hi, You can check in phoneNumberData. It's an array of objects. An item contains information for a single email address.

    Comment


    • #3
      Hi ChrisSka83,

      You can get the primary phone number Type value using the following formula script (using the last line you can test it in Administration > Formula Sandbox by selecting any contact):
      Code:
      $phoneNumberData = json\encode(phoneNumberData);
      $phoneNumberType = json\retrieve($phoneNumberData, string\concatenate(0, '.type'));
      output\printLine($phoneNumberType);
      To get Type values for all phone numbers (not just the primary one), you can use the following script:
      Code:
      $phoneNumbersCount = array\length(phoneNumberData);
      
      $phoneNumberData = json\encode(phoneNumberData);
      
      $i = 0;
      
      while ($i < $phoneNumbersCount) {
      
        $phoneNumberType = json\retrieve($phoneNumberData, string\concatenate($i, '.type'));
      
        $phoneNumbersTypesArray = array\push($phoneNumbersTypesArray, $phoneNumberType);
      
        $i = $i + 1;
      }
      
      $phoneNumbersTypes = array\join($phoneNumbersTypesArray, '\n');
      
      output\printLine($phoneNumbersTypes);
      Please keep in mind that if you get red dots at the beginning of lines when pasting these formula scripts, simply erase them.​

      Comment


      • #4
        Ok, reading out the data has already worked.
        But how do I now pass on the phone number if the type is mobile?
        I thought I could solve this with an If query, but unfortunately nothing came of it.​
        Attached Files

        Comment


        • #5
          Hi ChrisSka83

          Try this code, it should work

          PHP Code:
          $i 0;

          while (
          $i array\length(phoneNumberData)) {
            
            
          $type object\get(array\at(phoneNumberData$i), 'type');
            
            if (
          $type == 'Mobile') {
              
          $mobile object\get(array\at(phoneNumberData$i), 'phoneNumber');
              break;
            }
            
          $i $i 1;
          }
          ​ 

          The code simply iterate through the phone number data and if a mobile type exists then it will be assigned to a $mobile variable and it will break the loop.
          Rabii
          Web Dev

          Comment


          • #6
            Thank you

            your script does exactly what I had in mind.
            My script now looks like this:

            PHP Code:
            $i 0;

            while (
            $i array\length(phoneNumberData)) {
              
              
            $type object\get(array\at(phoneNumberData$i), 'type');
              
              if (
            $type == 'Mobile') {
                
            $mobile object\get(array\at(phoneNumberData$i), 'phoneNumber');
                break;
              }
              
            $i $i 1;
            }

            ifThenElse($mobile != nullwhatsapp=string\concatenate('https://wa.me/'$mobile), whatsapp null);
            ifThenElse($mobile != nullhandynummervorhanden truehandynummervorhanden false);​ 

            Now I have added a bool field, if mobile exists, then it will be set to true.
            So that I can check whether a mobile number is available and show the button, otherwise hide it.
            I actually wanted to shorten the formula, but the following formula didn't work somehow:


            PHP Code:
            ifThenElse($mobile != nullwhatsapp=string\concatenate('https://wa.me/'$mobile) && handynummervorhanden truewhatsapp null && handynummervorhanden false); 

            Comment


            • #7
              may be try this

              PHP Code:
              while ($i array\length(phoneNumberData)) {
                
                
              $type object\get(array\at(phoneNumberData$i), 'type');
                
                if (
              $type == 'Mobile') {
                  
                  
              $mobile object\get(array\at(phoneNumberData$i), 'phoneNumber');
                  
                  if (
              $mobile) {
                      
              whatsapp string\concatenate('https://wa.me/'$mobile);
                      
              handynummervorhanden true;
                  }
                  break;
                } else {
                      
              whatsapp null;
                      
              handynummervorhanden null;
                }

                $i 
              $i 1;
              }
              ​ 
              Last edited by rabii; 04-15-2024, 11:32 AM.
              Rabii
              Web Dev

              Comment


              • ChrisSka83
                ChrisSka83 commented
                Editing a comment
                An error has crept into your code.
                I only noticed it when I had a contact with just a landline number and no mobile number.

                } else {
                whatsapp = null;
                handynummervorhanden = null; <- false must be entered here, not null

              • rabii
                rabii commented
                Editing a comment
                Sorry i missed that one, good you spotted that error.
                Thanks

            • #8
              Great, works perfectly.

              Many thanks​ rabii

              Comment


              • rabii
                rabii commented
                Editing a comment
                you are welcome

            • #9
              Are you combining this with Kharg: https://forum.espocrm.com/forum/exte...ge2#post104838 ?

              Sound like I need to copy this idea, too bad we don't use WhatApp in our country often enough

              Comment


              • ChrisSka83
                ChrisSka83 commented
                Editing a comment
                Of course it can be copied. That's what the forum is for, to exchange information.
                You don't have to use WhatsApp. You can use any app.
                I would have liked to use Telegram, but it's by username and not by cell phone number.
            Working...
            X