query on the data object used in handler/select-related

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jgarcia
    Junior Member
    • Aug 2023
    • 18

    query on the data object used in handler/select-related

    Good night all

    I have an entity called "Categorías de producto" that is related to another entity "Fabricante" in the following form Categoría de producto Many-to-One Fabricante.

    I am testing with the following code. This code is in the following path "client/src/handlers/select-related/same-fabricante-many.js" and is configured for a field of the opportunity entity

    Code:
    define('handlers/select-related/same-fabricante-many', ['handlers/select-related'], Dep => {
        return class extends Dep {
            getFilters(model) {
                let advanced = {};
                let fabricantesIds = ['65a66c52349517ee9', '65a66c67bd94f491d'];
                let nameHash = {
                    '65a66c52349517ee9': "HPE",
                    '65a66c67bd94f491d': "Fortinet",
                };
                advanced.fabricante = {
                    attribute: 'fabricanteId',
                    type: 'in',
                    value: fabricantesIds,
                    data: {
                        type: 'isOneOf',
                        nameHash: nameHash,
                    },
                };
                return Promise.resolve({
                    advanced: advanced,
                });
            }
        }
    });​
    The result is as follows:

    Click image for larger version

Name:	almost correct result.png
Views:	346
Size:	89.0 KB
ID:	101668

    As you can see, the filter does it correctly but the names are not shown in the place they should go.

    Doing a little research I came to the conclusion that if the relationship between "Categorias de producto" and "Fabricante" was Many-to-Many, the names would be displayed correctly, changing the advanced.fabricante to advanced.fabricantes due to the new relationship.

    I think that in data there must be a configuration so that the names are displayed correctly with the first relationship between the two entities.

    Please, if anyone could guide me or give me any information about it, I would be very grateful.
    Attached Files
  • yuri
    Member
    • Mar 2014
    • 8454

    #2
    Hi,

    For the data parameter in advanced search items I recommend to look into the field view that corresponds to the relationship. It can be link (for belongsTo), link-multile, link-parent views. For your case, it's here https://github.com/espocrm/espocrm/b...s/link.js#L939
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #3
      Originally posted by jgarcia
      Good night all

      I have an entity called "Categorías de producto" that is related to another entity "Fabricante" in the following form Categoría de producto Many-to-One Fabricante.

      I am testing with the following code. This code is in the following path "client/src/handlers/select-related/same-fabricante-many.js" and is configured for a field of the opportunity entity

      Code:
      define('handlers/select-related/same-fabricante-many', ['handlers/select-related'], Dep => {
      return class extends Dep {
      getFilters(model) {
      let advanced = {};
      let fabricantesIds = ['65a66c52349517ee9', '65a66c67bd94f491d'];
      let nameHash = {
      '65a66c52349517ee9': "HPE",
      '65a66c67bd94f491d': "Fortinet",
      };
      advanced.fabricante = {
      attribute: 'fabricanteId',
      type: 'in',
      value: fabricantesIds,
      data: {
      type: 'isOneOf',
      nameHash: nameHash,
      },
      };
      return Promise.resolve({
      advanced: advanced,
      });
      }
      }
      });​
      The result is as follows:

      Click image for larger version

Name:	almost correct result.png
Views:	346
Size:	89.0 KB
ID:	101668

      As you can see, the filter does it correctly but the names are not shown in the place they should go.

      Doing a little research I came to the conclusion that if the relationship between "Categorias de producto" and "Fabricante" was Many-to-Many, the names would be displayed correctly, changing the advanced.fabricante to advanced.fabricantes due to the new relationship.

      I think that in data there must be a configuration so that the names are displayed correctly with the first relationship between the two entities.

      Please, if anyone could guide me or give me any information about it, I would be very grateful.
      what is the relationship between opportunity and the two entities ?
      Rabii
      Web Dev

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #4
        If you have a one to many between opportunity and Fabricante or Fabricante is a parent in opportunity then you need to change the code, try this code below and make sure that the names of the relationships are correct:

        PHP Code:
        define('handlers/select-related/same-fabricante-many', ['handlers/select-related'], Dep => {
            return class extends Dep {
        
                getFilters(model) {
                    const advanced = {};
        
                    let fabricanteId = null;
                    let fabricanteName = null;
        
                    if (model.get('fabricanteId')) {
                        fabricanteId = model.get('fabricanteId');
                        fabricanteName = model.get('fabricanteName');
                    }
        
                    if (!fabricanteId && model.get('parentType') === 'Fabricante' && model.get('parentId')) {
                        fabricanteId = model.get('parentId');
                        fabricanteName = model.get('parentName');
                    }
        
                    if (fabricanteId) {
                        advanced.fabricante = {
                            attribute: 'fabricanteId',
                            type: 'equals',
                            value: fabricanteId,
                            data: {
                                type: 'is',
                                nameValue: fabricanteName,
                            },
                        };
                    }
        
                    return Promise.resolve({
                        advanced: advanced,
                    });
                }
            }
        });

        PS: this is not upgrade safe, i suggest that you create this file under custom directory to make sure it is upgrade safe.

        Hope this helps
        Rabii
        Web Dev

        Comment


        • jgarcia
          jgarcia commented
          Editing a comment
          Thank you very much for your help Rabii, but the relationship is the one I indicated in the previous comment, due to that relationship, I created my own file based on the example of the Case entity, which is similar to the code you provided me. Anyway, it now works as I wanted.

          PS: thanks for the advice, I have a custom directory the reason I was working in the directory mentioned above is because I thought the directory was the reason it wasn't showing me the names.uctos
      • jgarcia
        Junior Member
        • Aug 2023
        • 18

        #5
        Originally posted by yuri
        Hi,

        For the data parameter in advanced search items I recommend to look into the field view that corresponds to the relationship. It can be link (for belongsTo), link-multile, link-parent views. For your case, it's here https://github.com/espocrm/espocrm/b...s/link.js#L939
        Thank you very much Yuri, now if it works correctly, I will also follow the recommendation.

        Comment

        • jgarcia
          Junior Member
          • Aug 2023
          • 18

          #6
          Originally posted by rabii

          what is the relationship between opportunity and the two entities ?
          The relationship is the following:

          opportunity many to many Fabricante
          opportunity Many-to-many Categoria de productos​

          Comment

          Working...