Custom Field Like Name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ananya K
    Member
    • Jul 2020
    • 50

    Custom Field Like Name

    I am trying to create a custom view for a group of fields like the name.

    I have customized the `Name` field so that it will group the fields salutation, first name, middle name. last name and the suffix name. And it's working fine as expected.

    Now, I am trying to create a new view of fields same as Name for the postal code.
    I am trying to create a field that will show both zip4 and zip 5 which is read-only.

    Files that I have created:
    - espocrm/client/custom/src/views/fields/postalCode.js
    - espocrm/custom/Espo/Custom/Resources/metadata/fields/postalCode.json
    - espocrm/custom/Espo/Custom/Core/Utils/Database/Orm/Fields/PostalCode.php

    And the field definition under entity def is,
    PHP Code:
    "postalCodes": {
    "type": "postalCode",
    "view": "custom:views/fields/postalCode",
    "readOnly": true,
    "inlineEditDisabled": true
    },

    1. The field is able to display the value in the detail view. But in the list view, the field is showing the value undefined. If the zip4 and zip5 fields are added to the list view then the value is displayed.
    2. The same problem with the report and the value is not exported.

    Findings:
    1. The espocrm/custom/Espo/Custom/Core/Utils/Database/Orm/Fields/PostalCode.php is not getting executed. Because of this the
    entity->fields['postalCodes']['select'] is not creating a right query.

    Can anyone please help me how to fix this? Or Am I missing anything here?
    Attached Files
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    I don't use the advanced pack so I can't help you with reports but I can try to help solving the "undefined" issue in list view.

    Could you please post the code for the files that you created ?

    Comment

    • esforim
      Active Community Member
      • Jan 2020
      • 2204

      #3
      Won't be able to help you with either of the question but I have two tip that might help you progress a bit instead:

      (1) have a look Real Estate extension that might be helpful ( https://www.espocrm.com/extensions/real-estate/ );

      (2) secondly is to test it with other address field to see if it work, e.g. try Street, or State, or Country or City. One issue Postcode is it doesn't support Auto-complete/Options. But State, Country and City support that (see admin > settings > Address City Autocomplete List ) or URL: /#Admin/settings/
      And I'm not sure if it relevant to what you trying to do. It might just be the field Postal Code that causing you to fail rather than the coding itself.

      Comment


      • Ananya K
        Ananya K commented
        Editing a comment
        I don't have Real Estate Extension.
        I just need a field view like the name which will show Zip4 and Zip5 and which will work in Report export also.

      • esforim
        esforim commented
        Editing a comment
        Hi Ananya,

        Real Estate Extensions is free to download and install.

        What I'm saying is: there 'may' be code in there that give you some information to help what you trying to do.
    • Ananya K
      Member
      • Jul 2020
      • 50

      #4
      I have uploaded the above-listed files in txt format.
      • postaCodeJS.txt => espocrm/client/custom/src/views/fields/postalCode.js
      • PostalCodeJson.txt => espocrm/custom/Espo/Custom/Resources/metadata/fields/postalCode.json
      • PostalCodeOrm.txt => espocrm/custom/Espo/Custom/Core/Utils/Database/Orm/Fields/PostalCode.php
      Attached Files

      Comment

      • telecastg
        Active Community Member
        • Jun 2018
        • 907

        #5
        I managed to incorporate the custom "postalCode" type field to the Contact entity and display in both list and detail modes using the following scripts:

        1) Custom field metadata definition:
        custom/Espo/Custom/Resources/metadata/fields/postalCode.json
        Code:
        {
            "view": "custom:views/fields/postal-code",
            "actualFields":[
                "postalCode4"
            ],
            "notActualFields": [
                "postalCode"
            ],
            "params":[
                {
                    "name":"required",
                    "type":"bool",
                    "default":false
                }
            ],
            "fields":{
                "postalCode4":{
                    "type": "varchar",
                    "trim": true,
                    "maxLength": 40
                },
                "postalCode" :{
                    "type": "varchar",
                    "trim": true,
                    "maxLength": 40,
                    "notStorable": true,
                    "select": {
                        "select": "addressPostalCode"
                    }
                }
            },
            "notMergeable":true,
            "notCreatable":true,
            "filter":true,
            "skipOrmDefs": true,
            "personalData": true
        }
        2) Custom field front-end class:
        client/custom/src/views/fields/postal-code.js
        Code:
        define('custom:views/fields/postal-code', 'views/fields/varchar', function (Dep) {
        
            return Dep.extend({
        
                type: 'postalCode',
        
                init: function () {
                    Dep.prototype.init.call(this);
                    this.postalCode4Field = 'postalCodesPostalCode4';
                    this.postalCodeField = 'postalCodesPostalCode';
                },
        
                data: function () {
                    var data = Dep.prototype.data.call(this);
                    data.postalCode4Value = this.model.get(this.postalCode4Field);
                    data.postalCodeValue = this.model.get(this.postalCodeField);
                    if (this.mode === 'detail') {
                        data.isNotEmpty = !!data.postalCode4Value || !!data.postalCodeValue;
                    } else if (this.mode === 'list' || this.mode === 'listLink') {
                        data.isNotEmpty = !!data.postalCode4Value || !!data.postalCodeValue;
                    }
        
                    if (data.isNotEmpty && this.mode == 'detail' || this.mode == 'list' || this.mode === 'listLink') {
                        data.formattedValue = this.getFormattedValue();
                        data.value = data.formattedValue;
                    }
                    data.valueIsSet = this.model.has(this.postalCode4Field) || this.model.has(this.postalCodeField);
        
                    return data;
                },
        
                getFormattedValue: function () {
                    var code4 = this.model.get(this.postalCode4Field);
                    var code = this.model.get(this.postalCodeField);
                    var value = '';
                    value += ' ' + code;
                    value += '-' + code4;
                    value = value.trim();
        
                    return value;
                },
        
            });
        });
        3) Custom metadata entityDefs script for the Contact entity to incorporate the new custom field type:
        custom/Espo/Custom/Resources/metadata/entityDefs/Contact.json
        Code:
        {
            "fields" : {
                "postalCodes": {
                    "type": "postalCode",
                    "view": "custom:views/fields/postal-code",
                    "readOnly": true,
                    "inlineEditDisabled": true
                }
            }
        }
        4) Custom layout definitions for the Contact entity to display the new custom field in list and detail modes:
        custom/Espo/Custom/Resources/layouts/Contact/list.json
        Code:
        [
            {
                "name": "name",
                "link": true
            },
            {
                "name": "phoneNumber"
            },
            {
                "name": "emailAddress",
                "width": 20,
                "notSortable": true
            },
            {
                "name": "postalCodes"
            }
            {
                "name": "tenancies",
                "notSortable": true
            }
        ]
        custom/Espo/Custom/Resources/layouts/Contact/detail.json
        Code:
        [
            {
                "rows": [
                    [
                        {
                            "name": "name"
                        },
                        false
                    ],
                    [
                        {
                            "name": "phoneNumber"
                        },
                        {
                            "name": "emailAddress"
                        }
                    ],
                    [
                        {
                            "name": "address"
                        },
                        {
                            "name": "postalCodes"
                        }
                    ]
                ],
                "style": "default",
                "label": "Overview"
            }
        ]
        5) Custom language metadata translation of the custom field label (choose your own language folder for this)
        custom/Espo/Custom/Resources/i18n/en_US/Contact.json
        Code:
        {
            "fields": {
                "postalCodes": "Postal Codes"
            }
        }
        Notes:

        This solution works with Espo 6.0.4 in a test local site

        I am very busy right now refactoring our extensive custom code to work with Espo 6+ so I didn't have time to do it, but I believe that a complete solution would require additionally:

        a) A zip4 field input control (the field is read-only at present) to enter Contact information manually

        b) Testing and if necessary develop code for searching or exporting

        c) Optionally develop a custom "address" definition that incorporates the zip+4 new field instead of the current "addressPostalCode" field

        I also did not use the ORM field class because it wasn't necessary for my solution and also because like you, I couldn't figure out how to invoke the class like its done for the "personName" field type.
        As I understand, the custom field ORM definition is supposed to be called by the "correctFields()" function in the "Converter" class but I could not get the custom field ORM class to fire. Not sure if its some kind of a bug or just my lack of knowledge

        If you make changes/improvements to this solution please post them here to benefit everyone else.
        Last edited by telecastg; 03-02-2021, 05:50 PM.

        Comment


        • esforim
          esforim commented
          Editing a comment
          For some reason your image attachment is not working.

        • telecastg
          telecastg commented
          Editing a comment
          Do you mean the screen capture images ?, they show up fine in my computer ???

          UPDATE: I can't see the former screen captures either... not sure why and don't have them in my computer anymore, so I removed them from the positing
          Last edited by telecastg; 03-02-2021, 05:51 PM.
      • Ananya K
        Member
        • Jul 2020
        • 50

        #6
        telecastg The solution is not working for me!!
        I did some modifications still I have the same problem with the value.

        What is that

        PHP Code:
        
        "actualFields":[ "postalCode4" ],
        "notActualFields": [ "postalCode" ], 
        
        In the entiftDef of the fields. What does it will do actually?

        Comment

        • telecastg
          Active Community Member
          • Jun 2018
          • 907

          #7
          Originally posted by Ananya K
          telecastg The solution is not working for me!!
          I did some modifications still I have the same problem with the value.
          Not sure why, the code worked in our local test site. Check the console to see if any errors are thrown.

          Originally posted by Ananya K
          What is that


          "actualFields":[ "postalCode4" ],
          "notActualFields": [ "postalCode" ],


          In the entiftDef of the fields. What does it will do actually?
          The modified table for "Contact" will have a new field "postal_code_postal_code4" where you store the zip code extra 4 digits, "postalCode" is a runtime reference, not an actual database field.

          Comment

          • Ananya K
            Member
            • Jul 2020
            • 50

            #8
            telecastg

            I have customized the address which will add the addressPostalCode4 to it.
            I am trying to create the postalCodes which will show the addresspostalCode4 and addressPostalCode in one field. (Like name, address).
            But in the list view, the value is coming like undefined -undefined. If I add addressPostlCode and addressPostalCode4 to the list view then only it will come. I don't want that. Also, the value should be exported in the reports.

            Because
            Code:
                 this.model.get(this.postalCode4Field)
            is not loaded in this.model while loading the list view.

            espocrm/custom/Espo/Custom/Resources/metadata/fields/postalCode.json

            Code:
            {
                "view": "custom:views/fields/postal-code",
                "actualFields":[
                    "addressPostalCode4",
                    "addressPostalCode"
                ],
                "notActualFields": [
                    ""
                ],
                "params":[
                {
                    "name":"required",
                    "type":"bool",
                    "default":false
                }
            ],
            "fields":{
                "addressPostalCode4":{
                   "type": "varchar",
                    "trim": true,
                   "maxLength": 40,
                    "select": {
                        "select": "addressPostalCode4"
                    }
                },
               "addressPostalCode":{
                    "type": "varchar",
                    "trim": true,
                    "maxLength": 40,
                     "select": {
                         "select": "addressPostalCode"
                     }
               }
            },
            "notMergeable":true,
            "notCreatable":true,
            "filter":true,
            "skipOrmDefs": true,
            "personalData": true
            }
            But in the this.model field postalCodesAddressPostalCode will be loaded. But this is the wrong field name. I want addressPostalCode and addressPostalCode4 should be loaded. Any idea? whats is the problem?

            Comment

            • telecastg
              Active Community Member
              • Jun 2018
              • 907

              #9
              I think that it will help understanding the way "complex" fields like "address" or "personName" work in Espo.

              The attribute/field "address" is a complex virtual "field" (not a real database table column) made up of five real/actual model attributes/fields (sub-fields) "street", "city", "state", "country" and "postalCode".

              When you add an "address" attribute/field to an entity ("Contact" for example), Espo prepends the word "address" to the actual field names, since they are sub-fields of "address", and adds then five properties to the model: "addressStreet", "addressCity", "addressState", "addressCountry" and "addressPostalCode", so to get one of these values, "street" for example, you would write
              Code:
              this.model.get('addressStreet');
              (Because of Espo's naming conventions, the attributes become database table columns: "address_street", "address_city", "address_state", "address_country" and "address_postal_code".)

              The solution that I posted was for your original goal of creating an independent field, and thus creates a complex field "postalCodes" that is made up of two "sub-fields":

              1) "postalCode4" which is the field where you store the additional 4 digits for the zip code and becomes "postalCodesPostalCode4" entity attribute and "postal_codes_postal_code4" database field.

              2) "postalCode" which becomes "postalCodesPostalCode" attribute and it is not an actual database field, but a runtime reference to the value contained in the entity's attribute "addressPostalCode".

              Notice how these two attributes are prepended with "postalCodes" because they are sub-fields of "postalCodes".

              As I mentioned in my post, one of the possible improvements was to modify the "address" field to incorporate the zip plus 4 capability instead of using a stand alone field like "postalCodes", and I think that this is what you are trying to accomplish now.

              To do that you will need to create a custom "addressPlus4" field cloning the existing "address" field type and making the necessary modifications to incorporate an actual zip4 field, then you will need to substitute the field "address" for "addressPlus4" in all entities where you want to display the zip plus 4 capability.

              With regards to displaying it in reports, we don't have the advance pack so I can't help you with that part.

              One question is why do you need to have two separate fields for zip code ?. Unless you have a specific need to segregate the zip code information, you could just enter the complete string in the existing postalCode field instead of creating a whole new "address" custom field.
              Last edited by telecastg; 12-01-2020, 12:31 AM.

              Comment

              • Ananya K
                Member
                • Jul 2020
                • 50

                #10
                telecastg

                I have to store zip code 5 and zip code 4 in different fields. So I have customized the address to support zip codes 4 and 5 and it's working fine.

                I am doing all this for Reporting purposes. I need to create a PostalCode field like Name which will merge the addressPostalCode4 and addressPostalCode and export the same in the report. (like how the name field is working)

                Can anyone help me? Is it possible to do?
                Attached Files

                Comment

                • telecastg
                  Active Community Member
                  • Jun 2018
                  • 907

                  #11
                  Originally posted by Ananya K
                  I have to store zip code 5 and zip code 4 in different fields. So I have customized the address to support zip codes 4 and 5 and it's working fine.
                  I'm happy to hear that it's working now, to correspond please post all the the complete final scripts here, so other participants can see how it was done and learn from your experience



                  Comment

                  • Ananya K
                    Member
                    • Jul 2020
                    • 50

                    #12
                    telecastg For Address it is working fine.

                    I want another field like Name which will display addressPostalCode4 and addressPostalCode in one field and also able to export in the report.

                    Can anyone help me out in this?

                    Comment

                    Working...