there is such a code (php 8) bitrix24
function show($res,$title=''){
echo '<pre>';
echo $title;
echo '<br/>';
print_r($res);
echo '</pre>';
}
//bath с формулой
/*
получить значение в элементе перечисления (enum)
$data массив вложенных ассоциативных массивов
$keyFind имя ключа поиска
$find значение ключа для поиска
$def значение ключа для поиска по умолчанию
$keyVal имя клююча результата/значения
пример
_getObjParam($data, 'NAME', 'Сайт www.cad.ru', 'Личный контакт', 'STATUS_ID');
*/
function _getObjParam(array $data, $keyFind, $find, $def, $keyVal) {
show (['inType'=>gettype($data),'data'=>$data,'keyFind'=> $keyFind,'find'=> $find, 'def'=> $def,'keyVal'=> $keyVal],'in');
$ret='';
$arrID=[];
if (!empty($find)) //если есть что искать
$arrID = array_column(array_filter($data, fn ($v) => array_key_exists($keyFind, $v) && $v[$keyFind] == $find),$keyVal);
if(empty($arrID) && !empty($def)) //если ничего не найдено но есть значение по умолчанию
$arrID = array_column(array_filter($data, fn ($v) => array_key_exists($keyFind, $v) && $v[$keyFind] == $def),$keyVal);
if(!empty($arrID)) //если что то нашли берем первое
$ret=$arrID[0];
// $ret=1;
show ($ret,'out');
return $ret;
};
$interpolate = "_getObjParam";
$previos_res_single='$result[crm_source_status][2][STATUS_ID]';
$previos_res_array='$result[crm_source_status]';
$leadID=18036;
$sourceDescription = 'Сайт www.cad.ru';
$arData = [
'crm_source_status' => [ //массив статусов
'method' => 'crm.status.list',
'params' => ['filter' => ["ENTITY_ID" => "SOURCE",]]
],
'1_crm_lead_const' => [ //одно значение из массива статусов напрямую
'method' => 'crm.lead.update',
'params' => ['ID'=>$leadID, 'fields' => ["SOURCE_ID" => '$result[crm_source_status][0][STATUS_ID]',]]
],
'2_crm_lead_const' => [ //одно значение из массива статусов через переменную
'method' => 'crm.lead.update',
'params' => ['ID'=>$leadID, 'fields' => ["SOURCE_ID" => $previos_res_single,]]
],
'3_crm_lead_const' => [ //одно значение из массива статусов просто числом
'method' => 'crm.lead.update',
'params' => ['ID'=>$leadID, 'fields' => ["SOURCE_ID" => 4,]]
],
'4_crm_lead_const' => [ //одно значение из массива статусов через внешнюю функцию
'method' => 'crm.lead.update',
'params' => ['ID'=>$leadID, 'fields' => [
"SOURCE_ID" => "{$interpolate($previos_res_array,'NAME', $sourceDescription, 'Личный контакт', 'STATUS_ID')}",
// 'SOURCE_ID'=>call_user_func_array('_getObjParam', array('$result[crm_source_status]', 'NAME', $sourceDescription, 'Личный контакт', 'STATUS_ID')),
]]
],
$result = CRest::callBatch($arData, $halt = 0);
show ($result);
the first request crm_source_status returns an array , all subsequent requests process it via '$result[crm_source_status]...' this is the response of the previous request returned in the context of batch execution
queries 1_crm_lead_const and 2_crm_lead_const handle values well in context
but I need to pre-process the results... and I call an external function and want to pass there the results of the first crm_source_status request in the 4_crm_lead_const request
according to the logs, the external function is triggered immediately before the batch request - and accordingly receives an input not an array from the context, but just a string...
to stay in the context (i.e., to wait for the execution of the previous request) - I probably need to register a dynamic function (string) for the request 4_crm_lead_const - ....
'params' => ['ID'=>$leadID, 'fields' => ["SOURCE_ID" => eval('function (){...
but I do not know how to do it... tell me?
ps
the result of the previous query '$result[crm_source_status]' is given in string format (single quotes) and not interpolation...
perhaps the word '$result is sewn into their api and has some other meaning... - I do not know
Array
(
[result] => Array
(
[result] => Array
(
[crm_source_status] => Array
(
....
== '$result[crm_source_status]'
... log CRest::callBatch return...
in
Array
(
[inType] => string
[data] => $result[crm_source_status]
[keyFind] => NAME
[find] => Сайт www.cad.ru
[def] => Личный контакт
[keyVal] => STATUS_ID
)
out
Array
(
[result] => Array
(
[result] => Array
(
[crm_source_status] => Array
(
[0] => Array
(
[ID] => 242
[ENTITY_ID] => SOURCE
[STATUS_ID] => 1
[NAME] => Контакт Lead
[NAME_INIT] =>
[SORT] => 10
[SYSTEM] => N
[CATEGORY_ID] => 0
[COLOR] => #D3EEF9
[SEMANTICS] =>
)
[1] => Array
(
[ID] => 240
[ENTITY_ID] => SOURCE
[STATUS_ID] => UC_YKLMC8
[NAME] => Личный контакт
[NAME_INIT] =>
[SORT] => 20
[SYSTEM] => N
[CATEGORY_ID] => 0
[COLOR] =>
[SEMANTICS] =>
)
[2] => Array
(
[ID] => 244
[ENTITY_ID] => SOURCE
[STATUS_ID] => 2
[NAME] => Сайт www.cad.ru
[NAME_INIT] =>
[SORT] => 30
[SYSTEM] => N
[CATEGORY_ID] => 0
[COLOR] => #D3EEF9
[SEMANTICS] =>
)
...
pps
can split it (batch) into several queries and work fine - but it's not pretty ))
$arData = [
'crm_source_status' => [ //массив статусов
'method' => 'crm.status.list',
'params' => ['filter' => ["ENTITY_ID" => "SOURCE",]]
],
];
$res = CRest::callBatch($arData, $halt = 0); //batch request
//show ($res);
echo _getObjParam($res['result']['result']['crm_source_status'], 'NAME', 'Сайт www.cad_.ru', 'Личный контакт', 'STATUS_ID');
$param = [
'ID'=>$leadID,
'fields' => ["SOURCE_ID" => _getObjParam($res['result']['result']['crm_source_status'], 'NAME', 'Сайт www.cad_.ru', 'Личный контакт', 'STATUS_ID')]
];
show($param);
$lead = CRest::call('crm.lead.update', $param); //single request
show($lead);
log
UC_YKLMC8
Array
(
[ID] => 17906
[fields] => Array
(
[SOURCE_ID] => UC_YKLMC8
)
)
Array
(
[result] => 1
[time] => Array
(
[start] => 1699711672.4691
[finish] => 1699711672.7058
...