Skip to main content

How to write custom Bitrix24 activity

How to write custom business-process activity for cloud version Bitrix24? 8 Simple Steps - for memory:

1) Create application structure

<?php
$protocol=$_SERVER['SERVER_PORT']=='443'?'https':'http';
$host=explode(':',$_SERVER['HTTP_HOST']);
$host=$host[0];
define(‘BP_APP_HANDLER',$protocol.'://'.$host.$_SERVER['REQUEST_URI']);
header('Content-Type: text/html; charset=UTF-8');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src=«//api.bitrix24.com/api/v1/"></script>
</body>
</html>

2) Check and process data

<?php
function clean($value = "") {
$value = trim($value);
$value = stripslashes($value);
$value = strip_tags($value);
$value = htmlspecialchars($value);
return $value;
}
?>
<?php
function check_length($value = "", $min, $max) {
$result = (mb_strlen($value) < $min || mb_strlen($value) > $max);
return !$result;
}
?>

3) Using Bitrix24 REST-api on php

<?
function callB24Method(array $auth, $method, $params){
$c=curl_init('https://'.$auth['domain'].'/rest/'.$method.'.json');
$params["auth"]=$auth["access_token"];
curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
curl_setopt($c,CURLOPT_POST,true);
curl_setopt($c,CURLOPT_POSTFIELDS,http_build_query($params));
AddMessage2Log($c, "demo_user_info");
$response=curl_exec($c);
AddMessage2Log($response, "demo_user_info");
$response=json_decode($response,true);
return $response['result'];
}
?>
<?php
$user_info=callB24Method($auth,'user.get',array(
‘ID’=>$ID
)
);?>

4) Method of Bitrix24 application debugging

<?php
define("LOG_FILENAME", $_SERVER[«DOCUMENT_ROOT»].»/__log_castom1_".date("Ymd").".txt»);
AddMessage2Log($user_info, "demo_user_info");
?>

5) Using Bitrix24 REST-api on javascript

BX24.callMethod(
'bizproc.activity.add',
params,
function(result)
{
if (result.error())
alertify.alert('Error: '+result.error());
else
alertify.alert('Installation successfully completed');
}
);

6) Creating and deleting simple activity for cloud Bitrix24

BX24.init(function()
{
});
function installActivity1()
{
var params={
'CODE':'user_additional_info',
'HANDLER':'<?=BP_APP_HANDLER?>',
'AUTH_USER_ID':1,
'USE_SUBSCRIPTION':'Y',
'NAME':'Additional information from users card',
'DESCRIPTION':'Aktivity gets information from the user cards',
'PROPERTIES':{
'User':{
'Name':'User',
'Type':'user',
'Required':'Y',
'Multiple':'N',
},
},
'RETURN_PROPERTIES':{
'PERSONAL_MOBILE':{
'Name':'Users_PERSONAL_MOBILE',
'Type':'string',
'Required':'N',
'Multiple':'N',
},
'WORK_POSITION':{
'Name':'Users_WORK_POSITION',
'Type':'string',
'Required':'N',
'Multiple':'N',
},
'UF_SKYPE':{
'Name':'Users_UF_SKYPE',
'Type':'string',
'Required':'N',
'Multiple':'N',
},
}
}
BX24.callMethod(
'bizproc.activity.add',
params,
function(result)
{
if (result.error())
alertify.alert('Error: '+result.error());
else
alertify.alert('Installation successfully completed');
}
);
}
 
function uninstallActivity1(){
var params={
'CODE':'user_additional_info'
}
BX24.callMethod(
'bizproc.activity.delete',
params,
function (result)
{
if (result.error())
alertify.alert('Error: '+result.error());
else
alertify.alert('Uninstallathion successfully completed');
}
);
}

7) Logging of requests and responses on javascript

BX.ajax.get('/b24/ajax/js_request_log.php', {
params
});
Logger:
<?php
require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php');
define('LOG_FILENAME', $_SERVER['DOCUMENT_ROOT'].'/ajax/js_error.txt');
$request = \Bitrix\Main\Context::getCurrent()->getRequest();
if ($request->get('data')) {
AddMessage2Log(print_r($request->get('data'), true));
}
?>

8) Return answer to business process

<?php
callB24Method($auth,'bizproc.event.send',array(
"EVENT_TOKEN"=>$event_token,
"RETURN_VALUES"=>array(
'PERSONAL_MOBILE'=>$user_info[0]['PERSONAL_MOBILE'],
'WORK_POSITION'=>$user_info[0]['WORK_POSITION'],
'UF_SKYPE'=>$user_info[0]['UF_SKYPE'],
),
"LOG_MESSAGE"=>'OK'
));
?>

Comments

  1. Hi Yuliya,

    Thank you for the tips, helped me a lot.

    Marcelo

    ReplyDelete

Post a Comment

Popular posts from this blog

How to add a custom activity type in Bitrix24 CRM

During one of our recent self-hosted Bitrix24 implementations, we had the task of adding a new type of Activity to CRM - something between a Call Activity and a Visit Activity. The customer wanted this new Activity to be able to participate in CRM reports and filters. To begin with, it was necessary to add a link to a new Activity type in the CRM entity card. We found out that the crm.timeline component is responsible for the output of this part of the interface. In particular, its template.php contains a list of displayed links to case types. For example, for a call, something like: Accordingly, as we can see, this is a regular link with the data parameter item-item-id. So we can add ours, with our own unique data-item-id. As you can see, the href for the link is set as "#" - clicking on this link is processed by the JS code from the script.js of this component. Indeed, the BX.CrmTimelineMenuBar function is responsible for this.processItemSelection()....

How to properly develop your own REST methods for on-premise Bitrix24

During one of our recent implementations, CRM Bitrix24 was used as a backend for another external system - for a student's account. Through an account interface, students can apply for training in the chosen course, upload the necessary documents, monitor the status of their application, pay tuition bills, and also interact with their curators. All this is very deeply integrated with Bitrix24 business processes. For such a deep integration of Bitrix with a third-party system, we lacked standard REST methods - the logic for using requests became too complicated and confusing.