Documentation

Workflow

Methods to handle workflows

Method Description
bizproc.workflow.instance.list Returns list of launched workflows. Alias of bizproc.workflow.instances.
bizproc.workflow.instances Returns list of launched workflows.
bizproc.workflow.terminate Stops an active workflow.
bizproc.workflow.start Launches a workflow.
bizproc.workflow.kill Deletes a launched workflow.

Example

Example of REST-application to handle Workflow launch/stop:

     1 <?php
     2 header('Content-Type: text/html; charset=UTF-8');
     3 ?>
     4 <!DOCTYPE html>
     5 <html lang="en">
     6 <head>
     7 	<meta charset="UTF-8">
     8 	<title></title>
     9 	<style type="text/css">
    10 		.wf-list {
    11 			list-style-type: none;
    12 		}
    13 		.wf-list li {
    14 			margin: 25px 0;
    15 		}
    16 		.wf-title {
    17 			font-size: 16px;
    18 			font-weight: bold;
    19 			color: grey;
    20 		}
    21 		.wf-complete {
    22 			padding: 5px;
    23 			border: 1px dotted;
    24 		}
    25 		.wf-complete .wf-title
    26 		{
    27 			color: green;
    28 			margin-bottom: 10px;
    29 			display: block;
    30 		}
    31 		.wf-complete textarea
    32 		{
    33 			margin-bottom: 10px;
    34 			display: block;
    35 		}
    36 		#user-name
    37 		{
    38 			font-weight: bold;
    39 			color: green;
    40 		}
    41 	</style>
    42 </head>
    43 <body style="display: none">
    44 <script src="//api.bitrix24.com/api/v1/"></script>
    45 <h1>Hello, <span id="user-name"></span></h1>
    46 <h2>This is example of Bizproc workflows API usage</h2>
    47 
    48 <button onclick="selectCRMEntity();">Select LEAD
    49 <span id="selected-entity" data-entity-id=""></span>
    50 <br>Select template:
    51 <select id="tpl-id"><option value="">-- choose --</option></select>
    52 <button onclick="startWf(document.getElementById('selected-entity').getAttribute('data-entity-id'), document.getElementById('tpl-id').value, getList);">Start BP</button>
    53 <hr>
    54 <button onclick="getList();">Get workflow instances list</button>
    55 <ul id="inst-list" class="inst-list"></ul>
    56 
    57 <script type="text/javascript">
    58 	var currentUser = null;
    59 	BX24.init(function()
    60 	{
    61 		BX24.callMethod('user.current', {}, function(res){
    62 			currentUser = res.data();
    63 			document.body.style.display = '';
    64 			document.getElementById('user-name').textContent = currentUser['NAME'] + ' ' + currentUser['LAST_NAME']
    65 		});
    66 
    67 		getTemplates();
    68 	});
    69 
    70 	function getList()
    71 	{
    72 		var listNode = document.getElementById('inst-list');
    73 
    74 		listNode.innerHTML = '';
    75 
    76 		BX24.callMethod(
    77 			'bizproc.workflow.instance.list',
    78 			{
    79 				select: [
    80 					'ID',
    81 					'STARTED',
    82 					'STARTED_BY',
    83 					'TEMPLATE_ID',
    84 					'DOCUMENT_ID'
    85 				],
    86 				order: {MODIFIED: 'DESC'},
    87 				filter: {MODULE_ID: 'crm', ENTITY: 'CCrmDocumentLead'}
    88 			},
    89 			function(result)
    90 			{
    91 				if(result.error())
    92 					alert("Error: " + result.error());
    93 				else
    94 				{
    95 					var wfs = result.data();
    96 
    97 					wfs.forEach(function(wf)
    98 					{
    99 						renderWfi(wf, listNode);
   100 					});
   101 				}
   102 			}
   103 		);
   104 	}
   105 
   106 	function getTemplates()
   107 	{
   108 		var selectNode = document.getElementById('tpl-id');
   109 
   110 		BX24.callMethod(
   111 			'bizproc.workflow.template.list',
   112 			{
   113 				select: [
   114 					'ID',
   115 					// 'MODULE_ID',
   116 					// 'ENTITY',
   117 					// 'DOCUMENT_TYPE',
   118 					// 'AUTO_EXECUTE',
   119 					'NAME',
   120 					// 'TEMPLATE',
   121 					// 'PARAMETERS',
   122 					// 'VARIABLES',
   123 					// 'CONSTANTS',
   124 					'MODIFIED',
   125 					'IS_MODIFIED',
   126 					'USER_ID',
   127 					'SYSTEM_CODE'
   128 				],
   129 				filter: {MODULE_ID: 'crm', ENTITY: 'CCrmDocumentLead'}
   130 			},
   131 			function(result)
   132 			{
   133 				if(result.error())
   134 					alert("Error: " + result.error());
   135 				else
   136 				{
   137 					var templates = result.data();
   138 
   139 					console.log(templates);
   140 
   141 					templates.forEach(function(tpl)
   142 					{
   143 						var option = document.createElement('OPTION');
   144 						option.setAttribute('value', tpl['ID']);
   145 						option.textContent = tpl['NAME'];
   146 
   147 						selectNode.appendChild(option);
   148 
   149 					});
   150 				}
   151 			}
   152 		);
   153 	}
   154 
   155 	function renderWfi(wf, parentNode)
   156 	{
   157 		var li = document.createElement('LI');
   158 		var span = document.createElement('SPAN');
   159 		span.textContent = wf['ID'];
   160 		span.classList.add('wf-title');
   161 
   162 		li.appendChild(span);
   163 
   164 		li.classList.add('wf-complete');
   165 
   166 		var wfContainer = document.createElement('DIV');
   167 
   168 		var doTerminate = function()
   169 		{
   170 			terminateWf(wf['ID'], function()
   171 				{
   172 					parentNode.removeChild(li);
   173 				}
   174 			);
   175 		};
   176 
   177 		var terminateButton = document.createElement('BUTTON');
   178 		terminateButton.textContent = 'Terminate';
   179 		terminateButton.addEventListener('click', doTerminate.bind(window), false);
   180 		wfContainer.appendChild(terminateButton);
   181 
   182 		li.appendChild(wfContainer);
   183 
   184 		parentNode.appendChild(li);
   185 	}
   186 
   187 	function terminateWf(id, cb)
   188 	{
   189 		var params = {ID: id, STATUS: 'Terminated by rest app.'};
   190 
   191 		BX24.callMethod(
   192 			'bizproc.workflow.terminate',
   193 			params,
   194 			function(result)
   195 			{
   196 				if(result.error())
   197 					alert("Error: " + result.error());
   198 				else if (cb)
   199 					cb();
   200 			}
   201 		);
   202 	}
   203 
   204 	function startWf(leadId, tplId, cb)
   205 	{
   206 		if (!leadId)
   207 		{
   208 			alert('Lead not selected');
   209 			return;
   210 		}
   211 
   212 		var params = {
   213 			TEMPLATE_ID: tplId,
   214 			DOCUMENT_ID: ['crm', 'CCrmDocumentLead', leadId],
   215 			PARAMETERS: null
   216 		};
   217 
   218 		BX24.callMethod(
   219 			'bizproc.workflow.start',
   220 			params,
   221 			function(result)
   222 			{
   223 				if(result.error())
   224 					alert("Error: " + result.error());
   225 				else if (cb)
   226 					cb();
   227 			}
   228 		);
   229 	}
   230 
   231 
   232 	function selectCRMEntity()
   233 	{
   234 		document.getElementById('selected-entity').textContent = '';
   235 		BX24.selectCRM({
   236 			entityType: ['lead']
   237 		}, function(selected)
   238 		{
   239 			if (selected['lead'] && selected['lead'][0])
   240 			{
   241 				document.getElementById('selected-entity').textContent = selected['lead'][0]['title'];
   242 				var	id =  selected['lead'][0]['id'];
   243 
   244 				document.getElementById('selected-entity').setAttribute('data-entity-id', id.substring(2));
   245 			}
   246 		})
   247 	}
   248 
   249 </script>
   250 </body>
   251 </html>
© «Bitrix24», 2001-2024