beforeSave calling a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scott_Smith
    Junior Member
    • Oct 2017
    • 27

    beforeSave calling a function

    I have a large section of code in beforeSave that needs to be called several times and I would like to make it a function.

    I have tried very simple functions(as below) but seem unable to call a function at all???

    ex:

    <?php
    namespace Espo\Custom\Hooks\Task;
    use Espo\ORM\Entity;
    class Task extends \Espo\Core\Hooks\Base
    {
    public function beforeSave(Entity $entity, array $options = array())
    {
    abc();
    }
    public function abc()
    {
    d$='test';
    }

    }

    Many thanks!
    Scott
  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    Hi

    You are in class. Here are methods, not functions. to call abc you need to write $this->abc(); (BTW, better make this method protected or private)

    Comment

    • Scott_Smith
      Junior Member
      • Oct 2017
      • 27

      #3
      Yes of course!... thanks for the prompt reply!

      Comment

      • Scott_Smith
        Junior Member
        • Oct 2017
        • 27

        #4
        Hmm... still not working... such a simple method??? I really don't get it... throws off error 500

        <?php
        namespace Espo\Custom\Hooks\Task;
        use Espo\ORM\Entity;
        class Task extends \Espo\Core\Hooks\Base
        {
        public function beforeSave(Entity $entity, array $options = array())
        {
        $this->abc();
        }
        protected function abc()
        {
        d$='test';
        }

        }

        Comment

        • Scott_Smith
          Junior Member
          • Oct 2017
          • 27

          #5
          Got it... stupid typo with the d$ instead of $d... rolling now.... thought I would put this up to share...

          I created an Entity called Processes which will have an Assignee and Instructions in RichText
          I then added a special type of Task/modified the layout called 'New Hire' and beforeSave the following code spawns multiple tasks so they appear in the assignees respective calendars and of course in the Tasks List

          Best regards
          Scott



          <?php
          namespace Espo\Custom\Hooks\Task;
          use Espo\ORM\Entity;
          class Task extends \Espo\Core\Hooks\Base
          {
          public function beforeSave(Entity $entity, array $options = array())
          {
          if($entity->isNew() && $entity->get('type')=="New Hire" && empty($options['ignoreHook'])) {
          $entityManager = $this->getEntityManager();
          $nh_name = ucwords($entity->get('newhirename'));
          $entity->set('name', 'New Hire - '.$nh_name);

          $this->nh_create($nh_name,'Create Contract',$entity, $entityManager); //Task 1 Create Contract
          $this->nh_create($nh_name,'Clearcare',$entity, $entityManager); //Task 2 Clearcare
          $this->nh_create($nh_name,'General Admin',$entity, $entityManager); //Task 3 General Admin
          $this->nh_create($nh_name,'Mentor',$entity, $entityManager); //Task 4 Mentor
          }
          }

          protected function nh_create($nh_name,$task_name, $entity, $entityManager){


          /* ***********************/
          /* * Get Process Master */
          /* ***********************/
          $proc = 'New Hire-'.$task_name;
          $proc_entity = $entityManager->getRepository('Processes')->where(array('name' => $proc,))->findOne();

          /* *******************************/
          /* * Create new records for tasks*/
          /* *******************************/
          $nh_entity = $entityManager->getEntity('Task');
          $d=array('teamsIds' => '591c1b62397f237f4');
          $nh_entity->set('teamsIds',$d);
          $nh_entity->set('teamsNames','Operations');

          /* *****************/
          /* * Update record */
          /* *****************/
          $au_id = $proc_entity->get('assignedUserId');
          $nh_entity->set('assignedUserId', $au_id);
          $nh_entity->set('type', 'New Hire');
          $nh_entity->set('newhirename', $nh_name);
          $nh_entity->set('name', $task_name.' - '.$nh_name);
          $nh_entity->set('dateStart', $entity->get('dateStart'));
          $nh_entity->set('dateEnd', $entity->get('dateEnd'));
          $nh_entity->set('status', 'Not Started');
          $nh_entity->set('priority', 'High');
          $nh_entity->set('instructions', $proc_entity->get('instructions'));

          /* ***************/
          /* * Save Task */
          /* ***************/
          $entityManager->saveEntity($nh_entity, ['ignoreHook' => true]);



          }



          }



          Comment

          • Scott_Smith
            Junior Member
            • Oct 2017
            • 27

            #6
            Couldn't have done it w/o you Tanya... thanks again!!!!

            Comment

            • Maryam123
              Junior Member
              • Apr 2020
              • 19

              #7
              tanya hope you are doing well i need lilitle help

              Espo.WARNING: E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument supplied for foreach()","file":"C:\\xampp\\htdocs\\espocrm\\app lication\\Espo\\Core\\HookManager.php","line":161, "context":{"hookDirs":["custom/Espo/Custom/Hooks"],"hookData":[],"hookDir":"custom/Espo/Custom/Hooks","fileList":["Contact.php"],"hookFiles":"Contact.php","scopeName":0,"hookS cop eDirPath":"custom\\Espo\\Custom\\Hooks","normalize dScopeName":"Case","scopeHooks":[]}} [] [2020-08-14 19:50:29] Espo.WARNING: E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument supplied for foreach()","file":"C:\\xampp\\htdocs\\espocrm\\app lication\\Espo\\Core\\HookManager.php","line":161, "context":{"hookDirs":["custom/Espo/Custom/Hooks"],"hookData":[],"hookDir":"custom/Espo/Custom/Hooks","fileList":["Contact.php"],"hookFiles":"Contact.php","scopeName":0,"hookS cop eDirPath":"custom\\Espo\\Custom\\Hooks","normalize dScopeName":"Case","scopeHooks":[]}} []


              i m getting this issue



              my code is below

              public function beforeSave(Entity $entity, array $options = array()) { if ($entity->isNew()) { $entityManager = $this->getEntityManager(); $nh_name = ucwords($entity->get('name')); $this->abc($nh_name,$entity, $entityManager); //Task 1 Create Contract } } public function abc($name,$entity,$entityManager) { $actions = "Create New User"; $action = "https://clinic.nexthealth.pk/ClinicConnectService/SignUp/SignUpPatient"; $firstName=$entity->get('firstName'); $vLastName=$entity->get('lastName'); $vCountry='PK'; $vEmailAddress=$entity->get('emailAddress') ; $password= 'softech@123'; $postArray = array("vFirstName" => $firstName, "vLastName" => $vLastName, "vCountry" => $vCountry, "vEmailAddress" => $vEmailAddress, 'password' => $password); $encryptObj = $this->encrypt_aes256(json_encode($postArray)); $ch = curl_init($action); curl_setopt($ch, CURLOPT_POST, 1); $queryString = "action=$action&encryptObj=$encryptObj"; curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($ch); curl_close($ch); $jsonObj = json_decode($result); if ($jsonObj->meta->code == "-1000") { $error_messages = $jsonObj->meta->message; } else if ($jsonObj->meta->code == "504") { $error_message = $jsonObj->meta->message; } else if ($jsonObj->meta->code == "200") { $nh_entity = $entityManager->getEntity('Contact'); $entityManager->saveEntity($nh_entity, ['ignoreHook' => true]); } }

              Comment

              • Maximus
                Senior Member
                • Nov 2018
                • 2731

                #8
                Hi Maryam123,
                Tanya is not working in the EspoCRM company any more. So you better to involve some other developers. I'd like to help you but I am not a developer. I have only one guess that probably might be helpful. I think that the error description says that you put your Hook into the wrong directory (your Hook is located in /custom/Espo/Custom/Hooks. But it should be in /custom/Espo/Custom/Hooks/Contact if the target entity is Contact: e.g. /custom/Espo/Custom/Hooks/Contact/Contact.php).
                Please read this https://docs.espocrm.com/development/hooks/#example.

                Comment

                Working...