Wiki source code of Foreign Field linked to another entity field
Last modified by Douglas Bower on 2026/05/12 09:26
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | Use case: | ||
| 4 | |||
| 5 | If you have established a relationship between the primary entitiy, and a related entity and need to display additional attributes from the Related Entity onto the details form of the Primary Entity, you can create a Foreign Field on the Primary entity and link it to whatever fields you need on the Related Entity. The values of the linked Related Entity will be available to add to any of the Primary Entity Layouts. | ||
| 6 | |||
| 7 | Example: | ||
| 8 | |||
| 9 | I have two entities: Company and Subsidiary. A company can have multiple Subsidiaries, but each subsidary only has one Company. So there is a Many Subsidiaries to One Company relationship. On the Subsidiary detail Layout, I would like to show the Company Name, Address, and Website so the user does not have to navigate to the Company record. To make this work: | ||
| 10 | |||
| 11 | 1. Create a relation field on the Subsidiary named "Company Name" and pick the Many subsidiary To One company link and the "Company Name" field in the Field selection of the create field UI. | ||
| 12 | 1. Create a relation field on the Subsidiary named "Company Address" and pick the Many subsidiary To One company link and the "Company Address" field in the Field selection of the create field UI. | ||
| 13 | 1. Create a relation field on the Subsidiary named "Company Website" and pick the Many subsidiary To One company link and the "Company Website" field in the Field selection of the create field UI. | ||
| 14 | |||
| 15 | You now have three new fields that can be added to the Subsidiary Detail Layout. They are controlled by a single relationship between the subsidiary and the company. When you change the link, all of the field values will change. | ||
| 16 | |||
| 17 | |||
| 18 | **Foreign fields** in EspoCRM allow you to display and search by fields from a related entity without creating a direct link field for the value itself. To use them, you define a field with the type foreign in your entity definition, specifying the link (the relationship to the parent entity) and the field (the specific attribute from the related entity to display). | ||
| 19 | |||
| 20 | When creating the foreign field defintion you need to select a "Link" that is an existing Many-To-One relationship for the Entity that you are creating a foreign key. Not sure if One-to_one is acceptable. | ||
| 21 | |||
| 22 | |||
| 23 | Key configuration steps include: | ||
| 24 | |||
| 25 | * ((( | ||
| 26 | **Basic Definition**: Set the field type to foreign, link it to an existing relationship (e.g., account), and specify the target field (e.g., industry). You can also set readOnly: true if the value should only be displayed. | ||
| 27 | |||
| 28 | {{{"fields": { "accountIndustry": { "type": "foreign", "link": "account", "field": "industry", "readOnly": true } }}}} | ||
| 29 | |||
| 30 | |||
| 31 | ))) | ||
| 32 | * **Visibility and Creation**: By default, foreign fields might be hidden from creation. To allow users to create records via the UI, you may need to set "notCreatable": false in the field's metadata or ensure "skip": false is set in the entity definition. | ||
| 33 | * **Dynamic Updates**: To automatically update foreign field values when the linked entity changes, you can use a custom view for the link field or a **Before Save Formula** to copy values. For example, a formula like assignedUserFromAccount = account.assignedUserName can populate a custom varchar field based on the linked account's user. | ||
| 34 | * **Search and Layouts**: Foreign fields can be included in list views and used for search filters (e.g., finding contacts based on their company's industry). They do not need to be displayed in the main layout if they are only needed for queries. | ||
| 35 | |||
| 36 | For complex scenarios, such as displaying multiple foreign fields from a single link, developers often create custom JavaScript views to listen for link changes and update the dependent foreign field views accordingly. | ||
| 37 | |||
| 38 | |||
| 39 | **User Question:** | ||
| 40 | |||
| 41 | We have entity 'Accounts' and it has foreign field 'Assigned User' from entity 'Users' (client and assigned manager). | ||
| 42 | Also we have entity 'Debit Cards', which has foreign field 'Assigned User' too, but additionally it has foreign field 'Account' (debit card, manager who responsible for card, client - card's owner). | ||
| 43 | How can i display in 'Debit card' information from 'Accounts'->'Assigned User' entity? | ||
| 44 | |||
| 45 | |||
| 46 | You can do as follows: | ||
| 47 | - In //Administration > Entity Manager > Debit Card > Fields// create a field **assignedUserFromAccount** with type //Varchar.// | ||
| 48 | - In //Administration > Entity Manager > Debit Card > Formula > Before Save Custom Script// add a simple formula: | ||
| 49 | |||
| 50 | Code: | ||
| 51 | |||
| 52 | {{{assignedUserFromAccount = account.assignedUserName;}}} | ||
| 53 | |||
| 54 | Thus, the field **assignedUserFromAccount** will display the //name// of Assigned User from the account with which this Debit Card is related. | ||
| 55 | |||
| 56 | Ok, i created new field and formula. Now to get values into that field, i need to update all Debit Card records. Is it possible to do it in simple way? | ||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | 1) | ||
| 61 | [[image:https://forum.espocrm.com/filedata/fetch?id=101595&d=1705340595||alt="Click image for larger version | ||
| 62 | |||
| 63 | Name: image.png | ||
| 64 | Views: 273 | ||
| 65 | Size: 11.9 KB | ||
| 66 | ID: 101595"]] | ||
| 67 | 2) | ||
| 68 | [[image:https://forum.espocrm.com/filedata/fetch?id=101596&d=1705340700||alt="Click image for larger version | ||
| 69 | |||
| 70 | Name: image.png | ||
| 71 | Views: 258 | ||
| 72 | Size: 9.7 KB | ||
| 73 | ID: 101596"]] | ||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | Yes, I found that method. It is ok when you have <1000 records, but if there is >10000 ? In my situation i had 1700, so it took some time to load all of them on one screen, mark and recalculate. But i have another similar task from CRM managers to create additional calculated field for entity with 100000 records... | ||
| 78 | |||
| 79 | Look closely at the second screenshot. On this screenshot, you need to click on the small triangle to select absolutely all records of your entity by 1 click | ||
| 80 | |||
| 81 | Beg my pardon, you mean 'Select All Result' selects complete all records (with not displayed on page) ? | ||
| 82 | |||
| 83 | That's right: absolutely all records (both those displayed on the page and those not). | ||
| 84 | |||
| 85 | |||
| 86 |