Relating Emails record with attachment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robin
    Junior Member
    • Aug 2025
    • 2

    #1

    Relating Emails record with attachment

    Hi together,

    I am currently trying to set/relate Email entities with attachments via formula script.
    Looking into the email entity I can see, that an Email has a parent-child relationship to attachments as well as an attachment-multiple field called "attachments".

    I've tried to relate the two entities with record\relate and I tried to set the value of attachmentsIds manually (see the two code snippets below)
    PHP Code:
    attachmentsIds = list("68d14663370926c9d"
    PHP Code:
    record\relate("Email"id"attachments", list("68d14663370926c9d"); 
    If I set the attachmentsIds manually in the FormulaSandbox and output the attachmentsIds straight after, they get correctly displayed. However they do not seem to be persisted because if I execute the script again but without setting the attachmentsIds they are gone.

    Background is, that I am importing Emails from an legacy system via the CSV-Import tool and I want to relate the attachments that I previously uploaded into the system with the imported Emails.

    If someone has any pointers how I can set the value of that attachment-multiple field of the Email entity that would be great

  • lazovic
    Super Moderator
    • Jan 2022
    • 1168

    #2
    Hi robin,

    This example works fine for me in the Formula Sandbox:
    Code:
    record\relate('Email', id, 'attachments', '68cac35dbdb9c6861');
    output\printLine(attachmentsIds);
    Output:

    ["68cac35dbdb9c6861"]

    Comment

    • shalmaxb
      Senior Member
      • Mar 2015
      • 1757

      #3
      I had the exact same problem: https://forum.espocrm.com/forum/gene...ord-by-formula
      It is a bit complicated, I hope you can follow my explanation. The decisive reason is in the last post/comment.

      Comment

      • robin
        Junior Member
        • Aug 2025
        • 2

        #4
        I have managed to solve the problem now by creating a seperate N:N relationship between Emails and documents and relate the Emails and attachments through them via the normal record/relate function.
        Thanks you for your help

        Comment

        Working...