I
I
Ivan Eremin2019-09-04 17:46:35
PHP
Ivan Eremin, 2019-09-04 17:46:35

How to make a request to the Bitrix24 rest api in a loop?

Faced a problem.
There are Bitrix webinars on js for requests to the Bitrix24 rest api. But, I'm not in the tooth with my foot on JS.
How to make a rest api request with a loop to get deals and comments to them into an array for the required period?
the result produces 50 responses. Just a line through the POST request helpers - the incoming JSON is visible.
But how do you wrap it all up? Broke my whole head. And after all, all this still needs to be placed in an array and driven through the component somehow into another Bitrix :) For the
second week (or already the third) I have been racking my brains, re-reading, reviewing webinars. It's unclear.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kulgan, 2019-09-05
@kulgan

I'll try to answer what I understand:
You can't get more than 50 in one request - the api limitation. To get the rest of the values, let's look at the official doc(transactions) . Let's see this code

if(result.more())
                result.next();

, he receives the remaining transactions. If you insert it as in the example from the docks, then it will receive transactions until it receives everything.
var allDeals = []; 
// тут код запроса, но для краткости начнём с callback кода 
function(result) 
    {
        if(result.error())
            console.error(result.error());
        else
        {
            for(let i = 0; i < result.data().length; i++){   //этим кодом вставляем полученные сделки в массив всех 
                 allDeals.push(result.data()[i]);               //полученных сделок
            }		
            if(result.more())
                result.next();						
        }
    }

If there are always 100+ transactions in the response, then it is better to understand batch requests

M
MrHyde, 2019-09-07
@MrHyde

You can use the library https://github.com/2BAD/bitrix where the processes of obtaining data have already been optimized.
A couple of lines will be enough

import Bitrix from '@2bad/bitrix'

const bitrix = Bitrix('https://PORTAL_NAME.bitrix24.ru/rest', 'ACCESS_TOKEN')

// Получить сделку с ID: 77
bitrix.deals.get('77')
  .then(({ result }) => {
    // Get typed payload
    const { TITLE } = result // string
    console.log(TITLE)
  })
  .catch(console.error)

// Получить все сделки и их пользовательские поля
bitrix.deals.list({ select: ["*", "UF_*"] })
  .then(({ result }) => {
    const titles = result.map((e) => e.TITLE)
    console.log(titles)
  })
  .catch(console.error)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question