Announcement

Collapse
No announcement yet.

Relationships link between Contacts

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

  • Relationships link between Contacts

    Please help solve an important problem using the Workflow or other formula.
    Condition:
    The client first registers on the third-party Terminal service and is assigned terminalAccountID (int) there.
    Then this value is passed to ESPOCRM in the terminalAccountID (integer) field of the "Contact", with all other data for creating a new record.

    The agent has an entry in "Contact" and is a user of the portal in ESPOCRM.
    If a new "Contact" came via a referral link to a third-party Terminal from a "Contact" agent, then the terminalAccountID of this agent is transmitted in the terminalAgentAccountID field of the new "Contact" in ESPOCRM.

    I made for the entity "Contact" Relationships with "Contact"
    contactParentIB One-to-Many (field for agent)
    and
    contactsIB Many-to-One (field for attracted clients)

    Questions:
    1. Using what formula to establish a relationship link between Contacts if the condition is met?

    ifThen(
    terminalAgentAccountID != null && terminalAgentAccountID == contact.terminalAccountID,
    entity\addLinkMultipleId('contactParentIB', 'contactId')
    );

    2. How to make the Agent see this new Contact in the portal?



  • #2
    Hello,
    It is not clear.

    Is the Agent a Portal user or it is a Contact?
    You really created the relationship between Contact and Contact or Contact and Portal User?

    By default the Portal User has a connection to one Contact that is due to a system logic allows him to view this Contact record.
    I didn't test such logic previously so I can't tell whether it will work if you make additional relation to link the Portal User with a lot of Contacts.

    Comment


    • #3
      The Agent is a Contact that has a Portal User.
      I created a relationship between a Contact and a Contact, but I can do it according to a different logic if it works.

      "By default the Portal User has a connection to one Contact that is due to a system logic allows him to view this Contact record." - I understand it
      The Agent should have access to data only through the Portal, and there he should see other Contacts associated with his Contact.

      Comment


      • #4
        That make sense.

        1. Your formula won't work. You need to find the desired Parent Conatct id before you will be able to link new Contact to the Parent Contact. You can achieve it with this formula https://docs.espocrm.com/administrat...#recordfindone.
        Note that for this formula you need to set the filter (check links in the end of the record\findOne() formula description). I believe in your case you will need to add a filter with one of those ways.

        2. To let the Portal user view all related to the Parent Contact records, you need to set permission in the Portal Role for the Contact entity -> Read 'Contact'.
        Last edited by Maximus; 10-08-2020, 01:38 PM.

        Comment


        • #5
          I wrote this code and it works, but it is not completely correct.


          $id = record\findOne('Contact', 'terminalAgentAccountID', 'desc', 'reportFilter5f7f6865ae0352ea7');
          ifThen(
          terminalAgentAccountID != null,
          entity\addLinkMultipleId('contactParentIB', $id)
          );

          Result


          Report


          Report Result


          But there was another task, how to make the correct selection in the Report and compare terminalAgentAccountID with the terminalAccountID of another Contact. Now it works like this, with this filter CRM selects the first Contact in which both fields are not equal to Null, but this is not correct filtering.

          How do I set comparison conditions?
          Last edited by sobolevfff; 10-08-2020, 07:55 PM.

          Comment


          • #6
            Probably it is much better to write Hook for your purpose (code skills required)?
            Please investigate this manual https://docs.espocrm.com/development/hooks/

            Comment


            • #7
              To be honest, I have very little experience in coding. But I read the instructions and something else and that's what came of it. I cannot verify this code, so please show me my mistakes.

              Do I need to create such a file in a directory?
              custom/Espo/Custom/Hooks/Contact/ContactAgent.php

              <?php
              namespace Espo\Custom\Hooks\Contact;

              use Espo\ORM\Entity;

              class ContactAgent extends \Espo\Core\Hooks\Base
              {
              public function beforeSave(Entity $entity, array $options = array())
              {
              if($entity->isNew() && empty($option['ignoreHook'])) {

              $terminalAgentAccountID = $this->getEntityManager()->getEntity('terminalAgentAccountID');

              if($terminalAgentAccountID != null) {
              $agent = $entityManager
              ->getRepository('Contact')
              ->where(['terminalAccountID' == $terminalAgentAccountID,])
              ->find();

              $entityManager
              ->getRepository('Contact')
              ->getRelation($agent, 'contactParentIB')
              ->relateById($contactId);
              $entityManager->saveEntity($agent, ['ignoreHook' => true]);
              }

              }
              }
              }
              Last edited by sobolevfff; 10-09-2020, 04:59 PM.

              Comment


              • #8
                Gives an error message
                Espo.ERROR: (500) ORM: Repository 'terminalAgentAccountID' does not exist.; POST /crm/api/v1/Contact; line: 165, file: application/Espo/ORM/EntityManager.php [] []


                Last edited by sobolevfff; 10-12-2020, 06:09 PM.

                Comment


                • #9
                  Please, help. Reworked the code, but now another error.




                  if($entity->isNew() && empty($option['ignoreHook'])) {
                  $terminalAgentAccountID = $entity->get('terminalAgentAccountID');
                  if (!empty($terminalAgentAccountID)) {
                  $agentId = $entityManager->getRepository('Contact')
                  ->where([
                  'terminalAccountID' => $terminalAgentAccountID,
                  ])
                  ->find();
                  return $agentId;
                  $entityManager->getRepository('contact')->relate($entity, 'contactsParentIB', $agentId);
                  $entityManager->saveEntity($entity, ['ignoreHook' => true]);



                  ERROR: Неперехваченная ошибка исключения: «Вызов функции-члена getRepository () при нулевом значении» в custom / Espo / Custom / Hooks / Contact / ContactAgent.php, строка 20 {"исключение": "[объект] (Ошибка (код: 0):

                  Here line 20: $agentId = $entityManager-&gt;getRepository('Contact')

                  Comment


                  • #10
                    Hi,
                    This works for me:
                    PHP Code:
                    <?php
                    namespace Espo\Custom\Hooks\Contact;

                    use 
                    Espo\ORM\Entity;

                    class 
                    ContactAgent extends \Espo\Core\Hooks\Base
                    {
                        public function 
                    beforeSave(Entity $entity, array $options = array())
                        {

                            
                    $entityManager $this->getEntityManager();

                            
                    $terminalAccountID $entity->get('terminalAccountID');

                            if(
                    $entity->isNew() && $terminalAccountID) {

                                
                    $contactParentid $entityManager->getRepository('Contact')->where(['terminalAgentAccountID=' => $terminalAccountID])->findOne();

                                
                    $entityManager->getRepository('Contact')->relate($entity'contactParent'$contactParentid);

                            }
                        }
                    }
                    'contactParent' is the key value of the related Parent Contact which is Agent (see screenshot).
                    Attached Files

                    Comment


                    • #11
                      Thank you so much, the issue is almost resolved!!
                      1. Doesn't work if a new Contact is added. To start processing, I need to massively update the list. I removed "$entity->isNew() " condition because the code doesn't work with it.

                      2.I fixed target entity, it worked but vice versa.
                      $entityManager->getRepository('Contact')->relate($entity, 'contacts', $contactParentid);

                      In this form, the code works after a massive update of records in the table. I don't understand why the code doesn't work when adding a new Contact. Rather, it worked while there were few records in the table. But as soon as one Contact has a lot of links to other Contacts, it will stop working with new records. It is desirable that the code works when adding a new Contact, and after changes to existing Contacts.
                      i tried changing findOne() to find() but didn't help.





                      <?php
                      namespace Espo\Custom\Hooks\Contact;

                      use Espo\ORM\Entity;

                      class ContactAgent extends \Espo\Core\Hooks\Base
                      {
                      public function beforeSave(Entity $entity, array $options = array())
                      {

                      $entityManager = $this->getEntityManager();

                      $terminalAccountID = $entity->get('terminalAccountID');

                      if($terminalAccountID) {

                      $contactParentid = $entityManager->getRepository('Contact')->where(['terminalAgentAccountID=' => $terminalAccountID])->findOne();

                      $entityManager->getRepository('Contact')->relate($entity, 'contacts', $contactParentid);

                      }
                      }
                      }



                      And this massive update doesn't normally work. After creating an entity, you need to make changes to it, and then make changes to all the entities so that the link appears. I noticed that the code only works with a mass update of the Team. I have to update all records many times in different ways to establish a relationship. How can I set the task to auto-update everything in the code so as not to do it manually?

                      It is not always possible to run the code. It seems that he works whenever he wants and with whatever records he wants. The more new entries I add, the less the code works.
                      http://prntscr.com/uyqa7j
                      Last edited by sobolevfff; 10-13-2020, 08:15 PM.

                      Comment


                      • #12
                        <?php
                        namespace Espo\Custom\Hooks\Contact;

                        use Espo\ORM\Entity;

                        class ContactAgent extends \Espo\Core\Hooks\Base
                        {
                        public function beforeSave(Entity $entity, array $options = array())
                        {

                        $entityManager = $this->getEntityManager();

                        $terminalAgentAccountID = $entity->get('terminalAgentAccountID');

                        if($entity->isNew() && $terminalAgentAccountID) {

                        $contactParentid = $entityManager->getRepository('Contact')->where(['terminalAccountID=' => $terminalAgentAccountID])->findOne();

                        $entityManager->getRepository('Contact')->relate($entity, 'contactParent', $contactParentid);

                        }
                        }
                        }



                        I corrected your code a bit and it worked !! Thanks a lot!
                        Last edited by sobolevfff; 10-13-2020, 09:21 PM.

                        Comment

                        Working...
                        X