Adding file attachments to a record in Microsoft Dynamics 365 Finance and Operations (D365FO) is a straightforward process from the user's perspective. The user only clicks the paperclip icon 📎, selects a file for the attachment, and the attachment is added to the record.
When a developer starts making customizations related to D365FO Attachments, you might think it’s logical and straightforward process too. But when you dig in, trying to add or select the attachments programmatically, you find two CompanyId fields in the DocuRef table. Weird, right? 😕 You are immediately confused as we were. You start to Google and struggle to find more details about which of these two fields you should use to store or get attachments correctly.
Well, this article will save you a lot of time as we will describe the difference between the ActualCompanyId and RefCompanyId fields and how D365FO stores reference to the legal entity.
The CompanyId related fields in the DocuRef table
If you look at the DocuRef table, you will notice that there are two fields whose names contain CompanyId: RefCompanyId and ActualCompanyId.
You may first wonder if these fields hold the same information and why. 🤔 In reality, the CompanyId reference in these two fields depends on the table type you are adding the attachments to. In some cases, both contain the same values, but there are scenarios where they are different.
Scenario #1: ActualCompanyId == RefCompanyId
If the table you are adding an attachment to is a company-specific table (the Save Data Per Company property is set to Yes), these two fields hold the same information. They are both set to the current CompanyId. An example of this type of table is the PurchTable table.
To show the example we add an attachment to one of the purchase orders.
After adding the attachment to the Purchase order record, we can open the DocuRef table using Table Browser and check the newly created record in the DocuRef table. Since the PurchTable is a company-specific table, the DocuRef table record holds the same data in the ActualCompanyId and RefCompanyId fields.
Scenario #2: ActualCompanyId != RefCompanyId
When you add an attachment to a record of a company-neutral table (the Save Data Per Company property is set to No), then the ActualCompanyId and RefCompanyId fields do not hold the same data. The RefCompanyId is set to DAT, while the ActualCompanyId field is set to the current company you are working on.
We can see this scenario by using the Products menu item in the Product information management workspace (using the EcoResProduct table in the background), which contains items that are not yet released to any of the legal entities.
After we add a new product image to the product from the USMF legal entity in the background, we create a reference to the EcoResProduct table. However, because the product was not released to the USMF legal entity, the product image can be viewed only through the selected product's Related information and under the record attachments as shown on the images below.
If we check now the entry added to the DocuRef table for the newly added product image through the table browser, we can see that the ActualCompanyId (USMF) and RefCompanyId (DAT) are different.
Grasping the dynamic duo roles of ActualCompanyId and RefCompanyId in the DocuRef table is essential for developers. 🛠️ In company-specific tables, these fields match, while for company-neutral tables, they diverge. By understanding the difference you can easily create a customization to add or select attachments in the right way in your D365FO environment.