Make a relationship field unique

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shalmaxb
    Senior Member
    • Mar 2015
    • 1602

    Make a relationship field unique

    In an 1:n relationship I want to prevent, that a user can choose an already choosen related record of another entity not would be able to choose again.
    How is that possible?
  • esforim
    Active Community Member
    • Jan 2020
    • 2204

    #2
    I'm assuming it is only possible (without coding is):

    1) Filter relationship = blank
    2) Change it to some sort of n:1 (I can never understand this) where relationship can only exist as 1.
    3) Formula, search and give error if relationship link is found.

    None of these are elegant solution, and I'm sure your EspoCRM skill is better than mine.

    Comment

    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #3
      Originally posted by shalmaxb
      In an 1:n relationship I want to prevent, that a user can choose an already choosen related record of another entity not would be able to choose again.
      How is that possible?
      what do you mean? by default espocrm will not duplicate the selected resources it will be overwritten.

      Still not sure what you want to achieve though
      Rabii
      Web Dev

      Comment

      • shalmaxb
        Senior Member
        • Mar 2015
        • 1602

        #4
        Hello,

        I have an Invoice entity (custom, not from sales pack), where I create invoice items from a stock, that is unique (means, each article to sell is available only one time). Let´s say, I sold an article and invoiced it by adding to an invoice position (that is the relationship, which should be unique).
        When I write another invoice, I don`t want this article available anymore to choose as an item again. Though I need to keep it in the "stock", it should not be deleted there, when sold.

        Comment

        • item
          Active Community Member
          • Mar 2017
          • 1476

          #5
          Hi,
          for me, the best way is so :

          Invoice 1:n : product
          product 1:1 article with unique number ( i remember you asked before on forum)

          and when you select a article, apply default filter like « not selled article »

          this give you many way to customise your crm and open the ERP way

          If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

          Comment

          • rabii
            Active Community Member
            • Jun 2016
            • 1250

            #6
            hey

            If it is a 1:n meaning one invoice has many items, then when selecting the invoice you can apply a filter to return only items that has no invoice meaning invoiceId is empty.
            Rabii
            Web Dev

            Comment

            • shalmaxb
              Senior Member
              • Mar 2015
              • 1602

              #7
              Ok thanks, I get it.
              Theoretically.

              I understand, that I can create a filter and apply to a certain list view as primary filter. So far so good. But from explanations I only find old depracted ways and newer, that I cannot understand. It seems easy, but as often, from the reduced information, which I gather from different posts and threads it has not been possible for me so far to implement that.
              I would really be grateful for a clear procedure. Perhaps anyone can help?

              Comment


              • esforim
                esforim commented
                Editing a comment
                I think once v8 hit and we can export Entity it will be more 'convenient' to post solution.
            • rabii
              Active Community Member
              • Jun 2016
              • 1250

              #8
              hey i am happy to help, can you share name of entities and links between them and i will bake something for you.
              Rabii
              Web Dev

              Comment

              • shalmaxb
                Senior Member
                • Mar 2015
                • 1602

                #9
                rabii, thank you for your generous offer. I hope I will be able to explain understandable. Unfortunately my app is in German, but I translated the terms.

                I have three entities involved

                1. Artwork (Werke)
                2. Item Position Sale (Positionen Atelierverkauf)
                3. Invoice (Atelierverkauf). Here the item list will be displayed, what works as desired.

                In the first step I create the invoice positions. The positions fetch the items from Artwork, where the filtering should happen. I already created a filter, but by default, when fetching the artwork the filter is set to "All". I want to achieve, that the filter ist set to "available", what means, that I want to show all items, that had not been invoiced before. Because an invoiced artwork is sold ant not available. The manual filter is set to the linked field with the parameter empty/null, which works as desired.
                One problem might occur: as far as I saw, the configured filter is set in the layout manager, but without difference between list view and small list view. That means, that in the main entity (Artwork/Werke), from where I fetch the items the default list in that entity will also be filtered, what I do not want. In the entity Artwork/Werke I want to se all recordes, being invoiced or not. I don`t know, if it would be possible to filter the list view different to the list view small.

                Invoice positions item list

                list of linked items to be filtered

                Relation beteen position and artwork

                Comment

                • rabii
                  Active Community Member
                  • Jun 2016
                  • 1250

                  #10
                  Hey shalmaxb

                  Based on what you provided above, you can achieve this easily, just create a custom view for the field (atelierrechnungWerke) on the entity (Positionen Werke), this should be under client\custom\src\views\positionen-werke\fields\atelierrechnung-werke.js (please make sure the names are correct of both entity and the field. and just paste the code below (also review the names of the entity and the field as i am not sure how they are provided in your instance):

                  PHP Code:
                  define('custom:views/positionen-werke/fields/atelierrechnung-werke', 'views/fields/link', function (Dep) {
                  
                     return Dep.extend({
                  
                          getSelectPrimaryFilterName: function () {
                              return 'available';
                          },
                  
                      });
                  });

                  Please note that i assumed the filter available is a primary filter, otherwise if it is Bool filter then use method below instead:

                  PHP Code:
                  define('custom:views/positionen-werke/fields/atelierrechnung-werke', 'views/fields/link', function (Dep) {
                  
                     return Dep.extend({
                  
                          getSelectBoolFilterList: function () {
                              return ['available'];
                          },
                  
                      });
                  });

                  Don't forget to add the custom view to the field on the positionen werke entityDefs.

                  Hope this helps.
                  Last edited by rabii; 08-24-2023, 03:04 PM.
                  Rabii
                  Web Dev

                  Comment

                  • shalmaxb
                    Senior Member
                    • Mar 2015
                    • 1602

                    #11
                    rabii, first, thank you a lot for your support.

                    Unfortunately, I did not get it to work yet. I guess, because I may have some error at some place.

                    First I created the desired filter, which of course manually works.

                    I think, the error comes with the entity and field names, as you already mentioned. So I will tell, what I put there:

                    In this line: views/positionen-werke/fields/atelierrechnung-werke

                    I put the name of the entity, in which the related field is present. The entity is named: PositionenAtelierrechnung, so I put there positionen-atelierrechnung
                    The field is named: atelierrechnungWerke, so I named it atelierrechnung-werke

                    The whole path now looks as this:

                    Code:
                    custom:views/positionen-atelierrechnung/fields/atelierrechnung-werke
                    The file name is atelierrechnung-werke.js

                    As filter name, I put the name of the former created filter.

                    In the entity PositionenAtelierrechnung i added to the field atelierrechnungWerke under section "links" in entityDefs of that entity the following:

                    Code:
                    "atelierrechnungWerke": {
                    "type": "belongsTo",
                    "foreign": "positionenAtelierrechnung",
                    "entity": "Werke",
                    "audited": false,
                    "isCustom": true,
                    "view": "custom:views/positionen-atelierrechnung/fields/atelierrechnung-werke"
                    },
                    This last part might be wrong also, as I do not know, how it has to be correct.​

                    Comment


                    • rabii
                      rabii commented
                      Editing a comment
                      is the filter created from the UI ? if it is the case then it won't work, because the filter needs to be programmed. i am happy to help get this done, just need to know how the filter works meaning, is it based on a boolean field on Werke entity?

                    • shalmaxb
                      shalmaxb commented
                      Editing a comment
                      Yes, the filter had been created by UI. To save your time, let me try to follow the tutorial from telecastg (https://forum.espocrm.com/forum/deve...g-a-link-field).
                      Perhaps I will succeed with that. If not, I will reach out again.

                    • rabii
                      rabii commented
                      Editing a comment
                      sure you just need to create a filter and that should work
                  Working...