A
A
Artem Mikhnevich2019-08-19 10:24:46
JavaScript
Artem Mikhnevich, 2019-08-19 10:24:46

How to send data to Bitrix24?

Kind people, please tell me and direct me to the right path)
By clicking, I send data to Bitrix24 via webhooks.
On idea I should transfer obligatory parameters. But all such examples are in PHP, and here I am trying to make the inventor of the bicycle using JS.
Maybe I'm not passing the correct url or Parameters

let btn = document.querySelector('button');
    btn.addEventListener('click', createLid());

let fields =
  {
    "TITLE": "ИП Титов",
    "NAME": "Глеб",
    "SECOND_NAME": "Егорович",
    "LAST_NAME": "Титов",
    "STATUS_ID": "NEW",
    "OPENED": "Y",
    "ASSIGNED_BY_ID": 1,
    "CURRENCY_ID": "USD",
    "OPPORTUNITY": 12500,
    "PHONE": [ { "VALUE": "555888", "VALUE_TYPE": "WORK" } ]
  };
let params = { REGISTER_SONET_EVENT: "Y" };
let REGISTER_SONET_EVENT = {
  'LOGIN': "xxxxxxx", // обязательно, логин для доступа к crm
  'PASSWORD':'xxxxxxxx', // обязательно, пароль для доступа к crm
  'TITLE': 'test-plugin' // обязательно, название лида
}
let url = `http://crmtest.domen.ru/rest/967/key4g4g4g4g/profile/crm.lead.add(${fields}, ${params})`;
let url2 = "http://crmtest.domen.ru/rest/crm.lead.add.json?fields[TITLE]=Обратныйзвонок&fields[PHONE]='34345345'&fields[NAME]='dsfs'&auth=key4h4h4h";

function createLid() {
  fetch(url, {
    method: 'post'
  })
  .then(function (data) {
    console.log('Request succeeded with JSON response', data);
  })
  .catch(function (error) {
    console.log('Request failed', error);
  });
};

Fetch returns a successful response. With content like this
body: (...)
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 404
statusText: "Not Found"
type: "cors"

and such an error
Failed to load resource: the server responded with a status of 404 (Not Found)

That is, I'm not forming the url correctly. But I found another example on the forums, in the code: url2
but it also answers the same.
Maybe I'm not doing the whole request correctly, I need Jedi advice)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
smilingcheater, 2019-08-19
@timartinov

let url = `http://crmtest.domen.ru/rest/967/key4g4g4g4g/profile/crm.lead.add(${fields}, ${params})`;

You will be defined, you form URL or a method call? You form an incorrect url, so you get 404.
As far as I remember, the URL should look something like this
let url = ` http://crmtest.domen.ru/rest/967/key4g4g4g4g/profi...
And all the listed fields must be sent via POST . Now in url2 they are all attributed to you in the GET query string.

A
ArmorDarks, 2019-09-19
@ArmorDarks

You can use the @2bad/bitrix JavaScript (TypeScript) client , which will greatly simplify the task:

import Bitrix from '@2bad/bitrix'

const bitrix = Bitrix('https://PORTAL.bitrix24.ua/rest/1/WEBHOOK_URL')

bitrix.leads.create({
  TITLE: 'Test lead'
})
  .then(console.log)

A
Artem Mikhnevich, 2019-09-19
@timartinov

If anyone will be useful, I did so. Using webhooks to create a lead.

fetch(url, {
      method: 'post',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        fields: data.fields
      })
    })
    .then(function (response) {
      loader.classList.add("hiden");
      success.classList.remove("hiden");
      return console.log('Request succeeded', response.json());
    })
    .catch(function (error) {
      loader.classList.add("hiden");
      fail.classList.remove("hiden");
      console.log('Request failed', error);
    });
};

let data = {
  fields: {
    TITLE: "",
    NAME: "",
    STATUS_ID: "20",
    OPENED: "20",
    LAST_NAME: "",
    SOURCE_ID: "",
    POST: "",
    BIRTHDATE: "",
    UF_CRM_KOMMENTARII: '',
    UF_CRM_1545200238: '216', // тип лида
    PHONE: [{
      VALUE: "",
      VALUE_TYPE: "WORK"
    }],
    EMAIL: [{
      VALUE_TYPE: "WORK",
      VALUE: "",
    }],
    WEB: [{
      VALUE_TYPE: "WORK",
      VALUE: "",
    }]
  },
  params: {
    "REGISTER_SONET_EVENT": "Y"
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question