Announcement

Collapse
No announcement yet.

NamingConvention on Documents/DocumentFolder => Hook - afterSave + related-entities

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

  • NamingConvention on Documents/DocumentFolder => Hook - afterSave + related-entities

    Hi there

    i have a custom entity called "BusinessOpportunity" with an attribute called "internalName"

    Every time that a new "BusinessOpportunity" is created, i'd like to prepare an expected DocumentFolder hierarchy to put their all the document related to this entity BusinessOpportunity.

    I have created a hook... with a beforeSave function to force the internalName in uppercase.

    PHP Code:
    class Base extends \Espo\Core\Hooks\Base
    {
      
    // beforeSave | afterSave | beforeRemove | afterRemove
      // if ($entity->isNew())
      
    public function beforeSave(Entity $entity)
      {
          
    $entity->set('nomInterne'strtoupper($entity->get('nomInterne')));

      }

    now... i need to create the documentFolder structure, afterSave, like that:

    Top-level Document Folder = $BusinessOpportunity->internalName
    Then, sub-folders (subfolder1, subfolder2)

    Ex:
    For a BusinessOpportunity row with internalName = "Example 1",
    I want to create a DocumentFolder hierarchy to put there my future documents:

    Example 1
    ... -> subfolder1
    .... -> subfolder2

    Any clues about how to do my afterSave webhook to create this related-entities rows ?
    I check an example between Cases and KnowledgeBaseArticles (cf http://forum.espocrm.com/forum/devel...elated-entites)
    but i have some issues to make what i want.

    Thanks for your help

  • #2
    Hello

    EspoCRM Documentation. Contribute to espocrm/documentation development by creating an account on GitHub.

    something like
    PHP Code:
    ////in afterSave method
    $topFolder $this->getEntityManager()->getEntity('DocumentFolder');
    $topFolder->set('name'$entity->get('nomInterne'));
    $this->getEntityManager()->saveEntity($topFolder);

    $subFolder1 $this->getEntityManager()->getEntity('DocumentFolder');
    $subFolder1->set('name''subFolder 1');
    $subFolder1->set('parentId'$topFolder->id);
    $this->getEntityManager()->saveEntity($subFolder1);
    /// 

    Comment


    • #3
      Thanks a lot !
      I manage to get it worked in the meantime with these following code

      PHP Code:
      foreach ($folders as $folder_name) {
              
      $folder $this->getEntityManager()->getEntity('DocumentFolder'); 
              
      $folder->set('name'$folder_name);
              
      $folder->set('parentId'$folder_top_level_id);
              
      $folder->set('parentName'$name); 
              
      $this->getEntityManager()->saveEntity($folder);
            } 
      I check the api call through my browser, network inspect tab.
      and I saw that the attribute parentName was also set (but not in your code example).

      Is it mandatory ? should i expect any troubles if i dont set it ?

      Comment


      • #4
        If you change target entity and it will be shown after process ending, is better to set link name fields (as parentName). But, if your modified entity is not target (not BusinessOpportunity in your case, but DocumentFolder), and it will be read later, you can ignore setting of link name fields. They will be loaded, when entity be read

        Comment


        • #5
          In my custom entity called "BusinessOpportunity", is there a way to display a custom field (like a dashlet) to display all the documentFolder + Documents closed to my BusinessOpportunity ?

          Thanks

          Comment


          • #6
            Nothing impossible Set the view of your field in entotyDefs, define the template and data you need to pass.

            Comment


            • #7
              Ok thanks going to try to dig into the code and other examples to check how i could do that!

              Comment

              Working...
              X