How to add a custom filter with custom enum field values?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rgcadavos
    Junior Member
    • Jan 2017
    • 28

    How to add a custom filter with custom enum field values?

    Hi All,

    I want to have a custom filter search that displays record lists with last action like last Email and last Call. I would like to have the values Email and Call in a custom enum field.

    I tried creating the file custom/Espo/Custom/Selectmanagers/Contact.php

    <?php

    namespace Espo\Custom\SelectManagers;

    use Espo\Modules\Crm\SelectManagers\Contact as ContactCrm;

    class Contact extends ContactCrm
    {
    protected function filterCustomEnum(&$result)
    {
    $result['customJoin'] .= " LEFT JOIN note AS notes ON contact.id = notes.parent_id AND notes.deleted = 0 ";
    $this->addAndWhere(array(
    'notes.type' => 'EmailSent'
    ), $result);
    }

    }

    I get still get the whole collection. Im new to EspoCRM and still having hard time extending customs.

    Anyone can enlighten me on this?

    Thanks.
    Last edited by rgcadavos; 03-14-2017, 09:40 AM.
  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    Hello

    check the path "custom/Espo/Custom/Selectmanagers/Contact.php" (SelectManagers - folder name is case sensitive)
    add enpoint (log some text) to know, if filter is read

    did you add to custom clientDefs filter definition, like

    Code:
    {
        "filterList": [
          {
             "name":"customEnum"
          }
       ]
    }
    and select in on the list?

    Comment

    • rgcadavos
      Junior Member
      • Jan 2017
      • 28

      #3
      Yes, but in the total count display. It says Total: 2 but it displays only 1 record, though i have sent 2 email on that record. Is my query wrong or is there a need to add $this->limit() ???

      Comment

      • tanya
        Senior Member
        • Jun 2014
        • 4308

        #4
        application/Espo/ORM/DB/Query/Base.php method createSelectQuery
        add endpoint before return, like
        Code:
         if ($entityName == "Contact") { $GLOBALS['log']->error($sql);}
        run query from log file in phpMyAdmin and analyze, what you need to add to query (possible contact.id duplicates because of one of JOIN)

        Comment

        • rgcadavos
          Junior Member
          • Jan 2017
          • 28

          #5
          Hi @tanya,

          By the way, thanks for the responses.

          I have added

          $this->applyOrder('note.modified_at', true, $result);
          $result['distinct'] = true;

          and it did return the correct total count and was sorted to the latest activity.

          here is my final code,

          Code:
          protected function filterEmailSent(&$result)
          {
          $result['customJoin'] .= " LEFT JOIN note ON contact.id = note.parent_id AND note.deleted = 0";
          $this->addAndWhere(array(
          'note.type' => 'EmailSent'
          ), $result);
          $this->applyOrder('note.modified_at', true, $result);
          $result['distinct'] = true;
          }
          protected function filterHasCall(&$result)
          {
          $result['customJoin'] .= " LEFT JOIN note ON contact.id = note.parent_id AND note.deleted = 0";
          $this->addAndWhere(array(
          'note.related_type' => 'Call'
          ), $result);
          $this->applyOrder('note.modified_at', true, $result);
          $result['distinct'] = true;
          }
          and Contact.json in clientdef,

          Code:
          "filterList": [
          {
          "name":"emailSent",
          "label":"Email Sent"
          },
          {
          "name":"hasCall",
          "label":"Has Call"
          }
          ]
          but the label doesnt work well when selected. Can you help? is this a bug?
          Last edited by rgcadavos; 03-16-2017, 03:33 PM.

          Comment

          • tanya
            Senior Member
            • Jun 2014
            • 4308

            #6
            Hi
            Translation for this is in "presetFilters" section.
            Code:
            "presetFilters": {
                    "customEnum": "custom - Enum"
                }

            Comment

            • wtconseil
              Active Community Member
              • Apr 2015
              • 335

              #7
              can we do the translate / presetFilters through "custom" folder (=> e.g. custom/Espo/Custom/Resources/i18n/fr_FR/Contact.json )

              or should we do that in application/Espo/Resources/i18n/fr_FR/Global.json ?

              Comment

              • tanya
                Senior Member
                • Jun 2014
                • 4308

                #8
                of course, you can set it in custom/Espo/Custom/Resources/i18n/fr_FR/Contact.json

                Comment

                • rgcadavos
                  Junior Member
                  • Jan 2017
                  • 28

                  #9
                  Thanks a lot, tanya

                  Comment

                  • wtconseil
                    Active Community Member
                    • Apr 2015
                    • 335

                    #10
                    A small question :

                    If i had my filter in => application/Espo/Modules/Crm/Resources/metadata/clientDefs/Contact.json
                    PHP Code:
                        "filterList": [
                    -      "portalUsers"
                    +      "portalUsers",
                    +      "MyFilter"
                        ] 
                    
                    => it's working

                    but i was thinking i could add it in custom folder => custom/Espo/Custom/Resources/metadata/entityDefs/Contact.json
                    i try to add
                    PHP Code:
                    "filterList": [
                    -      "portalUsers"
                    +      "portalUsers",
                    +      "MyFilter"
                        ] 
                    
                    but .. not working.

                    any reason ?

                    If i let it in application folder, maybe it will be replace but one of your file during an upgrade?

                    Thanks

                    Comment

                    • tanya
                      Senior Member
                      • Jun 2014
                      • 4308

                      #11
                      Hi

                      you added to entityDefs, needs to clientDefs folder
                      also check if custom/Espo/Custom/Resources/metadata/clientDefs/Contact.json is valid and is readable.

                      Comment

                      • wtconseil
                        Active Community Member
                        • Apr 2015
                        • 335

                        #12
                        Oh thanks ! you're right !

                        Really ... really .. thanks for your awesome support!
                        everything is ok for me now :-))

                        PHP Code:
                            modified:   custom/Espo/Custom/Resources/i18n/fr_FR/Contact.json
                            new file:   custom/Espo/Custom/Resources/metadata/clientDefs/Contact.json
                            new file:   custom/Espo/Custom/Selectmanagers/Contact.php 
                        
                        :-)))

                        Comment

                        • theBuzzyCoder
                          Senior Member
                          • Feb 2018
                          • 102

                          #13
                          Hi,

                          I am trying to do the same with leads but its not working

                          here's my code and path to file

                          /home/user/projects/www/espocrm/custom/Espo/Custom/SelectManagers/Lead.php

                          PHP Code:
                          <?php
                          namespace Espo\Custom\SelectManagers;
                          
                          use Espo\Modules\Crm\SelectManagers\Lead as LeadCrm;
                          
                          class Lead extends LeadCrm
                          {
                              protected function filterNewLeads(&$result)
                              {
                                  $result['whereClause'][] = array(
                                      'status=' => 'New'
                                  );
                              }
                          }
                          ?>

                          /home/user/projects/www/espocrm/custom/Espo/Custom/Resources/metadata/clientDefs/Lead.json

                          Code:
                          {
                              "filterList": [
                                  {
                                      "name": "newLeads"
                                  }
                              ]
                          }
                          /home/codaxtr_user/projects/www/espocrm/custom/Espo/Custom/Resources/i18n/en_US/Lead.json

                          Code:
                          {
                              "presetFilters": {
                                  "newLeads": "New Leads"
                              }
                          }
                          The filter "New Leads" is not showing up in the Leads

                          Last edited by theBuzzyCoder; 02-07-2018, 10:01 AM.

                          Comment

                          • tanya
                            Senior Member
                            • Jun 2014
                            • 4308

                            #14
                            Hello
                            Check if your json files are valid (maybe it's a part of json, if not I see extra commas). And filter name has to be "name": "newLeads" (lower camel case)

                            Comment

                            • theBuzzyCoder
                              Senior Member
                              • Feb 2018
                              • 102

                              #15
                              It started working somehow!

                              In Administrator when I got to label manager -> Lead -> presetFilters and change the value, where exactly is it updated.

                              In database(if yes, please specify which table / field) or Lead.json
                              Last edited by theBuzzyCoder; 02-07-2018, 01:36 PM.

                              Comment

                              Working...