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' )); ?>
Hi Yuliya,
ReplyDeleteThank you for the tips, helped me a lot.
Marcelo