Announcement

Collapse
No announcement yet.

Unique field error

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

  • 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 View Post

    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​​​​​​

  • #2
    Could anyone help me?
    Thanks

    Comment


    • #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


      • #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

      • #5
        Originally posted by Exsto View Post
        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, 12:56 PM.

        Comment


        • #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

        • #7
          Good morning,
          Can someone help me?
          Thanks

          Comment


          • #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

          • #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


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

              Comment


              • #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

                Working...
                X