Relate document automatically to parent record by formula

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shalmaxb
    Senior Member
    • Mar 2015
    • 1705

    #1

    Relate document automatically to parent record by formula

    Hi, I have an entity, from which I create a PDF document, send it by mail and at the same time put a copy of that PDF to a pre-defined folder. This all works as desired. How can I link this document to the parent record from whoích the PDF had been created, so that I can display the created PDF(s) in the relationship panel of the parent record.
    I have e relation established between the document folder and th default relationship n:n between documents and parent entity. Should I create another 1:n relationship between documents and entity, or waht solution would work.
  • victor
    Active Community Member
    • Aug 2022
    • 952

    #2
    Originally posted by shalmaxb
    I have an entity, from which I create a PDF document, send it by mail and at the same time put a copy of that PDF to a pre-defined folder. This all works as desired
    How exactly did you achieve this? Can you show in detail?

    Comment

    • shalmaxb
      Senior Member
      • Mar 2015
      • 1705

      #3
      It is quite a complex formula, I hope you can get it, how I achieved it. The two very last (commented) lines are those to link the document, what does not work. So far I have only the defaut n:n relationship between the custom entity "Participants = CTeilnehmerKlassen" and the documents.

      Here the formula:

      Code:
      // ------------------------------ Print a contract as PDF, send it by Mail to the participant and save in a document folder ---------------------------------
      //create name and filename of the PDF
      bezeichnungPDFVertrag =
      string\concatenate('Teilnahmevereinbarung',' ',teilnehmerAngNachname,' ',teilnehmerAngVorname,' ',jahr,' ',instrument);
      //Dateiname PDF Vertrag erzeugen
      dateinamePDFVertrag = string\concatenate('Teilnahmevereinbarung','_',teilnehmerAngNachname,'_',teilnehmerAngVorname,'_',ja hr,'_',instrument,'.pdf');
      //Read ID of the document folder, which I create triggere by another entity and link by relationship to the current entity
      dokumentenordnerID = documentFolderTeilnehmerKlasseId;
      //create PDF and put into document folder
      //Here I use a enum field with the value "Print Contract (Vertrag drucken)" to create the PDF with data from the current entity and mark a hidden Bool as true
      ifThen(vertragDrucken == 'Vertrag drucken' && vertragGedrucktUndGesendet == false,
      $pdfId = ext\pdf\generate(
      'CTeilnehmerKlassen',
      entity\attribute('id'),
      '66affa9d1f48b34d1',
      dateinamePDFVertrag
      );
      //creates the document and puts it to the pre-defined folder
      record\create(
      'Document',
      'type', 'Teilnahmevereinbarung',
      'folderId', dokumentenordnerID,//ID des Dokumentenordners Verträge
      'name', bezeichnungPDFVertrag,
      'fileId', $pdfId,
      'publishDate', datetime\today()
      ));
      
      //sending the contract by E-Mail an attach it
      $attachmentId = $pdfId;
      $zipId = '676c7e8448d8329e6';
      ifThen(
      vertragDrucken == 'Vertrag drucken' && vertragGedrucktUndGesendet == false, //Der Button wurde betätigt
      $emailId = record\create(
      'Email', //Es wird eine Email angelegt
      'from', 'info@workshop-jazz.de', //Absendeadresse, System-Email
      'to', teilnehmerAnmeldungen.emailAddress, //Empfänger aus der Relation übernehmen
      //'status', 'Draft',
      'attachmentsIds', list($attachmentId, $zipId), //das erzeugte PDF wird zu den vorhandenen Anhängen gesetzt
      'parentId', entity\attribute('id'), //Die ID des Datensatzes auswählen
      'parentType', 'CTeilnehmerKlassen' //Die Entität des Datensatzes auswählen
      ));
      
      //Use pre-defined Email-Template to send and after send set the Bool to false, which changes the enum to the value "Contract sent (Vertrag gesendet)"
      ifThen(
      vertragDrucken == 'Vertrag drucken' && vertragGedrucktUndGesendet == false, //Der Button wurde betätigt
      ext\email\applyTemplate($emailId, '6683ddaeb2ac74393'); //Die ID des vorbereiteten Templates
      ext\email\send($emailId); //Die oben erzeugte Email ID verwenden
      vertragGedrucktUndGesendet = true; //Checkbox Vertrag gesendet wird markiert
      );
      
      // after send set the Bool to false, which changes the enum to empty value, to not display it anymore, a message will be displayed, that the contract had already be sent (WYSIWYG field)
      ifThen(
      vertragGedrucktUndGesendet == true,
      vertragDrucken = ' '
      //This should be the formula to link the document record to the participant entity, but it doesn`t work
      //$ids = record\findRelatedMany('CTeilnehmerKlassen', id, 'documents', 10);
      //record\relate('Document', id, 'cTeilnehmerKlassens', $ids);
      );
      Last edited by shalmaxb; 07-03-2025, 01:30 PM.

      Comment

      • shalmaxb
        Senior Member
        • Mar 2015
        • 1705

        #4
        I apologize for pushing, but this question of mine is not yet answered. Is something like that at all possible with formula?

        Comment

        • lazovic
          Super Moderator
          • Jan 2022
          • 1109

          #5
          Hi shalmaxb,

          Can you please try using this formula script?
          Code:
          bezeichnungPDFVertrag = string\concatenate('Teilnahmevereinbarung', ' ', teilnehmerAngNachname, ' ', teilnehmerAngVorname, ' ', jahr, ' ', instrument);
          dateinamePDFVertrag = string\concatenate('Teilnahmevereinbarung', '_', teilnehmerAngNachname, '_', teilnehmerAngVorname, '_', jahr, '_', instrument, '.pdf');
          
          dokumentenordnerID = documentFolderTeilnehmerKlasseId;
          
          ifThen(vertragDrucken == 'Vertrag drucken' && !vertragGedrucktUndGesendet,
          
              $pdfId = ext\pdf\generate(
                  'CTeilnehmerKlassen',
                  entity\attribute('id'),
                  '66affa9d1f48b34d1',
                  dateinamePDFVertrag
              );
          
              $documentId = record\create(
                  'Document',
                  'type', 'Teilnahmevereinbarung',
                  'folderId', dokumentenordnerID,
                  'name', bezeichnungPDFVertrag,
                  'fileId', $pdfId,
                  'publishDate', datetime\today()
              );
          
              record\relate(
                  'CTeilnehmerKlassen',
                  id,
                  'documents',
                  $documentId
              );
          
              $attachmentId = $pdfId;
              $zipId = '676c7e8448d8329e6'; 
          
              $emailId = record\create(
                  'Email',
                  'from', 'info@workshop-jazz.de',
                  'to', teilnehmerAnmeldungen.emailAddress,
                  'attachmentsIds', list($attachmentId, $zipId),
                  'parentId', id,
                  'parentType', 'CTeilnehmerKlassen'
              );
          
              ext\email\applyTemplate($emailId, '6683ddaeb2ac74393');
              ext\email\send($emailId);
          
              vertragGedrucktUndGesendet = true;
          );
          
          ifThen(
              vertragGedrucktUndGesendet,
              vertragDrucken = ' '
          );

          Comment

          • shalmaxb
            Senior Member
            • Mar 2015
            • 1705

            #6
            Thank you very much, lazovic you brought me on the path. The only thing I had to change was the $documentId, as the calculation is too fast and thus the Id is not yet registered in the database and stays empty..
            I created a custom textfield like lastDocumentId, into which I fetched the $documentId by another line in the record\create formula:

            Code:
            $documentId = record\create(
            'Document',
            'type', 'Teilnahmevereinbarung',
            'folderId', dokumentenordnerID,//ID des Dokumentenordners Verträge
            'name', bezeichnungPDFVertrag,
            'fileId', $pdfId,
            'publishDate', datetime\today());
            letzteDocumentId = $documentId
            );
            Then I referred to that lastDocumentId in the relate formula, which I had to put outside of the ifThen declaration. The link is then achieved by recalculate.

            Code:
            record\relate(
            'CTeilnehmerKlassen',
            id,
            'documents',
            letzteDocumentId);​
            After the contract is created, sent, put into the documents and linked to the participant, I set another formula to reset the execution of all this and nulled the lastDocumentId, so with a new contract the execution will be correct.

            Comment


            • shalmaxb
              shalmaxb commented
              Editing a comment
              The issue with the documentId is likely due to the relationship. An Id is created, when the document is created and then the id is saved in the n:n relation and becomes one of many Id`s. If that happened, the formula cannot fetch the one Id anymore.
          Working...