Use the payroll method
GetList().
$arOrder = ["ID" => "DESC"]; // sorting
$arFilter = ["UF_DEAL_ID" => $dealId]; // filter by ID of the transaction
$arSelect = ["ID", "UF_DEAL_ID"]; // what fields to choose
$invoices = CCrmInvoice::GetList($arOrder, $arFilter, false, false, $arSelect);
while($invoice = $invoices->Fetch()) {
// your code
}
This problem can occur if the transaction multiple accounts (actually, so cycle is used), so you will need additional filtering options.
You can also use the rest method
crm.invoice.list. But you will need to additionally generate incoming webhook and use
HttpClient to invoke a method
in this case, the code will be like this:
use Bitrix\Main\Web\HttpClient;
use Bitrix\Main\Web\Json
$client = new HttpClient;
$webhook = "https://webhook_url/crm.invoice.list";
$params = [
"filter" => $arFilter,
"order" => $arOrder
];
$request = $client->post($webhook, Json::encode($params));
$result = Json::decode($request);
foreach($result["result"] as $res) {
// your code
}