Lead modification date when adding comments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • livewire
    Senior Member
    • Nov 2016
    • 100

    Lead modification date when adding comments

    Hi

    Is it possible to change the lead modification date when a comment/stream has been added?

    Coz i have many leads in my system so we regularly update the leads with comments but the problem we have is even though we add a comment to the lead, the modification date does not change unless we edit and change the lead details itself. so we are unable to see which lead was updated last correctly.

    Any help will be appreciated.

    Thanks
  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    It's possible only if you create a hook for Note entity.

    Comment

    • robbyslaughter
      Junior Member
      • Dec 2017
      • 5

      #3
      Would it be possible to explain a little further what you mean by this? It seems like you're saying:

      1. Create a file, custom/Espo/Custom/Hooks/{EntityName}/{HookName}.php;
      a. I presume EntityName is Lead and HookName would be something like UpdateModifcationDate
      b. Or, is the EntityName Stream?
      2. Something goes in that file that causes the associated Lead to be saved. Questions:
      a. Should we use AfterSave or AfterRelate?
      b. How do you connect to the associated Lead/Stream?
      c. If It is 1b above, then how is this *only* for Stream updates that are connected to Leads?

      Thank you!

      Comment

      • tanya
        Senior Member
        • Jun 2014
        • 4308

        #4
        1. EntityName is Note
        2.a. afterSave
        2.b. something like this
        if ($entity->get('parentType') == "Lead" && $entity->get('parentId')) {
        $lead = $this->getEntityManager()->getEntity("Lead", $entity->get('parentId'));
        if ($lead) {
        $lead->set('FIELD_NAME', new \DateTime('NOW', new \DateTimeZone('UTC')));
        $this->getEntityManager()->saveEntity($lead);
        }
        }

        Comment

        • robbyslaughter
          Junior Member
          • Dec 2017
          • 5

          #5
          Thanks, I got it to work. For reference for anybody else who finds this thread, here are the steps

          1. Create an Hooks directory and a Note subdirectory under custom/Espo/Custom.

          This is because Streams are actually Notes. We are going to tie this hook to whenever someone saves a Note.

          2. Create a file in this subdirectory called number.php.

          I chose number.php because this is a field in the note table in the database that gets saved. I believe the filename has to correspond to a field that is saved.

          3. Make it contain the following data. Note that I put parts which are unique to our installation in italics.

          PHP Code:
          <?php
          
          namespace Espo\Custom\Hooks\Note;
          
          use Espo\ORM\Entity;
          
          class number extends \Espo\Core\Hooks\Base
          {
            public function afterSave(Entity $entity, array $options = array())
              {
                 if ($entity->get('parentType') == "Opportunity" && $entity->get('parentId')) {
                    $opportunity = $this->getEntityManager()->getEntity("Opportunity", $entity->get('parentId'));
                    if ($opportunity) {
                       $opportunity->set('FIELD_NAME', new \DateTime('NOW', new \DateTimeZone('UTC')));
                       $this->getEntityManager()->saveEntity($opportunity);
                    }
                 }
              }
          }
          
          ?>
          You'll notice the class is called number, which corresponds to the filename. I wanted to tie this to an Opportunity, not a lead, so I added that in as the parent type to check against. Also I named the variable $opportunity for clarity. When you're done safe this file.

          4. Go to your Administration menu and click Clear Cache.

          5. Try adding a note to the stream of an existing opportunity. You'll have to refresh to see it on the screen, but the data is changed immediately.

          Comment

          • livewire
            Senior Member
            • Nov 2016
            • 100

            #6
            i tried it with lead (changed the variables to match the lead)... but it does not change the modifiedAt date on the lead.. remains the same.. any idea why?

            Comment

            • livewire
              Senior Member
              • Nov 2016
              • 100

              #7
              Got it working... I renamed the folder wrong Thanks for the help

              Comment

              • pixility
                Junior Member
                • May 2019
                • 2

                #8
                I want to realize the same for the account entity.

                I modified the code above and changed opportunity to „account“ but i doesn't work for me.

                PHP Code:
                
                <?php
                
                namespace Espo\Custom\Hooks\Note;
                
                use Espo\ORM\Entity;
                
                class number extends \Espo\Core\Hooks\Base
                {
                  public function afterSave(Entity $entity, array $options = array())
                    {
                       if ($entity->get('parentType') == "Account" && $entity->get('parentId')) {
                          $account = $this->getEntityManager()->getEntity("Account", $entity->get('parentId'));
                          if ($account) {
                             $account->set('FIELD_NAME', new \DateTime('NOW', new \DateTimeZone('UTC')));
                             $this->getEntityManager()->saveEntity($account);
                          }
                       }
                    }
                }
                
                ?>
                When i try it, i get a error 500 when i want to make a comment on an account.

                I also changed the permissions of the file and folder to 775.

                Any idea what i did wrong?

                Is there a way to do thid for all entitys?

                Comment

                Working...