Announcement

Collapse
No announcement yet.

Custom function squareroot for formula

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

  • Custom function squareroot for formula

    I would like to do some calculations with formula, but currently it does not seems to have squareroot function.

    I have seen in the documentation that you can add custom functions to formula:
    Formula functions - EspoCRM Documentation

    Also PHP has a function for square root:
    PHP: sqrt - Manual

    How would i be able to implement this?

  • #2
    You just need a custom formula function that will calculate the sauqreroot using php build in function sqrt(), try this below:

    1 - Create a file custom/Espo/Custom/Core/Formula/Functions/NumberGroup/SquareRootType.php with the code:

    PHP Code:
    <?php


    namespace Espo\Custom\Core\Formula\Functions\NumberGroup;

    use 
    Espo\Core\Formula\Functions\BaseFunction;
    use 
    Espo\Core\Formula\ArgumentList;

    class 
    SquareRootType extends BaseFunction
    {
        public function 
    process(ArgumentList $args)
        {
            if (
    count($args) < 1) {
                
    $this->throwTooFewArguments();
            }

            
    $value $this->evaluate($args[0]);

            if (!
    is_foat($value) || !is_numeric($value)) {
                return 
    null;
            }

            return 
    float sqrt($value);
        }
    }

    2 - Create a file custom/Espo/Custom/Resources/metadata/app/formula.json and add the code:​
    PHP Code:
    {
        
    "functionList": [
            
    "__APPEND__",
            {
                
    "name""number\\sqrt",
                
    "insertText""number\\sqrt(VALUE)",
                
    "returnType""float"
            
    }
        ],

        
    "functionClassNameMap": {
            
    "number\\sqrt""Espo\\Custom\\Core\\Formula\\Functions\\NumberGroup\\SquareRootType"
        
    }
    }
    ​ 

    Go to administration clear cache and rebuild the system and if you go to formula you should have a new function number\\sqrt()

    I hope this helps

    Comment


    • #3
      Thank you so much for your time and help. I made the files but get an error:

      ERROR: Slim Application Error Type: ParseError Code: 0 Message: syntax error, unexpected identifier &quot;sqrt&quot;, expecting &quot;;&quot; File: /var/www/vhosts/....../custom/Espo/Custom/Core/Formula/Functions/NumberGroup/SquareRootType.php Line: 23 Trace: #0 /var/www/vhosts/....../vendor/composer/ClassLoader.php(428): Composer\Autoload\includeFile() #1 [internal function]: Composer\Autoload\ClassLoader->loadClass() #2

      Line 23 seems to be " return float sqrt($value);"

      What could be the issue here?

      Comment


      • #4
        when you copy code from the forum a color hidden carater will be added, please code the code to a code editor and remove any extra caracter then copy and pose to your instance files.

        Comment


        • #5
          this is what i mean
          Attached Files

          Comment


          • #6
            I checked and am unable to find an extra character in the first file. Copied in Notepad++, even tried in MS Word with all hidden characters view in hope to find it...but i haven't. I did find it in the second, but that didn't seem to make a difference.

            Is there anything else i could check?

            Comment


            • #7
              Try this instead, there was some error in previous code, sorry:

              PHP Code:
              <?php


              namespace Espo\Custom\Core\Formula\Functions\NumberGroup;

              use 
              Espo\Core\Formula\Functions\BaseFunction;
              use 
              Espo\Core\Formula\ArgumentList;

              class 
              SquareRootType extends BaseFunction
              {
                  public function 
              process(ArgumentList $args)
                  {
                      if (
              count($args) < 1) {
                          
              $this->throwTooFewArguments();
                      }

                      
              $value $this->evaluate($args[0]);

                      if (!
              is_numeric($value)) {
                          return 
              $this->throwBadArgumentType(1'numeric');
                      }

                      return 
              sqrt($value);
                  }
              }

              Comment


              • #8
                I have tested this last code and works fine.

                Comment


                • #9
                  Originally posted by rabii View Post
                  I have tested this last code and works fine.
                  You are a hero. Thank you so much!

                  Comment

                  Working...
                  X