Announcement

Collapse
No announcement yet.

Custom Hook Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom Hook Help

    When I convert a lead to an opportunity I want to populate the Opportunity 'name' field automatically with salutionName + firstName + lastName from the lead fields, I'm fumbling around trying to cobble together some code for a custom hook but all totally new to me so if anyone can help I'd appreciate it, is this code along the right lines? It doesn't seem to work

    PHP Code:
    namespace Espo\Custom\Hooks\Lead;

    use 
    Espo\ORM\Entity;

    class 
    Lead_To_Opportunity_Hook extends \Espo\Core\Hooks\Base

    public function beforeSave(Entity $entity, array $options=array())
    {

        
    $entityManager $this->getEntityManager();
        
    $sisterId $entity->get('opportunityId');
        
    $sisterType 'Opportunity';

        
    $sisterObject $entityManager->getRepository($sisterType)->where(['id'=>$sisterId])->findOne();
        
    // Pass Lead salutation, first name and last name to Opportunity name field
        
    $opportunityName $entity->get('salutationName').' '.$entity->get('firstName').' '.$entity->get('lastName');
        
    $sisterObject->set('name',$opportunityName);

        
    $entityManager->saveEntity($sisterObject);


  • #2
    I think a better approach is to create a custom Controller for lead and override the postActionConvert method.
    Original controller :application\Espo\Modules\Crm\Controllers\Lead.php
    take a look at https://docs.espocrm.com/development...ing-controller

    Comment


    • #3
      1. If you whant set opportunity field hook shoud be on opportunity
      2. In opportunity original lead accessible from originalLead (you can see it in entity manager -> link manager

      Try this code (/custom/Espo/Custom/Hooks/Opportunity/setName.php):

      PHP Code:
      <?php

      namespace Espo\Custom\Hooks\Opportunity;

      use 
      Espo\ORM\Entity;

      class 
      setName extends \Espo\Core\Hooks\Base
      {
      public static 
      $order 10;

      public function 
      afterSave(Entity $entity, array $options = array())
      {
      $lead $this->getEntityManager()->getEntity('Lead'$entity->get('originalLeadId'));
      if (
      $lead) {
      $name $lead->get('salutationName') . ' ' $lead->get('firstName') . ' ' $lead->get('lastName');
      $entity->set('name'$name);
      }
      }
      }
      Last edited by dimyy; 08-08-2021, 10:14 AM.

      Comment

      Working...
      X