Custom function squareroot for formula

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbrands
    Junior Member
    • Feb 2018
    • 23

    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?
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #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
    Rabii
    Web Dev

    Comment

    • mbrands
      Junior Member
      • Feb 2018
      • 23

      #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

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #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.
        Rabii
        Web Dev

        Comment

        • rabii
          Active Community Member
          • Jun 2016
          • 1250

          #5
          this is what i mean
          Attached Files
          Rabii
          Web Dev

          Comment

          • mbrands
            Junior Member
            • Feb 2018
            • 23

            #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

            • rabii
              Active Community Member
              • Jun 2016
              • 1250

              #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);
                  }
              }
              Rabii
              Web Dev

              Comment

              • rabii
                Active Community Member
                • Jun 2016
                • 1250

                #8
                I have tested this last code and works fine.
                Rabii
                Web Dev

                Comment

                • mbrands
                  Junior Member
                  • Feb 2018
                  • 23

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

                  Comment

                  Working...