Examples
Several basic examples of handling CRM entities. Examples are given for leads, but can be used similarly to handle other CRM entities.
Adding a lead.
if (\Bitrix\Main\Loader::includeModule('crm')) { $entity = new CCrmLead; $fields = array( 'TITLE' => 'Test' ); echo $entity->add($fields); }
Modifying, deleting a lead
<?php if (\Bitrix\Main\Loader::includeModule('crm')) { $entity = new CCrmLead(true);//true - checking access permissions $fields = array( 'TITLE' => 'Test' ); $entity->update(1, $fields); $entity->delete(1); }
Retrieving operations. Main principles are practically similar to information blocks retrieval.
<?php if (\Bitrix\Main\Loader::includeModule('crm')) { CCrmLead::GetListEx( $arOrder = array(), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array()); }
Access permissions are checked by default during the retrieval. To cancel access permission check, CHECK_PERMISSIONS => 'N'
must be passed in the filter.
Adding a simple product. Simple product - is the product, attached to an entity, bypassing the product catalog. It will not be saved separately in the database.
<?php if (\Bitrix\Main\Loader::includeModule('crm')) { $rows = array(); $rows[] = array( 'PRODUCT_NAME' => 'Insurance', 'QUANTITY' => 2, 'PRICE' => 300, 'MEASURE_CODE' => 796 ); $rows[] = array( 'PRODUCT_NAME' => 'Manager visit', 'QUANTITY' => 1, 'PRICE' => 100, 'MEASURE_CODE' => 796 ); CCrmProductRow::SaveRows('D', 10, $rows);//attach to deal CCrmProductRow::SaveRows('L', 8, $rows);//attach to lead CCrmProductRow::SaveRows('Q', 1, $rows);//attach to quotation //for invoices, it's little different - when updating or adding, specify via separate field CCrmInvoice::add(array( 'ORDER_TOPIC' => 'New invoice', 'PRODUCT_ROWS' => $rows )); }
Adding product to a catalog
<?php if (\Bitrix\Main\Loader::includeModule('crm')) { $pid = CCrmProduct::add(array( 'NAME' => 'Product in the database', 'QUANTITY' => 1, 'PRICE' => 100, 'MEASURE_CODE' => 796, 'CURRENCY_ID' => 'USD', )); if ($pid) { $rows = array(); $rows[] = array( 'PRODUCT_ID' => $pid, 'QUANTITY' => 1, ); CCrmProductRow::SaveRows('D', 10, $rows); } }
Get CODE of the update.
<?php if (\Bitrix\Main\Loader::includeModule('crm')) { //retrieving list of updates \Bitrix\Crm\Measure::getMeasures(); //пretrieving list of updates by default \Bitrix\Crm\Measure::getDefaultMeasure(); //array of the following type is returned: /* [ID] => 1 [CODE] => 6 [IS_DEFAULT] => [SYMBOL] => m */ }
Retrieving codes for reference entities of selection lists
<?php if (\Bitrix\Main\Loader::includeModule('crm')) { $fields = array( 'TYPE_ID' => CCrmActivityType::Call, //type of the deal itself, can be as follows: Meeting, Call, Task, Email (class constants) 'OWNER_TYPE_ID' => CCrmOwnerType::Deal, //type of main entity, to which the deal is attached, //can be a Deal, Lead, Contact, Company (class constants) 'OWNER_ID' => 10, //ID of the main entity 'SUBJECT' => 'Outgoing phone call', 'START_TIME' => $date, 'END_TIME' => $date, 'COMPLETED' => 'Y', //deal completed flag 'RESPONSIBLE_ID' => 1, //responsible 'PRIORITY' => CCrmActivityPriority::Medium, //priority, can be None, Low, Medium, High (class constants) 'DESCRIPTION' => 'Description', 'DIRECTION' => CCrmActivityDirection::Outgoing, //relevant for type of deals, where direction of call can be specified //(incoming or outgoing - phone or email), //can be Outgoing, Incoming (class constants) 'BINDINGS' => array( array( 'OWNER_TYPE_ID' => CCrmOwnerType::Deal, 'OWNER_ID' => 643 ) ), //array of additional associations - for example, call can be associated with a deal (main association), // not yet associated with contact - with WHOM the call is made. 'ORIGIN_ID' => 'XYZ_1' //some external identifier, optional ); CCrmActivity::Add($fields, true); }
© «Bitrix24», 2001-2024