Formula entity\sumRelated -> Filter?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • komplete22
    Junior Member
    • Jul 2018
    • 9

    Formula entity\sumRelated -> Filter?

    Hi,

    in documentation there is a example for entity\sumRelated function in formula:

    entity\sumRelated(LINK, FIELD, [FILTER]) Sums related records by a specified FIELD with an optional FILTER. (since version 5.2.6)

    Example:

    entity\sumRelated('opportunities', 'amountConverted', 'won')

    but on which field is 'won'? .. I mean... how can I give a field to the filter like "myfield=true" (boolean)
  • yuri
    Member
    • Mar 2014
    • 8440

    #2
    Hi,

    Filter won is defined here https://github.com/espocrm/espocrm/b...tunity.php#L41
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • komplete22
      Junior Member
      • Jul 2018
      • 9

      #3
      okay thanks, so a new file Taetigkeit.php under espo\custom\selectmanagers\

      with

      PHP Code:
         namespace Espo\Modules\Crm\SelectManagers;  
      
      class Taetigkeit extends \Espo\Core\SelectManagers\Base {    
      
       protected function filterVerrechenbar(&$result)
          {         $result['whereClause'][] = array(  
                'verrechenbar' => array('true')         );
          }  } 
      
      is fine? verrechenbar is a boolean field...

      Comment

      • komplete22
        Junior Member
        • Jul 2018
        • 9

        #4
        and is there a way to debug the formula?

        I always get 3,2 as an result for my formula:

        gesamtdauer = entity\sumRelated('taetigkeiten','Dauer', 'Verrechenbar')

        even though I add more taetigkeiten to the parent datarecord... the number 3,2 does not change in any way.

        Comment

        • komplete22
          Junior Member
          • Jul 2018
          • 9

          #5
          it seems like it counts every child related records into the sum...
          and not only the ones who are child from the parent record...

          maybe it would be better to implement this with a hook and a afterSave function....
          but I have no idea how I can write a method which gets all child records and iterate through them suming a field together into the parent record... maybe someone can help me here?

          Comment

          • komplete22
            Junior Member
            • Jul 2018
            • 9

            #6
            for anyone who may concern the same issue, this is the final hook for calculating fields from a child to a parent:

            PHP Code:
              $taetigkeiten = $entity->get('taetigkeiten');
             $gesamtdauer = 0;
              foreach ($taetigkeiten as $t)  {  
              $dauer = $t->get('dauer');  
              $gesamtdauer = $gesamtdauer + $dauer;
             }
              $entity->set('gesamtdauer',$gesamtdauer); 
            
            put this into custom\repositories\CaseObj.php
            into

            public function afterSave(Entity $entity, array $options = array()) { parent::afterSave($entity, $options);

            Comment

            • tanya
              Senior Member
              • Jun 2014
              • 4308

              #7
              custom/Espo/Custom/SelectManagers/[YourEntityToBeFiltered].php and change the namespace to namespace Espo\Custom\SelectManagers;
              for the last better to create a hook https://www.espocrm.com/documentatio...lopment/hooks/

              Comment

              • rodrigocoelho
                Active Community Member
                • Jun 2016
                • 296

                #8
                Originally posted by tanya
                custom/Espo/Custom/SelectManagers/[YourEntityToBeFiltered].php and change the namespace to namespace Espo\Custom\SelectManagers;
                for the last better to create a hook https://www.espocrm.com/documentatio...lopment/hooks/
                No way. This could be easier for whom is not a programmer. Why not having filter as an Attribute condition, like "fieldx == 1"

                Is it possible to implement this?

                Comment

                • rodrigocoelho
                  Active Community Member
                  • Jun 2016
                  • 296

                  #9
                  Originally posted by rodrigocoelho

                  No way. This could be easier for whom is not a programmer. Why not having filter as an Attribute condition, like "fieldx == 1"

                  Is it possible to implement this?
                  Got it working, but it is really not easy. At least for who is not a programmer.

                  Comment

                  • yuri
                    Member
                    • Mar 2014
                    • 8440

                    #10
                    It's possible to achieve w/o development using Reports feature. Create Report Filter (at administration).

                    Use:

                    entity\sumRelated('opportunities', 'amountConverted', 'reportFilter5c41a0a396f66725d');

                    where 5c41a0a396f66725d is ID of Report Filter record you can obtain from a url.
                    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

                    Comment

                    • rodrigocoelho
                      Active Community Member
                      • Jun 2016
                      • 296

                      #11
                      Great. I suggest to put this on manual.... Another great feature of advanced package. Helps a lot.

                      Comment

                      Working...