IfThenElse Inside IfThenElse Formula?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pablo
    Senior Member
    • Aug 2015
    • 177

    IfThenElse Inside IfThenElse Formula?

    Hi,

    We have been trying to use formulas for cases with the following criterias:

    * If string name contains "Product A", set field product to "Product A"
    * If string name contains "Product B", set field product to "Product B"
    * If string name contains "Product C", set field product to "Product C"
    * If string name contains "Product D", set field product to "Product D"

    We have been testing A LOT of formulas but haven't been able to work it. We have the following for the first two criterias using wild cards:

    ifThenElse(
    string\substring(name, 1) = '*Product A*',
    product = 'Product A'
    ifThen(
    string\substring(name, 1) = '*Product B*',
    product = 'Product B')
    );

    Are we in the right track? What are we missing?

    Thanks a lot and hope to hear from you.



  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    Hello
    string\\substring(STRING, START, LENGTH) Extracts the characters from a STRING by START position and LENGTH.

    If LENGTH is omitted, the substring starting from START until the end of the STRING will be returned.

    If LENGTH is negative, then that many characters will be omitted from the end of STRING.
    wildcard like '*' doesn't work
    string\substring(name, 1) = '*Product A*',
    Don't you have advanced pack? Workflows can do what you want
    or develop baforeSave hook for your entity (on the forum exist a lot of examples)

    Comment

    • Pablo
      Senior Member
      • Aug 2015
      • 177

      #3
      Hi tanya

      Thanks. I think we are overcomplicating things.

      We have the advanced pack. How would you create a workflow where a fields CONTAINS a certain text? Using wildcards?

      Thanks again.

      Comment

      • tanya
        Senior Member
        • Jun 2014
        • 4308

        #4
        I didn't note, contains search mode is not supported for workflows.... sorry
        you can add this function to formula

        Create application/Espo/Core/Formula/Functions/StringGroup/ContainsType.php with code
        Code:
        namespace Espo\Core\Formula\Functions\StringGroup;
        
        use \Espo\Core\Exceptions\Error;
        
        class ContainsType extends \Espo\Core\Formula\Functions\Base
        {
            public function process(\StdClass $item)
            {
                if (!property_exists($item, 'value')) {
                    throw new Error();
                }
        
                if (!is_array($item->value)) {
                    throw new Error();
                }
        
                if (count($item->value) < 2) {
                    throw new Error();
                }
        
                $haystack = $this->evaluate($item->value[0]);
                $needle = $this->evaluate($item->value[1]);
        
                if (count($item->value) > 2) {
                    $offset = $this->evaluate($item->value[2]);
                    return !(strpos($haystack, $needle, $offset) === false);
                } else {
                    return !(strpos($haystack, $needle) === false);
                }
            }
        }
        clear cache and use this function in formula and you don't need wildcard

        How to use:
        string\contains(HAYSTACK, NEEDLE, OFFSET)
        Last edited by yuri; 04-18-2017, 02:50 PM.

        Comment

        • Pablo
          Senior Member
          • Aug 2015
          • 177

          #5
          Awesome! Thanks tanya

          We followed your instructions and see the formula string\\contains(HAYSTACK, NEEDLE, OFFSET)

          Can you please specify what does each "section" (HAYSTACK, NEEDLE, OFFSET) represent?

          How would our final formula be?

          ifThenElse(
          string\contains(HAYSTACK, NEEDLE, OFFSET) = 'Product A',
          product = 'Product A'
          ifThen(
          string\contains(HAYSTACK, NEEDLE, OFFSET) = 'Product B',
          product = 'Product B')
          );

          Thanks A LOT.

          Comment

          • tanya
            Senior Member
            • Jun 2014
            • 4308

            #6
            ifThenElse(
            string\contains(name, 'Product A'),
            product = 'Product A'
            ifThen(
            string\contains(name, 'Product B'),
            product = 'Product B')
            );

            Comment

            • Pablo
              Senior Member
              • Aug 2015
              • 177

              #7
              Hi tanya ,

              We used your formula but it didn't work.

              Just to be clear, what is the result of the formula string\contains? Will it return the numeric position of where the NEEDLE exists relative to the beginning of the HAYSTACK string? Or is it just a TRUE/FALSE result?

              Thanks.

              Comment

              • tanya
                Senior Member
                • Jun 2014
                • 4308

                #8
                TRUE/FALSE result
                return !(strpos($haystack, $needle) === false);
                for me it works. I tested

                Comment

                • Pablo
                  Senior Member
                  • Aug 2015
                  • 177

                  #9
                  Hi tanya

                  This is very frustrating. We haven't been able to set it up
                  • Are you using the formula in the entity manager or the workflow module?
                  • We are trying to keep things simple for now. We are using the new formula (string\\contains(HAYSTACK, NEEDLE, OFFSET)) for cases. If we use the following formula it doesn't work:
                  Code:
                  ifThenElse(string\contains(name, 'Product A'), description = 'Product A');
                  • If we use the following formula it will set description as 'Product A' with ANY text used in name:
                  Code:
                  ifThenElse(string\contains(name, 'Product A') = true, description = 'Product A');
                  Can you please confirm your process in the tests you made? We are SO CLOSE to accomplish it!

                  Thanks again for your valuable time.

                  Comment

                  • tanya
                    Senior Member
                    • Jun 2014
                    • 4308

                    #10
                    Yes, we are talking about formula
                    Code:
                    ifThenElse(string\contains(name, 'sort'), description='RESORT', description='NOSORT');
                    this was my test
                    Your example works as well (ifThenElse(string\contains(name, 'Product A'), description = 'Product A') Check data/logs

                    Comment

                    • Pablo
                      Senior Member
                      • Aug 2015
                      • 177

                      #11
                      Hi tanya

                      We see the following error in data/logs

                      [2017-04-20 14:19:43] Espo.ERROR: Formula failed: Class \Espo\Core\Formula\Functions\StringGroup\ContainsT ype was not found. [] []

                      Strange. We are using the same code in ContainsType and it's located in application/Espo/Core/Formula/Functions/StringGroup. You can see the ContainsType file we are using here.

                      What can it be?

                      Thanks a lot.

                      Comment

                      • tanya
                        Senior Member
                        • Jun 2014
                        • 4308

                        #12
                        Check the permission of this file, please

                        Comment

                        • Pablo
                          Senior Member
                          • Aug 2015
                          • 177

                          #13
                          Permission for ContainsType.php file is 644.

                          Should we change it to 777?

                          Thanks.

                          Comment

                          • tanya
                            Senior Member
                            • Jun 2014
                            • 4308

                            #14
                            Should be just readable for server user. Check file owner, please

                            Comment

                            • Pablo
                              Senior Member
                              • Aug 2015
                              • 177

                              #15
                              tanya GOOD NEWS!

                              The "issue" was that we didn't create the ContainsType.php to begin with <?php tag (I know, silly mistake).

                              Now it is working PERFECTLY! This is the final ContainsType.php

                              Thanks a LOT for your patience, time and knowledge.

                              You have saved us!

                              Best regards.

                              Comment

                              Working...