filter link field values by other entity link field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hi-ko
    Member
    • May 2015
    • 90

    filter link field values by other entity link field

    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?
  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    Do you want something like entity Case works? (after selecting account, you can select only contacts, related to this account)

    Comment

    • hi-ko
      Member
      • May 2015
      • 90

      #3
      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

      • hi-ko
        Member
        • May 2015
        • 90

        #4
        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')
                        }
                    }
                }
        
            });
        
        });
        and in Custom/Resources/metadata/entityDefs/Contract.json I set
        Code:
                "contact": {
                    "type": "link",
                    "view": "views/contract/fields/contact"
                },
        unfortunately the filter is not beeing used. How do I have to register a custom view? Maybe my path or definition is not correct?

        Thanks!

        Comment

        • tanya
          Senior Member
          • Jun 2014
          • 4308

          #5
          Right view is 'custom:views/contract/fields/contact'. Fix it in both of files

          Comment

          • hi-ko
            Member
            • May 2015
            • 90

            #6
            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

            • murray99
              Member
              • Jan 2017
              • 57

              #7
              I got the filter in the dialog to work, but did you find a solution for filtering the results in the autocomplete?
              I agree that not having the autocomplete filtered is a potential gotcha for the users.

              Comment

              • murray99
                Member
                • Jan 2017
                • 57

                #8
                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.


                Comment

                • krasnopv
                  Junior Member
                  • Jan 2019
                  • 3

                  #9
                  Originally posted by tanya
                  Do you want something like entity Case works? (after selecting account, you can select only contacts, related to this account)
                  Good morning, Tanya. How can I create this for a custom Entities?

                  Comment

                  • telecastg
                    Active Community Member
                    • Jun 2018
                    • 907

                    #10
                    Originally posted by krasnopv

                    How can I create this for a custom Entities?
                    This is how it can be implemented for a custom entity:

                    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"
                            },
                    
                    }
                    client/custom/src/views/service-contract/fields/contacts.js
                    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')
                                    }
                                }
                            }
                    
                        });
                    
                    });

                    Comment


                    • rabii
                      rabii commented
                      Editing a 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')
                                      }
                                  }
                              },
                          });
                      });
                      Any help please. iam using the latest version of the crm 5.9.3

                    • telecastg
                      telecastg commented
                      Editing a comment
                      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.
                  Working...