Invoke Custom Function from Workflow Task

This article will explain how to create a new custom function in workflow task. Follow steps below:

1. Prepare registration script (.php)

e.g.: vtiger_root/registerWFFunction.php

In order to register the function, this register file needs to be placed in your vtiger root directory, and then you manually need to call it from your browser: http://www.mydomain.com/vtiger/registerWFFunction.php

Warning! You have to call this register file once. Everytime you call this register file, it will add the function to the vtiger_workflowtasks_entitymethod table in the database, so if you call it more than once then it will get added to the database more than once.

In this example we register handleSetEstimatedDate() custom function defined in include/EstimatedDateHandler.php file for Invoice module.

<?php 
require_once("modules/com_vtiger_workflow/include.inc");
require_once("modules/com_vtiger_workflow/tasks/VTEntityMethodTask.inc");
require_once("modules/com_vtiger_workflow/VTEntityMethodManager.inc");
$adb = PearDatabase::getInstance();
$emm = new VTEntityMethodManager($adb);
    	$result = $adb->pquery("SELECT function_name FROM com_vtiger_workflowtasks_entitymethod WHERE module_name=? AND method_name=?", array('Invoice', 'SetEstimatedDate'));
    	if($adb->num_rows($result)==0){
$emm->addEntityMethod("Invoice","SetEstimatedDate","include/EstimatedDateHandler.php","handleSetEstimatedDate");
}

2. Create Handler file

In another file we need define custom function and write content of this function.
e.g.: include/EstimatedDateHandler.php

<?php
function handleSetEstimatedDate($entityData) {
  /* CONTENT OF CUSTOM FUNCTION ... */
}

3. Invoke Custom function with module

Last step is create and run via browser this file.
e.g.:vtiger_root/invokeCustomFunction.php

$adb = PearDatabase::getInstance();
       $forModule = 'Invoice';
       $summary = 'set Estimate Date '.$forModule;
       $result = $adb->pquery("SELECT workflow_id FROM com_vtiger_workflows WHERE summary=? AND module_name=?", array($summary, $forModule));
   	if($adb->num_rows($result)==0){
           echo 'Create Workflow for '.$forModule.'!<br />';
           
           $wm = new VTWorkflowManager($adb);
           $wf = Settings_Workflows_Record_Model::getCleanInstance($moduleName);
   		$wf->description = $summary;
   		$wf->test = Zend_Json::encode(array());
   		$wf->moduleName = $forModule;
   		$wf->executionCondition = 3;
   		$wf->filtersavedinnew = 6;
   		$wm->save($wf);
           $adb->pquery('UPDATE com_vtiger_workflows SET type=? WHERE workflow_id=?',array('basic',$wf->id));
           $wfId = $wf->id;
       } else {
           $wfId = $adb->query_result($result,0,'workflow_id');
       }
       $tResult = $adb->pquery("SELECT task_id FROM com_vtiger_workflowtasks WHERE workflow_id=?", array($wfId));
   	if($adb->num_rows($tResult)==0){
           $tm = new VTTaskManager($adb);
           echo 'Create Workflow Task for '.$forModule.'!<br />';
           $task = $tm->createTask('VTEntityMethodTask', $wfId);
           $task->active = true;
           $task->summary = "Estimated Date Handler";
           $task->methodName = "SetEstimatedDate";
           $tm->saveTask($task); 
       }

 

Share this post

Comments (2)

  • Stefano Reply

    Hi, i need to create a custom function that create a new Project Task when a Service Contract status is completed.
    Can you help me?.
    Thanks

    January 14, 2020 at 8:55 am
    • info@its4you.sk Reply

      Hello, thank you for contacting us. This is more like customization needed for you. Please contact us at info@its4you.sk with your request. We will reply you with more details. Thanks

      January 14, 2020 at 9:19 am

Leave a Reply

Your email address will not be published. Required fields are marked *