CaseObj and Email

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • concept
    Junior Member
    • Sep 2023
    • 17

    CaseObj and Email

    Hello, I am extending the class CaseObj to make sure that upon receiving an email the subject of the email is taken and, through an explode, you go to populate the fields of the Case.
    My code is:​

    PHP Code:
    namespace Espo\Modules\MyModule\Repositories;
    
    class CaseObj extends \Espo\Modules\Crm\Repositories\CaseObj
    {
        protected function beforeSave(Entity $entity, array $options = array())
        {
          parent::beforeSave($entity, $options);
        
    
            try {            
                    if ($entity->get('inboundEmailId'))
                    {
                        $GLOBALS['log']->error('------------ENTITY GET ID---------------------beforeSave  '.$entity->get('id'));
                        
                        $email = $this->getEntityManager()
                            ->getRepository('Email')
                            ->select(['id'])
                            ->where(array(
                                'emailId'=> $email,
                                'parentType=' => 'Case',
                                'parentId=' => $entity->get('id')
                        ))->findOne();
                        $GLOBALS['log']->error('------------EMAIL ID---------------------beforeSave  '.$email->get('id'));
                        
                        if($email){
                            $subjectEmail = $email->get('name');
                            $GLOBALS['log']->error('------------EMAIL SUBJECT---------------------beforeSave  '.$subjectEmail);
                            $array=explode("-", $subjectEmail);
                            $area=$array[0];
                            $areaD=$array[1];
                            $entity->set('area', $area);
                            $entity->set('areaD', $areaD);
      
                            
                            $GLOBALS['log']->error('----------------------AREA------------------------------------------- $area = ' . $area);
                            $GLOBALS['log']->error('----------------------AREA D----------------------------------------' . $areaD);
                            $GLOBALS['log']->error('----------------------CASE AREA----------------------------------' . $entity->get('area'));
        
                        }
                    }
            }catch(\Exception $error){
                $GLOBALS['log']->error('------------ERROR---------------------beforeSave '.$error);
    }
    }} 
    
    Log:
    PHP Code:
    ERROR: Repositories CaseObj ------------CASE accountProjectId ---------------------beforeSave accountProjectI= [] []
    ERROR: Repositories CaseObj ------------CASE accountProjectId --------------------- [] []
    ERROR: ------------ENTITY GET ID---------------------beforeSave 652667200066255d7 [] []
    ERROR: InboundEmail 65046ca6562def3bb, after-fetch hook; 0 Call to a member function get() on null [] [] 
    


    I think the problem is that the CaseObj class has no relationship with the Email entity, I just don’t know how to connect them.
    Can someone help me?
    Thank you​
    ​​
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #2
    Hi,
    i see one confusion rapidly :

    PHP Code:
    
    $email = $this->getEntityManager()
                            ->getRepository('Email')
                            ->select(['id'])
                            ->where(array(
                                'emailId'=> $email,  //// <---- change to $email->getId()
                                'parentType=' => 'Case',
                                'parentId=' => $entity->get('id')  //// <---- no need =
                        ))->findOne();
    Last edited by item; 10-11-2023, 10:26 AM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    • concept
      Junior Member
      • Sep 2023
      • 17

      #3
      item I continue to receive the same logs

      Comment


      • rabii
        rabii commented
        Editing a comment
        i think it is better to use a hook instead of repository.
    • item
      Active Community Member
      • Mar 2017
      • 1476

      #4
      Hi,
      as Rabii say, it's better follow his advice.

      i don't use case or email so i can just say so :
      and beforeSave, id is not filled (!?) if entity is new..to investigate


      PHP Code:
      
      namespace Espo\Modules\MyModule\Repositories;
      
      class CaseObj extends \Espo\Modules\Crm\Repositories\CaseObj
      {
          protected function beforeSave(Entity $entity, array $options = array())
          {
            parent::beforeSave($entity, $options);
          
      
              try {            
                      if ($entity->get('inboundEmailId'))
                      {
                          $GLOBALS['log']->error('------------ENTITY GET ID---------------------beforeSave  '.$entity->get('id'));
                          
                          $email = $this->getEntityManager()
                              ->getRepository('Email')
                              ->select(['id']) //// no need
                              ->where(array(
                                  'emailId'=> $email, //// <--- certainly $entity->get('inboundEmailId')
                                  'parentType=' => 'Case',  //// remove =
                                  'parentId=' => $entity->get('id') /// remove = and $entity->getId()
                          ))->findOne();
                          $GLOBALS['log']->error('------------EMAIL ID---------------------beforeSave  '.$email->get('id'));
                          
                          if($email){
                              $subjectEmail = $email->get('name');
                              $GLOBALS['log']->error('------------EMAIL SUBJECT---------------------beforeSave  '.$subjectEmail);
                              $array=explode("-", $subjectEmail);
                              $area=$array[0];
                              $areaD=$array[1];
                              $entity->set('area', $area);
                              $entity->set('areaD', $areaD);
        
                              
                              $GLOBALS['log']->error('----------------------AREA------------------------------------------- $area = ' . $area);
                              $GLOBALS['log']->error('----------------------AREA D----------------------------------------' . $areaD);
                              $GLOBALS['log']->error('----------------------CASE AREA----------------------------------' . $entity->get('area'));
          
                          }
                      }
              }catch(\Exception $error){
                  $GLOBALS['log']->error('------------ERROR---------------------beforeSave '.$error);
      }
      }}  
      If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #5
        AS MENTIONED BEFORE YOU CAN USE A HOOK INSTEAD, HERE IS A CODE BELOW THAT SHOULD WORK FOR YOUR USE CASE, Create a folder under custom\Espo\Custom\Hooks\Case and add a php file EmailSubject.php and paste the code below:

        PHP Code:
        <?php
        namespace Espo\Custom\Hooks\Case;
        
        use Espo\Core\Hook\Hook\BeforeSave;
        use Espo\Core\Exceptions\NotFound;
        use Espo\Core\ORM\EntityManager;
        use Espo\Entities\Email as EmailEntity;
        
        use Espo\ORM\Entity;
        use Espo\ORM\Repository\Option\SaveOptions;
        
        /**
         * @implements BeforeSave<User>
         */
        class EmailSubject implements BeforeSave
        {    
            // Adapt this based on how many hooks you have in your system for case entity
            public static int $order = 3;
        
            public function __construct(
                private EntityManager $entityManager
            ) {}
        
            public function beforeSave(Entity $entity, SaveOptions $options): void
            {
                $inboundEmailId = $entity->get('inboundEmailId');
        
                if (
                    // If you want to apply this hook only to new created cases otherwise the hook will go in loop
                    $entity->isNew() &&
                    !$inboundEmailId
                ) {
                    return;
                }
        
                /** @var ?EmailEntity $email */
                $email = $this->entityManager->getEntityById(EmailEntity::ENTITY_TYPE, $inboundEmailId);
        
                if (!$email) {
                    throw new NotFound();
                }
        
                $subjectEmail = explode("-", $email->getSubject());
        
                $entity->set([
                    'area', $subjectEmail[0],
                    'areaD', $subjectEmail[1]
                ]);
            }
        }

        I hope this helps
        Rabii
        Web Dev

        Comment

        Working...