Announcement

Collapse
No announcement yet.

Value type of datetime\diff(), how to use it in string\replace(), convert to string

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

  • Value type of datetime\diff(), how to use it in string\replace(), convert to string

    Hello guys, I am currently facing an issue with the value type of datetime\diff().

    I have this variable $pocetDnuKonecEtapy = datetime\diff(aktualEtapaKoniec, datetime\today(), 'days') which returns -1.

    But when I want to use this in the string\replace() function it does not work.

    Code:
    $upozorneni = string\replace('Do konce etapy: {dny}', '{dny}', $pocetDnuKonecEtapy);
    This is still returning 'Do konce etapy: {dny}' even if the variable returns -1.

    The problem is that datetime\diff() is not a string, not even a number (I tried it in the Formula Sandbox see code below).

    Code:
    $pocetDnuKonecEtapy = datetime\diff(aktualEtapaKoniec, datetime\today(), 'days') returns -1
    $pocetDnuKonecEtapy == -1 returns false
    $pocetDnuKonecEtapy == '-1' returns false
    So what type is it and how can I use it in the string\replace() please? Is there any way how to convert it to string, please?

    Thanks a lot in advance!
    Attached Files

  • #2
    I tried to make workaround by creating new field varchar (which should be string, right?) and then assigning the value from datetime\diff() into that varchar, which works.
    But when I want to use that varchar in string\replace() it is not working again. Am I overlooking something?
    Attached Files

    Comment


    • #3
      The hint in formula sandbox is telling me that datetime\diff() is an integer so why it is returning false when I use:
      Code:
      datetime\diff(aktualEtapaKoniec, datetime\today(), 'days') == -1
      when I use >= -1 then it returns True which is mindblowing
      ??
      Attached Files
      Last edited by Jakub Grufik; 01-19-2023, 03:08 PM.

      Comment


      • rabii
        rabii commented
        Editing a comment
        hey Jacob it is not a problem of the diff function but rather the string\replace function which accepts only string. only way to solve this is to add a custom function like (strval()) and then you can use i formula to change the output of the diff function into string to use in string\replace.

      • Jakub Grufik
        Jakub Grufik commented
        Editing a comment
        hey rabii, yep that is what I thought would be necessary because I was not able to find a way to convert to string with already existing functions. Thanks a lot for the point.
        As far as I am not php dev it will take me a bit of time to create a such custom function. But I think when I take already existing functions as an example I will be able to do it.

    • #4
      This is how you can create a custom function that converts an int into string:

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

      Code:
      <?php
      
      namespace Espo\Custom\Core\Formula\Functions\StringGroup;
      
      use Espo\Core\Formula\Functions\BaseFunction;
      use Espo\Core\Formula\ArgumentList;
      
      class StrvalType extends BaseFunction
      {
      public function process(ArgumentList $args)
      {
      $args = $this->evaluate($args);
      
      if (count($args) < 1) {
      $this->throwTooFewArguments();
      };
      
      $value = $args[0];
      
      return strval($value);
      }
      }​
      2 - Create a file custom/Espo/Custom/Resources/metadata/app/formula.json and add the code:

      Code:
      {
      "functionList": [
               "__APPEND__",
              {
                "name": "string\\strval",
                 "insertText": "string\\strval(VALUE)"
              }
      ],
      
      "functionClassNameMap": {
             "string\\strval": "Espo\\Custom\\Core\\Formula\\Functions\\StringGroup\\StrvalType"
      }
      }​
      ​
      Et voila this is what you need two files and the rebuild your session and you should have the new function on the formula.

      Good luck

      Comment


      • #5
        oh my god man rabii , it actually works!!! thanks a loooooooot. I will use your custom function creation as an example for me to learn from! thank you man!

        I have one question, please. If I will want to add another custom function, how should the formula.json syntax looks like? Is this correct approach how to extend that json, please?

        Code:
        {
            "functionList": [
                    "__APPEND__",
                    {
                        "name": "string\\strval",
                        "insertText": "string\\strval(VALUE)"
                    },
                    {
                        "name": "string\\example",
                        "insertText": "string\\example(VALUE)"
                    }​
            ],
            "functionClassNameMap": {
                   "string\\strval": "Espo\\Custom\\Core\\Formula\\Functions\\StringGroup\\StrvalType",
                   "string\\example": "Espo\\Custom\\Core\\Formula\\Functions\\StringGroup\\ExampleType"
            }
        }
        ​
        Thanks thousand times for this help man! It means a lot for me and it moves me by a mile in espocrm skillz.
        Last edited by Jakub Grufik; 01-19-2023, 05:17 PM.

        Comment


        • rabii
          rabii commented
          Editing a comment
          yes this is the correct way to add more functions.

      • #6
        rabii working perfectly!!! thank you very much
        Attached Files

        Comment


        • #7
          Those colored labels are the reason why I needed to resolve this )
          Attached Files

          Comment


          • #8
            It's possible to use numberFormat to cast an integer to a string https://docs.espocrm.com/administrat.../#numberformat

            Comment


            • rabii
              rabii commented
              Editing a comment
              thanks for sharing this.
          Working...
          X