P
P
Pavel Sidorov2021-11-16 15:03:15
1C-Bitrix
Pavel Sidorov, 2021-11-16 15:03:15

How to add an element to an infoblock through an agent?

Good!
I imagine the process of adding an element using api to the infoblock
code

if(CModule::IncludeModule("iblock")){
    $el = new CIBlockElement;
    $IBLOCK_ID_CATALOG = 53; 

    $arLoadProductArray = Array( 
      "IBLOCK_ID" => $IBLOCK_ID_CATALOG,  
      "NAME" => "тест1", 
      "ACTIVE" => "Y", 
      "PREVIEW_TEXT" => "Позиция успешно добавлена",
    ); 
    
    if($newElement = $el->Add($arLoadProductArray)) {  
      echo "Позиция добавлена"; }
    else { 
      echo "Error: ".$el->LAST_ERROR; 
    }
  }


Important line - If the module is not connected - an error. Connecting either with the help of a prologue, or we connect heder. I have a question. I want to create an agent so that it adds an element to the infoblock, but the highlight of the program is that I don’t understand how to connect the prologue - it gives an error. I decided to include the file itself in init.php. But the element is not added due to the absence of CModule::IncludeModule("iblock"). Tell me how to implement?CModule::IncludeModule("iblock")



function addAgent()
{
  include $_SERVER['DOCUMENT_ROOT']."/api/element.php";
  mailAgent(); // в файле /api/element.php находится еще функция отправки теста на почту и она работает
  return "addAgent();";
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Sidorov, 2021-11-16
@pavel__sidorov

shortened the code
added to init.php - the agent adds 1 element and falls off.

function testAgent()
{

    if(CModule::IncludeModule("iblock")){

      $el = new CIBlockElement;
      $IBLOCK_ID_CATALOG = 53; 
  
      $arLoadProductArray = Array( 
        "IBLOCK_ID" => $IBLOCK_ID_CATALOG,
        "NAME" => "тест1", 
        "ACTIVE" => "Y",
        "PREVIEW_TEXT" => "Позиция успешно добавлена",
      ); 
      if($newElement = $el->Add($arLoadProductArray));

    }
}

function AddIBElementByAgent()
{
   AddEventHandler("main", "OnProlog", "testAgent");
   return "AddIBElementByAgent()";
}

T
tgarl, 2021-11-16
@tgarl

You are smart about something.
Go to the admin panel and create an agent with the testAgent() function;
in init.php create your function

function testAgent()
{

    if(CModule::IncludeModule("iblock")){

      $el = new \CIBlockElement;
      $IBLOCK_ID_CATALOG = 53; 
  
      $arLoadProductArray = Array( 
        "IBLOCK_ID" => $IBLOCK_ID_CATALOG,
        "NAME" => "тест1", 
        "ACTIVE" => "Y",
        "PREVIEW_TEXT" => "Позиция успешно добавлена",
      ); 
      $newElement = $el->Add($arLoadProductArray);
    }
return "testAgent();";
}

all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question