Announcement

Collapse
No announcement yet.

Filter on selection of related record

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Filter on selection of related record

    Hi!
    I'm trying to filter records, which are displayed, when selecting related record. This is not a filter, like usual filter on front-end with checkbox, but filter which is used in addition to all other filters, when making select query to DB. I tried to use complex expression in "where" propery of entityDefs with no luck. Then I tried to read more docs, but I got even more confused. Can someone guide me how to resolve this issue?
    This is my complex expression (primaryTeamId is varchar field of current entity)

    PHP Code:
    "assignedUser": {
        
    "required"false,
        
    "readOnly"false,
        
    "where": {
            
    "IN": {
                
    "whereClause": {
                    
    "id=s": {
                        
    "from""TeamUser",
                        
    "select": ["userId"],
                            
    "whereClause": {
                                
    "deleted"false,
                                
    "teamId""{primaryTeamId}"
                            
    }
                    }
                }
           }
        }
    },
    ​ 

  • #2
    Maybe use a selectHandler. https://forum.espocrm.com/forum/deve...current-record. It can add frontend filters whenever you a select a record for a relation.

    If you want to completely handle it in the backend, you need to write some custom code.

    Comment


    • #3
      I would better write custom code in PHP. Where to find a reference?

      Comment


      • #4
        It's not easy to tell. First you need to pass some parameter from the frontend to let the backend to know to apply your filter.

        You can use a custom primary filter.

        Create a primary filter that won't be listed on the standard list view, but applied when you select records. selectPrimaryFilterName: https://docs.espocrm.com/development...tionshippanels

        Define an implementation class: https://docs.espocrm.com/development...erclassnamemap

        Then you will need just to apply a needed filter to a query builder.

        Comment


        • a.slyzhko
          a.slyzhko commented
          Editing a comment
          I see that I have to implement Espo\Core\Select\Primary\Filter interface, which has function apply(QueryBuilder $queryBuilder): void
          But how to pass a value of team id from the task entity to this method?

      • #5
        If you need to completely forbid the ability to select not allowed assigned user, you will need to add a backend validation for the field.

        Comment


        • #6
          If you want to pass some value, I recommend to resort to selectHandler that will return (in promise) a filter "linkedWith".

          Example: https://github.com/espocrm/espocrm/b...ccount-many.js

          See the topic I posted above for more detail.

          Comment


          • #7
            Thanks, I implemented this using selectHandler. But user can clear team filter. How to forbid this?

            Comment


            • #8
              Having a validation for the field is the easiest solution. Other solutions would require investigating and would take time.

              Comment


              • a.slyzhko
                a.slyzhko commented
                Editing a comment
                This validation is applied on entity save/update as I understand? I need a tool, which will select only valid related records before they are passed to the front-end. And to perform this filtering, I need entity id, to which I select related record, or a value of primaryTeamId of this entity.

            • #9
              It would require me spending some time to find a solution. I have other work to do. If it's a for link field, there's no problem to have a validation that just won't allow to save a record a wrong user.
              Last edited by yuri; 10-31-2023, 05:36 PM.

              Comment

              Working...
              X