How to display the Ajax Get Request response on the front-end

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Prathyush Shetty
    Junior Member
    • Oct 2023
    • 22

    How to display the Ajax Get Request response on the front-end

    I'm trying to get the list of cases followed by the user. I made a Espo.Ajax.getRequest(), which returns me a list of cases. I want it to be displayed in the Case list view.
    PHP Code:
    updateCollection: function () {
      this.collection.abortLastFetch();
      this.collection.reset();
      this.collection.where = this.searchManager.getWhere();
    // if the case filter selected is "follower"
      if (this.searchManager.data.advanced.follower) {
        Espo.Ajax.getRequest('Subscription/action/Followers', {
          ids: this.searchManager.data.advanced.follower.value,
          type: this.searchManager.data.advanced.follower.type
        }).then(response => {
          //return response;
        });
      }
      Espo.Ui.notify(this.translate('pleaseWait', 'messages'));
      this.collection.fetch().then(function () {
        Espo.Ui.notify(false);
      });
    },

    I tried passing caseids in this.collection.where, it works, but only if the count of ids is less. If the list of caseIds is huge it throws Error 414, saying Request URI is too long.

    Is there any way we could work with collections without getting 414, or somehow display the ajax response directly on the UI.
  • yuri
    Member
    • Mar 2014
    • 8452

    #2
    I'm not sure I understand enough the problem to be able to help.

    Maybe you need to use the "followed" bool filter instead.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • Prathyush Shetty
      Junior Member
      • Oct 2023
      • 22

      #3
      "followed" bool filter will display the cases followed by the logged-in user. I want a filter that displays the cases followed by any user. So I made an Ajax get request for that. It returns me the list of cases that are followed by any given user. how do I display this result on the front-end?

      Comment

      • yuri
        Member
        • Mar 2014
        • 8452

        #4
        1. Create a custom "link" type field with notStorable: true.

        Code:
        {
            "fields": {
                "isFollowedBy": {
                       "type": "link",
                       "notStorable": true,
                       "entity": "User"
                 }
            }
        }
        2. Define whereItemConverter for this field. Example: https://github.com/espocrm/espocrm/b.../Email.json#L2

        3. Create a converter class that will apply filter.

        Then you will pass the filter and handler it in the backend.
        If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

        Comment


        • yuri
          yuri commented
          Editing a comment
          I'm quite busy now, won't be able to answer on questions.
      • Prathyush Shetty
        Junior Member
        • Oct 2023
        • 22

        #5
        Hey yuri, I hope you have some time to answer a few of my questions.

        I followed the steps you suggested:

        1. created a custom "link" with notStorable: true

        2. defined whereItemConverter for this field in custom/selectDefs/Case.json
        PHP Code:
        "whereItemConverterClassNameMap": {
        "follower_equals": "Espo\\Custom\\Classes\\Select\\CaseObj\\Where\\ItemConverters\\FollowerEquals"
        }

        3. Created a converter class.
        PHP Code:
        <?php
        namespace Espo\Custom\Classes\Select\CaseObj\Where\ItemConverters;
        use Espo\Core\Select\Where\Item;
        use Espo\Core\Select\Where\ItemConverter;
        use Espo\ORM\Query\Part\WhereItem as WhereClauseItem;
        use Espo\ORM\Query\SelectBuilder as QueryBuilder;
        
        class FollowerEquals implements ItemConverter
        {
        public function convert(QueryBuilder $queryBuilder, Item $item): WhereClauseItem
        {
        ... // some code
        }
        }

        What do I do after that? I didn't get the part when you said "Then you will pass the filter and handler it in the backend"
        Last edited by Prathyush Shetty; 01-22-2024, 06:30 AM.

        Comment

        • rabii
          Active Community Member
          • Jun 2016
          • 1250

          #6
          It means use that filter in your backend like in a controller. check this existing example for user entity. https://github.com/search?q=repo%3Ae...Type&type=code
          Rabii
          Web Dev

          Comment

          • Prathyush Shetty
            Junior Member
            • Oct 2023
            • 22

            #7
            I tried to use this filter in the Case's controller, but it threw an error "Error 500: Unknown where item type 'followerEquals'​ "

            PHP Code:
            public function getActionList(Request $request, Response $response): stdClass
            {
              $searchParams = $this->fetchSearchParamsFromRequest($request);
              $findParams = $this->findParamsFetcher->fetch($request);
              $whereParams = $request->getQueryParams();
              foreach ($whereParams['where'] as $whereCondition){
                if ($whereCondition['attribute']== 'followerId') {
                  $value = $whereCondition['value'];
                }
              }
              if ($value){
                $searchParams = $searchParams->withWhereAdded(
                  WhereItem::fromRaw([
                    'type' => 'followerEquals',
                    'attribute' => 'id',
                    'value' => $value
                  ])
                );
              }
              $recordCollection = $this->getRecordService()->find($searchParams, $findParams);
              return (object) [
                'total' => $recordCollection->getTotal(),
                'list' => $recordCollection->getValueMapList(),
              ];
            }

            Comment


            • rabii
              rabii commented
              Editing a comment
              (follower_equals) follower has be a fieldname as per documentation The key format: {fieldName}_{conditionItemType}.

            • Prathyush Shetty
              Prathyush Shetty commented
              Editing a comment
              Field:

              "follower": {
              "type": "link",
              "notStorable": true,
              "entity": "User"
              }


              whereItemConverter:

              "whereItemConverterClassNameMap": {
              "follower_equals": "Espo\\Custom\\Classes\\Select\\CaseObj\\Where\\It emConverters\\FollowerEquals"
              }​


              Calling filter in Controller:

              $searchParams = $searchParams->withWhereAdded(
              WhereItem::fromRaw([
              'type' => 'followerEquals',
              'attribute' => 'id',
              'value' => $value
              ])
              );
          Working...