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
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); }
Comments (2)
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
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