Help appreciated: anybody can provide a hook with both afterRelate & afterUnrelate?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chrisjiang
    Member
    • Dec 2018
    • 88

    Help appreciated: anybody can provide a hook with both afterRelate & afterUnrelate?

    Hi,

    I have created an entity called Event and it has relationship with Contact. I have an "entryCount" field to calculate the contacts linked/related to the event:

    1. When a new contact is chosen to link/related to an event, the value of field "entryCount" of that event shall increase by 1.
    2. When a linked/related contact is chosen to unlink/unrelated to an event, the value of field "entryCount" of that event shall decrease by 1.

    Currently I can only do this by workflow but it has a time delay.

    I know creating customized hook is the best solution to this but I can not find any example sources (and I am not a professional engineer either). So I would be more than thankful if anyone can help me with such hook codes.

    Thanks in advance.

    Chris
    Last edited by chrisjiang; 12-28-2019, 02:16 PM.
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #2
    Hello,
    easy way : https://www.espocrm.com/documentatio...ation/formula/
    entity\countRelated


    entity\countRelated(LINK, [FILTER]) Returns a number of related records with an optional FILTER applied.

    Example:

    entity\countRelated('opportunities', 'open')

    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    • chrisjiang
      Member
      • Dec 2018
      • 88

      #3
      Originally posted by item
      Hello,
      easy way : https://www.espocrm.com/documentatio...ation/formula/
      entity\countRelated


      entity\countRelated(LINK, [FILTER]) Returns a number of related records with an optional FILTER applied.

      Example:

      entity\countRelated('opportunities', 'open')
      Thanks for helping.

      This is the way I currently use. The problem is the formula can not work dynamically, i.e. it can not automatically calculate and update the entryCount filed at the time of link or unlink. It depends on updating the record, or by selecting "Recalculating Formula" in the Listview.

      That is why I am looking for help on a hook code. I believe such function exists in TargetList as its "entryCount" field works exactly in a dynamically updating way. But I do not know where to look for such code of the function.

      Thanks again.

      Comment

      • item
        Active Community Member
        • Mar 2017
        • 1476

        #4
        Hello,
        nice info "like targetList" so i have look and result

        create a service for your entity : here is for Account .. and Contact
        On Account : where i realte a contact -> i just need make a refresh and the count is +1
        On Contact : where i relate a Account -> when i go .. the count is +1
        And unrelate work to .. no need to modifie these two Enitty.

        Just one "not my skill" => "auto refresh/render" .. so no need to "refresh" manually the browser

        Regards

        /custom/Espo/Custom/Services/Account.php
        PHP Code:
        <?php
        namespace Espo\Custom\Services;
        
        use \Espo\ORM\Entity as Entity;
        
        class Account extends \Espo\Modules\Crm\Services\Account
        {
        
             public function loadAdditionalFields(Entity $entity)
            {
                parent::loadAdditionalFields($entity);
                $this->loadEntryCountField($entity);
            }
        
            public function loadAdditionalFieldsForList(Entity $entity)
            {
                parent::loadAdditionalFields($entity);
                $this->loadEntryCountField($entity);
            }
        
            protected function loadEntryCountField(Entity $entity)
            {
                $count = 0;
                $count += $this->getEntityManager()->getRepository('Account')->countRelated($entity, 'contacts');
                $entity->set('countRelatedContact', $count);
            }
        }
        If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

        Comment

        • chrisjiang
          Member
          • Dec 2018
          • 88

          #5
          Originally posted by item
          Hello,
          nice info "like targetList" so i have look and result

          create a service for your entity : here is for Account .. and Contact
          On Account : where i realte a contact -> i just need make a refresh and the count is +1
          On Contact : where i relate a Account -> when i go .. the count is +1
          And unrelate work to .. no need to modifie these two Enitty.

          Just one "not my skill" => "auto refresh/render" .. so no need to "refresh" manually the browser

          Regards

          /custom/Espo/Custom/Services/Account.php
          PHP Code:
          <?php
          namespace Espo\Custom\Services;
          
          use \Espo\ORM\Entity as Entity;
          
          class Account extends \Espo\Modules\Crm\Services\Account
          {
          
          public function loadAdditionalFields(Entity $entity)
          {
          parent::loadAdditionalFields($entity);
          $this->loadEntryCountField($entity);
          }
          
          public function loadAdditionalFieldsForList(Entity $entity)
          {
          parent::loadAdditionalFields($entity);
          $this->loadEntryCountField($entity);
          }
          
          protected function loadEntryCountField(Entity $entity)
          {
          $count = 0;
          $count += $this->getEntityManager()->getRepository('Account')->countRelated($entity, 'contacts');
          $entity->set('countRelatedContact', $count);
          }
          }
          Thank you so much for the help. It WORKS!!!

          Now the only thing left is what you mentioned: Just one "not my skill" => "auto refresh/render" .. so no need to "refresh" manually the browser:

          It needs manual refresh of page to show the result of the field.

          I think the "auto refresh/render" may also exist in the TargetList module -

          But anyway I can enjoy the function with manual refresh now.

          Thank you so much for your kind help and wish you a happy and prosperous new 2020 year!!

          Comment

          • item
            Active Community Member
            • Mar 2017
            • 1476

            #6
            Hello,
            add this on client/custom/src/view/account/detail.js

            PHP Code:
            Espo.define('custom:views/account/detail', 'crm:views/account/detail', function (Dep) {
            
            return Dep.extend({
                setup: function () {
                    Dep.prototype.setup.call(this);
                    this.listenTo(this.model, 'after:relate', function () {
                        this.model.fetch();
                    }, this);
            
                    this.listenTo(this.model, 'after:unrelate', function () {
                        this.model.fetch();
                    }, this);
            
                });
            }); 
            
            If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

            Comment

            • chrisjiang
              Member
              • Dec 2018
              • 88

              #7
              Originally posted by item
              Hello,
              add this on client/custom/src/view/account/detail.js

              PHP Code:
              Espo.define('custom:views/account/detail', 'crm:views/account/detail', function (Dep) {
              
              return Dep.extend({
              setup: function () {
              Dep.prototype.setup.call(this);
              this.listenTo(this.model, 'after:relate', function () {
              this.model.fetch();
              }, this);
              
              this.listenTo(this.model, 'after:unrelate', function () {
              this.model.fetch();
              }, this);
              
              });
              }); 
              
              Thanks so much for your kind help. But unfortunately it does not work. My case is that I created a customized entity called "Event". Maybe I should put this file in a different folder?

              Thanks again!!

              Comment

              • item
                Active Community Member
                • Mar 2017
                • 1476

                #8
                Hello,
                of Course ... https://www.espocrm.com/documentatio.../custom-views/

                you must adapt :
                custom/Espo/Custom/Resources/metadata/clientDefs/YourEntityType.json
                client/custom/src/views/your-entity-type/record/detail.js

                and this : Espo.define('custom:views/you-entity-type/detail', 'crm:views/detail', inherit from base

                If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

                Comment

                • chrisjiang
                  Member
                  • Dec 2018
                  • 88

                  #9
                  Originally posted by item
                  Hello,
                  of Course ... https://www.espocrm.com/documentatio.../custom-views/

                  you must adapt :
                  custom/Espo/Custom/Resources/metadata/clientDefs/YourEntityType.json
                  client/custom/src/views/your-entity-type/record/detail.js

                  and this : Espo.define('custom:views/you-entity-type/detail', 'crm:views/detail', inherit from base

                  Hi,

                  I have tried different combinations but with bad luck.

                  Just want to bother you for more details: (my customized entity is called "event"

                  1. Shall I put all the following code in custom/Espo/Custom/Resources/metadata/clientDefs/event.json?

                  { "views": { "list": "custom:views/event/list", "detail": "custom:views/event/detail", "edit": "custom:views/event/edit" }, "recordViews": { "list": "custom:views/event/record/list", "detail": "custom:views/event/record/detail", "edit": "custom:views/event/record/edit" } }
                  2. Shall I create the detail.js file here under client/custom/src/views/event/record/detail.js with the following code?

                  Espo.define('custom:views/event/detail', 'crm:views/event/detail', function (Dep) {

                  return Dep.extend({
                  setup: function () {
                  Dep.prototype.setup.call(this);
                  this.listenTo(this.model, 'after:relate', function () {
                  this.model.fetch();
                  }, this);

                  this.listenTo(this.model, 'after:unrelate', function () {
                  this.model.fetch();
                  }, this);

                  });
                  });

                  I tried to do the above and got 404 with the Entity "event" then

                  Can you please continue to help me out on this? Thanks.

                  Comment

                  • item
                    Active Community Member
                    • Mar 2017
                    • 1476

                    #10
                    Hello,
                    1) you must just add views:{ "detail": ....}, no other !
                    2) Espo.define('custom:views/event/detail', 'crm:views/detail', function (Dep) { . As this is not a outofbox entity, you need base class herit.
                    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

                    Comment

                    • chrisjiang
                      Member
                      • Dec 2018
                      • 88

                      #11
                      Originally posted by item
                      Hello,
                      1) you must just add views:{ "detail": ....}, no other !
                      2) Espo.define('custom:views/event/detail', 'crm:views/detail', function (Dep) { . As this is not a outofbox entity, you need base class herit.
                      This time my custom/Espo/Custom/Resources/metadata/clientDefs/event.json is like this:

                      and client/custom/src/views/event/record/detail.js is like the other photo.

                      But still Bad luck Anything I did wrong?

                      Comment

                      • item
                        Active Community Member
                        • Mar 2017
                        • 1476

                        #12
                        Hello,
                        yes ..i see many typo error ..
                        attach the two file .. and i correct..

                        you can too use : online json validator : https://jsonformatter.curiousconcept.com/

                        as i have say. .it's not my skill..certainly not the perfect solution but work

                        regards
                        If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

                        Comment

                        • chrisjiang
                          Member
                          • Dec 2018
                          • 88

                          #13
                          Originally posted by item
                          Hello,
                          yes ..i see many typo error ..
                          attach the two file .. and i correct..

                          you can too use : online json validator : https://jsonformatter.curiousconcept.com/

                          as i have say. .it's not my skill..certainly not the perfect solution but work

                          regards
                          I corrected the typo error but found that that is not the key. I find that I am not able to manage this. -

                          Now I have no choice but give up.....

                          Anyway thanks very very much for your kind and patient help.

                          Wish you a happy 2020.

                          Comment

                          • chrisjiang
                            Member
                            • Dec 2018
                            • 88

                            #14
                            Originally posted by item
                            Hello,
                            add this on client/custom/src/view/account/detail.js

                            PHP Code:
                            Espo.define('custom:views/account/detail', 'crm:views/account/detail', function (Dep) {
                            
                            return Dep.extend({
                            setup: function () {
                            Dep.prototype.setup.call(this);
                            this.listenTo(this.model, 'after:relate', function () {
                            this.model.fetch();
                            }, this);
                            
                            this.listenTo(this.model, 'after:unrelate', function () {
                            this.model.fetch();
                            }, this);
                            
                            });
                            }); 
                            
                            Hi,

                            I really like to make this function work so I tested your code on a existing entity "campaign" with exactly the following:

                            add the following code on client/custom/src/view/campaign/detail.js :

                            Espo.define('custom:views/campaign/detail', 'crm:views/campaign/detail', function (Dep) {

                            return Dep.extend({
                            setup: function () {
                            Dep.prototype.setup.call(this);
                            this.listenTo(this.model, 'after:relate', function () {
                            this.model.fetch();
                            }, this);

                            this.listenTo(this.model, 'after:unrelate', function () {
                            this.model.fetch();
                            }, this);

                            });
                            });

                            But it still does not work? Did you test the code on your system and is there anything else I did wrong?

                            Thanks again.

                            Comment

                            • chrisjiang
                              Member
                              • Dec 2018
                              • 88

                              #15
                              Originally posted by item
                              Hello,
                              nice info "like targetList" so i have look and result

                              create a service for your entity : here is for Account .. and Contact
                              On Account : where i realte a contact -> i just need make a refresh and the count is +1
                              On Contact : where i relate a Account -> when i go .. the count is +1
                              And unrelate work to .. no need to modifie these two Enitty.

                              Just one "not my skill" => "auto refresh/render" .. so no need to "refresh" manually the browser

                              Regards

                              /custom/Espo/Custom/Services/Account.php
                              PHP Code:
                              <?php
                              namespace Espo\Custom\Services;
                              
                              use \Espo\ORM\Entity as Entity;
                              
                              class Account extends \Espo\Modules\Crm\Services\Account
                              {
                              
                              public function loadAdditionalFields(Entity $entity)
                              {
                              parent::loadAdditionalFields($entity);
                              $this->loadEntryCountField($entity);
                              }
                              
                              public function loadAdditionalFieldsForList(Entity $entity)
                              {
                              parent::loadAdditionalFields($entity);
                              $this->loadEntryCountField($entity);
                              }
                              
                              protected function loadEntryCountField(Entity $entity)
                              {
                              $count = 0;
                              $count += $this->getEntityManager()->getRepository('Account')->countRelated($entity, 'contacts');
                              $entity->set('countRelatedContact', $count);
                              }
                              }

                              Hi, possible to apply this method to the Stream function? For example: I want to add a field "StreamCount" to my Contact entity and want to see how many "Notes(Stream)" are attached to a record.

                              I tried the code but was told "Relationship not found between Contact and Note....".

                              As you helped me so much I would like to bother you again for help. Thanks very much in advance.

                              Comment

                              Working...