Add note to stream of parent entity on create/update of child entity

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TimHarrison
    Junior Member
    • Dec 2019
    • 7

    Add note to stream of parent entity on create/update of child entity

    I have two custom entities, Employee and Review. Employee is linked 1:n to Review. Employee is of type Person and Review is of type Event. Upon creation or update of the Review entity, I'd like to add a note to the stream of the Employee entity.

    What class do I need to create to allow me to check the status or various metadata of the Review upon creation/change, find the parent entity, and add a note in the stream of that entity?

    I know how to find the parent entity, but the two pieces of code I'm not sure on is what class/function to create and the code to add the note to the stream of a parent element.

    If someone could point me in the right direction, I would be very grateful! Please let me know if I can clarify anything.



  • yuri
    Member
    • Mar 2014
    • 8440

    #2
    Hi Tim,


    You can utilize afterSave hook https://github.com/espocrm/documenta...pment/hooks.md.


    PHP Code:
       $note = $this->getEntityManager()->createEntity([
           'parentId' => $entity->get('employeeId'),
           'parentType' => 'Employee',
           'type' => 'Post',
       ], ['createdById' => 'system']); 
    
    Last edited by yuri; 02-26-2020, 07:21 AM.
    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

    • TimHarrison
      Junior Member
      • Dec 2019
      • 7

      #3
      Thank you yurikuzn! That worked with a little bit of tweaking to get it working right. Apparently, it needs a string with the entity type as the first option to createEntity(). Took some trial and error to figure out, but this is what ended up working:
      PHP Code:
      <?php
      
      namespace Espo\Custom\Hooks\Review;
      
      use Espo\ORM\Entity;
      
      class EmployeeStream extends \Espo\Core\Hooks\Base
      {
      public function afterSave(Entity $entity, array $options = []) {
          $note = $this->getEntityManager()->createEntity('Note', [
              'parentId' => $entity->toArray()['parentId'],
              'parentType' => 'Employee',
              'type' => 'Post',
              'post' => 'Review was updated.'
          ], ['createdById' => 'system']);
      }
      }




      Comment


      • esforim
        esforim commented
        Editing a comment
        Very nice, another "coder" is using EspoCRM. Welcome.
    • TimHarrison
      Junior Member
      • Dec 2019
      • 7

      #4
      Is there any documentation on how to modify the note based on difference update scenarios? For instance, if a single field changes like the status, name, start date, etc. on the review, I'd like to bubble that up on the Employee stream. It's a little unclear on how to reflect those types of updates in a note entity.

      Comment

      • telecastg
        Active Community Member
        • Jun 2018
        • 907

        #5
        You could create a custom hook for the "review" entity and incorporate that logic in the BeforeSave or AfterSave functions.

        For example, assume that you want to write a post into an employee stream when the review entity status field changes to "completed", and as you stated, the Employee and Review are linked in a One-To-Many relationship, meaning that the Review entity will have a property "employeeId"

        custom/Espo/Custom/Hooks/Review/ReviewHooks.php
        PHP Code:
        <?php
        
        namespace Espo\Custom\Hooks\Review;
        
        use Espo\ORM\Entity;
        
        class ReviewHooks extends \Espo\Core\Hooks\Base
        {
            public function afterSave(Entity $entity, array $options=array())            
                $entityManager = $this->getEntityManager();  
                // get the linked employee entity
                $employeeObject = $entityManager->getEntity('Employee', $entity->get('employeeId'));
                // if the Review status changed to "completed" write a note linked to the Employee
                if ($entity->isAttributeChanged('status') && $entity->get('status') == 'completed' {
                    $note = $entityManager->createEntity('Note', [
                        'parentId' => $employeeObject->get('id),
                        'parentType' => 'Employee',
                        'type' => 'Post',
                        'post' => 'Review was completed.'
                        ], ['createdById' => 'system']);
                    // persist the note entity
                    $entityManager->saveEntity($note);
                }
            {      
        }

        For more information about hooks check this link: https://www.espocrm.com/documentatio...lopment/hooks/

        If you need guidance on how to manipulate entities this can help https://www.espocrm.com/documentation/development/orm/
        Last edited by telecastg; 02-27-2020, 07:07 AM.

        Comment

        Working...