Assuming for a custom entity I want to only allow to select/add contacts (hasMany link) for an specific account the user selected in another link field (account). Would it possible to filter the link by a subquery using values from another field?
Announcement
Collapse
No announcement yet.
filter link field values by other entity link field
Collapse
X
-
This would be a compromise. The contact control in Case is not filtered in the auto complete but at least when you klick on the select button the simple search is already filtered by the selected account. I will check how this is implemented.
The users would expect that the auto complete would be filtered the same way.
Comment
-
Now I tried to create a custom view for my custom entity Contract as:
./client/custom/src/views/contract/fields/contact.js
Code:Espo.define('views/contract/fields/contact', 'views/fields/link', function (Dep) { return Dep.extend({ getSelectFilters: function () { if (this.model.get('accountId')) { return { 'account': { type: 'equals', field: 'accountId', value: this.model.get('accountId'), valueName: this.model.get('accountName'), } }; } }, getCreateAttributes: function () { if (this.model.get('accountId')) { return { accountId: this.model.get('accountId'), accountName: this.model.get('accountName') } } } }); });
Code:"contact": { "type": "link", "view": "views/contract/fields/contact" },
Thanks!
- Likes 1
Comment
-
Thanks, that was the tirck. I already tried /moduel/view but without success. At the same time I found the right syntax in https://github.com/espocrm/documenta...ustom-views.md. Shame on me ...
Comment
-
OK I think I got it.
a) Create a Class of the AutoComplete to be Filtered in custom/Espo/Custom/SelectManagers/[YourEntityToBeFiltered].php
b) created a function like protected function filterAssignable(&$result) (see filterActive in application/Espo/SelectManagers/User.php as an example) Mine looked like this:
protected function filterAssignable(&$result)
{
$result['whereClause'][] = array(
'assign_status' => 'assignable',
);
}
c) in the custom field view definition like above add the function (in my case 'assignable'):
getSelectPrimaryFilterName: function () {
return 'assignable';
}
So now with Hi-Ko's filtering of the dialog and this filtering of the autocomplete it's harder for users to pick the wrong thing...
Hope that helps someone else.
- Likes 4
Comment
-
Originally posted by krasnopv View Post
How can I create this for a custom Entities?
Assume that your custom entity is "ServiceContract" linked as Many-To-One to "Account" as as Many-To-Many to Contact.
custom/EspoCustom/Resources/metadata/entityDefs/ServiceContract.json
Code:"fields": { "contacts": { "type": "linkMultiple", "view": "custom:views/service-contract/fields/contacts", "orderBy": "name" }, }
Code:Espo.define('custom:views/service-contract/fields/contacts', 'views/fields/link-multiple-with-primary', function (Dep) { return Dep.extend({ primaryLink: 'contact', getSelectFilters: function () { if (this.model.get('accountId')) { return { 'account': { type: 'equals', attribute: 'accountId', value: this.model.get('accountId'), data: { type: 'is', nameValue: this.model.get('accountName') } } }; } }, getCreateAttributes: function () { if (this.model.get('accountId')) { return { accountId: this.model.get('accountId'), accountName: this.model.get('accountName') } } } }); });
- Likes 1
Comment
-
Hey
i have tried this but it didn't work.
I have 3 custom enitities : Client - Project - Product.
I have a project (custom entity) with relationship below :
- Client One-to-Many Project
- Client Many-to-Many Product
- Project Many-to-Many Product
On the project create page, i want to chose a client and then when i chose products i want to get only products that belong to the selected client. I follow your steep but when i click on create project it keep only loading, below is my set up :
custom/EspoCustom/Resources/metadata/entityDefs/Project.json
Code:"fields": { "products": { "type": "linkMultiple", "layoutDetailDisabled": false, "layoutMassUpdateDisabled": false, "view": "custom:views/project/fields/products", "importDisabled": false, "noLoad": false, "isCustom": true } }
client/custom/src/views/project/fields/products.js
Code:Espo.define('custom:views/project/fields/products', 'views/fields/link-multiple', function (Dep) { return Dep.extend({ getSelectFilters: function () { if (this.model.get('clientId')) { return { 'client': { type: 'equals', attribute: 'clientId', value: this.model.get('clientId'), data: { type: 'is', nameValue: this.model.get('clientName') } } }; } }, getCreateAttributes: function () { if (this.model.get('clientId')) { return { clientId: this.model.get('clientId'), clientName: this.model.get('clientName') } } }, }); });
-
Usually "forever loading" indicates a javascript error. Use your browser tools to check the console output and see what error(s) is being thrown out so you can troubleshoot your scripts.
If you don't know, to bring the console in Chrome or Edge (the 2 browsers that I use) right click your mouse, select "inspect" and then select the tab "console". Try to add a new project again and see what error is shown in the log.
Comment