P
P
po4emu4ka20202021-05-02 12:07:32
1C-Bitrix
po4emu4ka2020, 2021-05-02 12:07:32

How to correctly collect a batch request to receive goods from several transactions at once, the number of which is initially unknown?

I am learning to use RestAPI in order to work on web hooks with B24.
It is not possible to correctly collect a batch request in any way.

I have a subquery with deals by a certain status. Next, I want to form a query that will pull out goods from all these transactions. But to understand how I can do it, and whether I can do it at all, I can’t.

Here is what I have:

$listRes = CRest::call(
    'batch',
    array(
        'halt' => 0,
      'cmd'=> array(
          'deals' => 'crm.deal.list?filter[STAGE_ID]=PREPAYMENT_INVOICE',
          'products' => 'crm.deal.productrows.get?id=$result[deals][0][ID]',
      )
      )
    );
echo '<pre>';
print_r($listRes['result']);
echo '</pre>';


This code, of course, outputs only the products of the first deal, since the zero index of the result is clearly specified: '
crm.deal.productrows.get?id=$result[deals][ 0 ][ID]'
this index should change just dynamically?
Tell me please.
Thanks in advance for any advice.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2021-05-12
@po4emu4ka2020

How to specify in the query that this index should change just dynamically?

There is no such possibility in Bitrix24.
The point lies in a slightly different vision of the problem. When you execute a request to get the quantity, it returns you no more than 50 elements with a pagination, but a batch is only 50 requests, that is, if such a mechanism took place, then the batch would be rubber, since in fact it must execute 51 requests (1 per getting the list and 50 more for getting each element). How would a batch request behave when you specified 3 requests? For example, getting a list of deals, getting specific deals, and getting some kind of reference book? Run 52 requests? No, it's fantasy.
What hack can be used?
You can generate requests yourself based on the hack.
Since in a batch the query result is given by a serial number (unless otherwise specified), they will always be in the range from 0 to 49.
For example, we want to get 10 deals from a page in one query, then we can send the following batch query:
'halt' => 0,
'cmd'  => [
  'deals' => 'crm.deal.list?filter[STAGE_ID]=PREPAYMENT_INVOICE&filter[>ID]=0&limit=10',
  'products_0' => 'crm.deal.productrows.get?id=$result[deals][0][ID]',
  'products_1' => 'crm.deal.productrows.get?id=$result[deals][1][ID]',
  ...
  'products_8' => 'crm.deal.productrows.get?id=$result[deals][8][ID]',
  'products_9' => 'crm.deal.productrows.get?id=$result[deals][9][ID]',
]

When we iterate "products_*" we will get the last processed ID (for example it will be 123456) and we can execute the following batch query:
'halt' => 0,
'cmd'  => [
  'deals' => 'crm.deal.list?filter[STAGE_ID]=PREPAYMENT_INVOICE&filter[>ID]=123456&limit=10',
  'products_0' => 'crm.deal.productrows.get?id=$result[deals][0][ID]',
  'products_1' => 'crm.deal.productrows.get?id=$result[deals][1][ID]',
  ...
  'products_8' => 'crm.deal.productrows.get?id=$result[deals][8][ID]',
  'products_9' => 'crm.deal.productrows.get?id=$result[deals][9][ID]',
]

But in any case, you should understand that the more data your batch request returns, the slower it will be executed, so I recommend limiting the list of received fields in crm.deal.list as well.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question