Announcement

Collapse
No announcement yet.

Notification after Lead assignment

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

  • Notification after Lead assignment

    Hello guys,

    I would like to turn on app notifications after lead assignment, but I am not able to select Lead in Administration->Notifications->Entities to notify about upon assignment

    Is there any way how to add the lead to the list?

    Thank you
    Click image for larger version

Name:	image.png
Views:	374
Size:	22.2 KB
ID:	98406

  • #2
    I don't think there is a way to do it through UI but i have found that you can implement (assignmentNotificatorClassName) to enable notification for certain entity, here is what you can do:

    1 - Create a file under custom\Espo\Custom\Classes\AssignmentNotificators\ Lead.php paste the code below:

    PHP Code:
    <?php

    namespace Espo\Custom\Classes\AssignmentNotificators;

    use 
    Espo\Core\Field\LinkParent;
    use 
    Espo\Core\Notification\AssignmentNotificator;
    use 
    Espo\Core\Notification\AssignmentNotificator\Params;
    use 
    Espo\Core\Notification\DefaultAssignmentNotificator;
    use 
    Espo\Core\Notification\UserEnabledChecker;
    use 
    Espo\Core\Utils\Metadata;
    use 
    Espo\Entities\Notification;
    use 
    Espo\Entities\User;
    use 
    Espo\Modules\Crm\Entities\Lead as LeadEntity;
    use 
    Espo\ORM\Entity;
    use 
    Espo\ORM\EntityManager;

    /**
     * @implements AssignmentNotificator<LeadEntity>
     */
    class Lead implements AssignmentNotificator
    {
        private const 
    ATTR_ASSIGNED_USERS_IDS 'assignedUsersIds';
        private const 
    TYPE_ASSIGN 'Assign';

        public function 
    __construct(
            private 
    DefaultAssignmentNotificator $defaultAssignmentNotificator,
            private 
    UserEnabledChecker $userEnabledChecker,
            private 
    EntityManager $entityManager,
            private 
    User $user,
            private 
    Metadata $metadata
        
    ) {}

        
    /**
         * @param LeadEntity $entity
         */
        
    public function process(Entity $entityParams $params): void
        
    {
            
    // Default assignment notifications not needed if stream is enabled.
            
    if (!$this->hasStream($entity->getEntityType())) {
                
    $this->defaultAssignmentNotificator->process($entity$params);
            }

            if (
    $entity->getStatus() !== LeadEntity::STATUS_NEW) {
                return;
            }

            if (
    self::ATTR_ASSIGNED_USERS_IDS &&
                !
    $entity->isAttributeChanged(self::ATTR_ASSIGNED_USERS_IDS)
            ) {
                return;
            }

            
    /** @var string[] $prevIds */
            
    $prevIds $entity->getFetched(self::ATTR_ASSIGNED_USERS_IDS) ?? [];
            
    $ids $entity->get('assignedUsersIds');

            
    $newIds array_filter($ids, fn ($id) => !in_array($id$prevIds));

            
    $assignedUser $entity->getAssignedUser();

            if (
    $assignedUser) {
                
    $newIds array_filter($newIds, fn($id) => $id !== $assignedUser->getId());
            }

            
    $newIds array_values($newIds);

            foreach (
    $newIds as $id) {
                
    $this->processForUser($entity$id);
            }
        }

        
    /**
         * @param LeadEntity $entity
         */
        
    private function processForUser(Entity $entitystring $userId): void
        
    {
            if (!
    $this->userEnabledChecker->checkAssignment($entity->getEntityType(), $userId)) {
                return;
            }

            
    $createdBy $entity->getCreatedBy();
            
    $modifiedBy $entity->getModifiedBy();

            
    $isSelfAssignment $entity->isNew() ?
                
    $createdBy && $userId ===  $createdBy->getId() :
                
    $modifiedBy && $userId === $modifiedBy->getId();

            if (
    $isSelfAssignment) {
                return;
            }

            
    /** @var Notification $notification */
            
    $notification $this->entityManager->getRDBRepositoryByClass(Notification::class)->getNew();

            
    $notification
                
    ->setType(self::TYPE_ASSIGN)
                ->
    setUserId($userId)
                ->
    setRelated(
                    
    LinkParent::create($entity->getEntityType(), $entity->getId())
                )
                ->
    setData((object) [
                    
    'entityType' => $entity->getEntityType(),
                    
    'entityId' => $entity->getId(),
                    
    'entityName' => $entity->getName(),
                    
    'isNew' => $entity->isNew(),
                    
    'userId' => $this->user->getId(),
                    
    'userName' => $this->user->getName(),
                ]);

            
    $this->entityManager->saveEntity($notification);
        }

        private function 
    hasStream(string $entityType): bool
        
    {
            return (bool) 
    $this->metadata->get(['scopes'$entityType'stream']);
        }
    }

    2 - Create a file under this Path: metadata > notificationDefs > Lead.json. paste the code below

    PHP Code:
    {
        
    "assignmentNotificatorClassName""Espo\\Custom\\Classes\\AssignmentNotificators\\Lead",
        
    "forceAssignmentNotificator"true
    }​ 

    The code above checks if there are multiple assignedusers (just in case if you use that in the future) then it will be send notification to new assigned users Or new assigned user if only use a single default assigned user.

    I have tested it but i think it should work, please note that it doesn't work for current user, so to check if it works you need to use two sessions two users.

    I hope this helps
    Rabii
    Web Dev

    Comment


    • maxyang
      maxyang commented
      Editing a comment
      Can the above code trigger email notifications after assigning a customer (account)?

    • rabii
      rabii commented
      Editing a comment
      No that is for in app notification. if you want to use email then you need to implement Espo\Core\Notification\EmailNotificationHandler interface. Check out how it is done for CaseObj i guess there is something.

    • maxyang
      maxyang commented
      Editing a comment
      Anyway, thank you. Even though I still haven't figured it out.

  • #3
    Hm, I also can not add Lead to the in-App Notification.

    But I have two custom entities and one I can add and one I can not. Not sure why its like that or what causes on entity to be added and the other not. Both were created via the UI.

    Comment


    • #4
      It it possible to add any entity to the Entities to notify about upon assignment list if Stream is turned off for this entity.

      Comment


      • #5
        yes the reason why is that in order to have the all entities there the entity should have stream enabled and should have multiple assignedusers
        check this code which fill in the field with list of entitytypes https://github.com/espocrm/espocrm/b...ty-list.js#L41
        Rabii
        Web Dev

        Comment


        • #6
          OK i figure out how it works, there should be 3 conditions in order for your entity to be listed on the assignment notification list, in your entity scopes you should have the 3 conditions as below:

          PHP Code:
          {
              
          "entity"true,
              
          "stream"false,
              
          "notifications"true
          }​ 
          It should be entity true which means it is not used as an object by the system, it should not have stream on because if stream is on then that information will be logged into the stream and it should have notification on. if you add this to any entity it will be available on the assignment notification entity list.

          SO ALL YOU NEED TO DO IS ADD THIS TO YOUR ENTITY SCOPE FILE
          Rabii
          Web Dev

          Comment


          • #7
            Remember if you have advanced pack you can use workflow to notify assignment easily without the need for any code changes.
            Rabii
            Web Dev

            Comment


            • #8
              This could be an improvment for the docs. Stating in order to have in-app notifications, the entity needs to have streams disabled. Have not seen such note or I missed it.

              Comment


              • lazovic
                lazovic commented
                Editing a comment
                If a Stream is enabled for an entity and you create a record with this entity type with another assigned user, that user will receive a notification that something has been assigned to them. No additional settings are needed. Please try to check it in your instance.

              • Jakub Grufik
                Jakub Grufik commented
                Editing a comment
                hello lazovic, I have stream turned on for the Leads and still cannot choose Lead in Administration->Notifications... or this behavior is automatic without need to set it up in Administration?

              • ThomasB
                ThomasB commented
                Editing a comment
                You need to disable streams, if you want to add notification in the admin interface.

                See the code:
                {
                "entity": true,
                "stream": false,
                "notifications": true
                }​

                If I understood lazovic correctly, if stream is turned on, it basically acts like in-app notifcations are turned on in the admin interface.
                Because users will reveive notifications, there is no need to add them in that admin option.
                Last edited by ThomasB; 10-10-2023, 12:34 PM.

            • #9
              Hi Jakub Grufik,

              If Stream is turned on, you don’t need to do or configure anything else.
              Check this on your instance. Log in as another user in the anonymous tab and from the main tab (for example, under the admin) create/edit a lead by adding the assigned user from the anonymous tab to it. This user will receive a notification.

              Comment


              • #10
                My main message is the following: if Stream is enabled for an entity, there is absolutely no need to add this entity to the Entities to notify about upon assignment list. The notification about the assignment will already be sent to the assigned user.

                Comment


                • Jakub Grufik
                  Jakub Grufik commented
                  Editing a comment
                  got it, thanks a lot for this usefull info have a nice day mate
              Working...
              X