Announcement

Collapse
No announcement yet.

Filter a Link field by foreign variable

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

  • Filter a Link field by foreign variable

    I have a custom entity which has a Many-to-One relationship with the Account Entity. My Account Entity has a custom enum of "type" where I have different kinds of accounts I am keeping track of (i.e. Vendor, Customer, Agency)

    My custom entity has a link to the accounts where I'd like to select only accounts with the "type" of "Vendor". I've attached a screenshot of the select filter I'd like to have, but I'm not sure how to write it so that it works. I'm assuming it's something similar to what I'm working with already?


    I have created a custom view: **This does not work.** \client\custom\src\views\currentProduct\field\vend or.js

    Espo.define('custom:views/currentProduct/fields/vendor', 'views/fields/link', function (Dep) {

    return Dep.extend({

    getSelectFilters: function () {
    return {
    'account.type': {
    type: 'anyOf',
    value: 'Vendor',
    valueName: this.model.get('accountName'),
    }
    };
    }
    });

    });



    I've attached the custom view in custom/Espo/Custom/Resources/metadata/entityDefs/CurrentProduct.json

    {
    "fields": {
    "vendor": {
    "type": "link",
    "dynamicLogicVisible": null,
    "view": "custom:views/contract/fields/product",
    "required": false,
    "audited": false,
    "tooltip": false
    }
    },
    "links": {
    "vendor": {
    "type": "belongsTo",
    "foreign": "vendor",
    "entity": "Account",
    "audited": false,
    "isCustom": true
    }
    }
    }
    This is what filter i'd like to apply.
    Last edited by joy11; 08-11-2017, 01:57 PM.

  • #2
    Hello

    https://stackoverflow.com/questions/...20526#44820526
    https://github.com/espocrm/documenta...ustom-views.md


    needed path \client\custom\src\views\current-product\fields\vendor.js

    definition
    Code:
    Espo.define('custom:views/current-product/fields/vendor', 'views/fields/link', function (Dep) {
    in entityDefs field view has to be the same as in definition 'custom:views/current-product/fields/vendor'

    Comment


    • #3
      I'm sorry, I posted wrong entitydefs above.

      I have this in my custom/Espo/Custom/Resources/metadata/entityDefs/CurrentProducts.json :

      {
      "fields": {
      "vendor": {
      "type": "link",
      "dynamicLogicVisible": null,
      "view": "custom:views/currentProduct/fields/vendor",
      "required": false,
      "audited": false,
      "tooltip": false
      }
      },
      "links": {
      "vendor": {
      "type": "belongsTo",
      "foreign": "vendor",
      "entity": "Account",
      "audited": false,
      "isCustom": true
      }
      }
      }


      this does not work either. I think that my vendor.js isn't quite right? I'm not sure how to write it to only filter and display the accounts with type = "Vendor" (type is an enum added to accounts)

      Comment


      • #4
        using current-product instead of currentProduct is important. Check in browser log, if your view was read or you get 404 error

        Comment


        • #5
          You were right. It wasn't reading until I changed it to current-product. I am not getting any errors now and it's closer because something shows up as a filter. It still doesn't act quite like I thought it would.

          I've attached a screenshot of how the filter shows up currently. My enum from accounts is called "type" with label "Account Type". I'm not sure how to reference that value. This is what I have in the vendor.js view for that field.

          getSelectFilters: function () {
          return {
          'accountType': {
          type: 'anyOf',
          value: 'Vendor',
          valueName: this.model.get('accountName'),
          }
          };
          }

          Last edited by joy11; 08-11-2017, 07:08 PM.

          Comment


          • #6
            Is there a spot where I could just enter the mysql query to pull the accounts marked as vendors and have them selectable in the link drop-down? I'm trying to work within the parameters of the program, but this seems like it should be something very easy and is giving me problems for way too long.

            This is what I want to be able to select from the linked field: SELECT `id` FROM `account` WHERE `deleted` = 0 AND `type` LIKE 'Vendor'
            Anyone know how to do this?

            Comment


            • #7
              You can use the code;
              PHP Code:
              $accountList $entityManager->getRepository('Account')->where(array(    
                 
              'type' => 'Vendor',    
              ))->
              find(); 

              Comment


              • #8
                I've been working with custom views that are .js and the custom entityDefs that are .json
                I'm not sure where to put this php so it is called/used. Do I create a function of vendorFilter in a Custom/SelectManagers/CurrentProducts/CurrentProducts.php
                then envoke that on the clientDefs? I've tried so many different things and read so many different forums looking for hints that I don't know what direction is correct.

                Will this allow me to populate the link's select field?
                Last edited by joy11; 08-22-2017, 03:23 PM.

                Comment


                • #9
                  I think, you can add to a filter to an Account: custom/Espo/Custom/SelectManagers/Account.php. Examples located at application/Espo/Modules/Crm/SelectManagers/Account.php

                  Comment


                  • #10
                    So if I create: custom/Espo/Custom/SelectManagers/Account.php

                    <?php
                    namespace custom\Espo\Custom\SelectManagers\Account;

                    class Account extends \Espo\Modules\Crm\SelectManagers\Account
                    {
                    protected function filterVendor(&$result)
                    {
                    $result['whereClause'][] = array(
                    'type' => 'Vendor'
                    );
                    }
                    }

                    The above code is similar to what was in the application/Espo/Modules/Crm/SelectManagers/Account.php

                    I'm not sure how to call this function from my entity, CurrentProduct, for the linked field "vendor" which is linked to the Account Entity. I have 25,000+ Accounts and 75 of them are "Vendors". I only want the 75 Vendors to come up in the select or when typed in the field in my CurrentProduct entity's vendor field.

                    All of the other examples I have seen are only for filtering other entities that already have an association with an account (like contacts etc) instead of filtering from a value defined in the account entity already. I've viewed every post in the forums which even remotely dealt with filters, but i'm not getting it to work. I'm not sure if I need to work with the view and entityDef or the SelectManager and the clientDef of my CurrentProduct entity, or now maybe the Account.php?
                    Last edited by joy11; 08-25-2017, 09:21 PM.

                    Comment


                    • #11
                      I've tried to just focus on one part at a time now. I know that I can get the value to filter in the select by using the custom view and the entityDef. I'll worry about the text complete after I get the select menu to work.

                      I'm able to get the words "Account Type" to show up in the selection menu for the Vendor link. It does not pick up any of the other information set in the js however and I'm unable to select Vendor. I am able to add the "Account Type" filter from the add fields and then select vendor that way, but I want to preset it.

                      I'm assuming it is because I don't have this portion formed correctly, but any hint would be appreciated.

                      getSelectFilters: function () {
                      return {
                      'Account Type': {
                      type: 'anyOf',
                      value: 'Vendor',
                      }
                      };
                      }
                      Last edited by joy11; 08-30-2017, 09:58 PM.

                      Comment


                      • #12
                        It's pretty easy. Used Chrome DevTools to find the correct search parameters.
                        Code:
                        getSelectFilters: function () {            
                                    return {
                                        'type': {
                                            type: 'in',
                                            value: ['Vendor']
                                        }
                                    };            
                                },
                        Tested. It works fine.
                        Last edited by tarasm; 08-29-2017, 01:46 PM.

                        Comment


                        • #13
                          tarasm Thank you! This works!
                          I figured it was something easy with the right syntax or key words. I would have never thought the type would be 'in'. Now I will try to tackle the text filter.

                          Comment

                          Working...
                          X