Unique field error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Exsto
    Member
    • Nov 2018
    • 58

    Unique field error

    Hi, I would like to make the VAT and fiscal code fields unique, the fields must be varchar. It's possible to do it?
    I tried this:

    Originally posted by theBuzzyCoder

    One can add
    Code:
    {
    "fields": {
    "fieldName": {
    "unique": true
    }
    }
    }
    in metadata.entityDefs.Entity.json under fields.fieldName
    But, when I save a database error.
    Any suggestions?

    Thank you​​​​​​
  • Exsto
    Member
    • Nov 2018
    • 58

    #2
    Could anyone help me?
    Thanks

    Comment

    • theBuzzyCoder
      Senior Member
      • Feb 2018
      • 102

      #3
      Yes, you can. The info I gave you was just a guide, not the solution. You've to think about how you can make it work.

      you cannot create a field without the type.

      custom/Espo/Custom/Resources/metadata/entityDefs/<your-entity>.json

      substitute <your-entity> with right value. If the vat field is in Lead then <your-entity> is Lead, so your file will be custom/Espo/Custom/Resources/metadata/entityDefs/Lead.json


      Code:
      {
        "fields": {
           "vat": {
             "type": "varchar",
             "maxLength": 255,
             "notNull": true,
             "trim": true,
             "required": true,
             "readOnly": false,
             "unique": true
          }
        }
      }
      Then from your EspoCRM website, go to EspoCRM Administration > Label Manager > Lead > fields and update the vat fields label.

      Comment

      • Exsto
        Member
        • Nov 2018
        • 58

        #4
        Thank you now it works, you have been very kind.
        There is only one problem, if the field already exists from error 500, how can I display a personalized message as "VAT number already present"
        Thanks again

        Comment


        • theBuzzyCoder
          theBuzzyCoder commented
          Editing a comment
          Your question is unclear. Are you asking for a duplicate check of field values? Can you elaborate your question with screenshots if possible

        • eymen.elkum
          eymen.elkum commented
          Editing a comment
          he means the error message that appears when you try to save new entity with an already exist unique field value, catching the error and make it friendly
      • theBuzzyCoder
        Senior Member
        • Feb 2018
        • 102

        #5
        Originally posted by Exsto
        Thank you now it works, you have been very kind.
        There is only one problem if the field already exists from error 500, how can I display a personalized message as "VAT number already present"
        Thanks again
        Create a service in this place

        espocrm/custom/Espo/Custom/Services/<Your-Entity>.php where <your-entity> is like Lead

        PHP Code:
        namespace Espo\Custom\Services;
        
        use Espo\ORM\Entity;
        
        class Lead extends \Espo\Modules\Crm\Services\Lead
        {
          protected function getDuplicateWhereClause(Entity $entity, $data)
          {
            return array('vat' => $entity->get('vat'));
          }
        } 
        
        Now try creating a duplicate.

        your extends <class> depends on what entity it is.

        If you check espocrm/application/Espo/Modules/Crm/Services there will be a file called Lead.php. That's why I can extend it. If you don't know what to extend with, just check espocrm/application/Espo/Modules/Crm/Services path first to see if there is a file corresponding to <your-entity>, otherwise extend below metioned class

        \Espo\Services\Record

        which points to espocrm/application/Espo/Services/Record.php file
        Last edited by theBuzzyCoder; 09-19-2019, 11:56 AM.

        Comment

        • Exsto
          Member
          • Nov 2018
          • 58

          #6
          Hello, thank you for having responded.
          I tried how you wrote, but when I try to save a lead it gives me this error:
          Errore 500: Class '\Espo\Custom\Services\Lead' does not exist.

          [2019-09-26 18:42:14] Espo.ERROR: (500) Class '\Espo\Custom\Services\Lead' does not exist.; GET /api/v1/Lead?select=salutationName%2CfirstName%2ClastName% 2Cname%2Cstatus%2CaccountName%2CemailAddressIsOpte dOut%2CemailAddress%2CemailAddressData%2CassignedU serId%2CassignedUserName%2CcreatedAt&maxSize=100&o ffset=0&orderBy=createdAt&order=desc; line: 116, file: /var/www/html/espocrm/application/Espo/Core/ServiceFactory.php

          Could you give me some suggestions?
          Thank you very much

          Comment


          • theBuzzyCoder
            theBuzzyCoder commented
            Editing a comment
            Can you put the code you wrote here. The full file
        • Exsto
          Member
          • Nov 2018
          • 58

          #7
          Good morning,
          Can someone help me?
          Thanks

          Comment

          • Exsto
            Member
            • Nov 2018
            • 58

            #8
            Hi, this is the file
            Thanks

            PHP Code:
            namespace Espo\Custom\Services;
            
            use Espo\ORM\Entity;
            
            class Lead extends \Espo\Modules\Crm\Services\Lead
            {
              protected function getDuplicateWhereClause(Entity $entity, $data)
              {
                return array('partita_iva' => $entity->get('partita_iva'));
              }
            } 
            

            Comment


            • theBuzzyCoder
              theBuzzyCoder commented
              Editing a comment
              Where did you place this file? give me absolute path or relative path from espocrm folder. Also Go to Administration > click on clear cache

              and retry once
          • Exsto
            Member
            • Nov 2018
            • 58

            #9
            Hi, sorry unfortunately the Google translator helps a lot but not enough. I didn't understand what you'd like to see
            Thanks

            Comment

            • Exsto
              Member
              • Nov 2018
              • 58

              #10
              the Lead.php file was created here: espocrm/custom/Espo/Custom/Services

              Comment

              • Exsto
                Member
                • Nov 2018
                • 58

                #11
                Hello,
                I solved now it works fine, but when I create a lead and not when I change it.
                Can I insert the alert also in edit? If so, how can I do it?
                Thanks

                Comment


                • Exsto
                  Exsto commented
                  Editing a comment
                  Ok, that's why I solved it
              • Exsto
                Member
                • Nov 2018
                • 58

                #12
                I am using the duplicate check also for the e-mail address. Can I get a lock so I can't save the record?

                Comment

                • eduardofendrich
                  Member
                  • Jul 2021
                  • 49

                  #13
                  Originally posted by Exsto
                  Hello,
                  I solved now it works fine, but when I create a lead and not when I change it.
                  Can I insert the alert also in edit? If so, how can I do it?
                  Thanks
                  HI, how did you solve that?

                  thanks

                  Comment

                  Working...