This is an old revision of the document!


Custom functions

Important

This Page describe a feature, which require PHP development skills.
If you use this feature in a wrong way, you could crash the Workflow!

Lots of Workflow Tasks in the Designer could use custom functions.

You recognize the option to use this functions, if you have an green border around a textfield and a special icon () next to the textbox.

Almost all such fields have special hints about this function. (for example, what value you have to return)

These scripts are default PHP Scripts with limited functions. All whitelisted PHP functions have the same arguments, like the original one.
Each custom function have to return a value with the “return”statement.

You could use the following PHP functions:

  • md5, rand, implode, substr, explode, microtime, date, time, sha1, hash, intval, floatval, floor, ceil, foreach
  • all functions, started with “str” (strpos, str_pad, str_replace, …)

Additional functions

I have added some special PHP functions you could use to do special tasks.
Also you could create your own functions. Read here

This function will return the complete download link for a Record inside Documents and could for example be used in eMails.

  • $crmid - mixed (integer/string) The CRM-ID of the Document Record (current Record is $crmid)

example:

return "<a href='".wf_documentlink($crmid)."'>Download File</a>";

wf_get_entity($crmid)

Returns an array with all values from the record you specify with $crmid.

  • $crmid - mixed (integer/string) Set the CRM-ID to load

example:

$entity = wf_get_entity($assigned_user_id);
return $entity["email1"];

This will return the primary email address of the assigned to User. (I know this could be done easier, but you could get also the email from the reports_to User within the assigned_to User in this way.)

wf_date($db_date, $interval, $format)

This function makes date calculation easier. If you only wants to format a given date, please use the default php date function.

example:

return wf_date("2001-12-31", "+1 day", "d.m.Y"); # returns 01.01.2002



wf_dbquery($sql)

Returns an array with all columns from first row of the sql query.

  • $sql - string Which SQL Query should be executed

example:

$record = wf_dbquery("SELECT setype, crmid FROM vtiger_crmentity ORDER BY createdtime DESC LIMIT 1"); ;
return $record["crmid"];

Get the CRMID of the latest record in your vtiger system.

wf_dbSelectAll($sql)

returns an array with all result rows with all columns, this query will return

  • $sql - string Which SQL Query should be executed

example:

$record = wf_dbSelectAll("SELECT setype, crmid FROM vtiger_crmentity ORDER BY createdtime DESC LIMIT 10"); ;
/*
$record will be
array(
  array( "setype" => "Leads", "crmid" => 1),
  array( "setype" => "Contacts", "crmid" => 2 )
 )
*/

Get the CRMID of the latest record in your vtiger system.

custom function Examples

This example is only written to demonstrate the functions and could be done better.

$var1 = "864"."0";
$var1 = $var1.(0 + intval($vtiger_purchaseorder)).intval($vtiger_purchaseorder);
$var2 = 1 + (2 * 5) - 8;
$var1 = substr($var1, 0, 5);
if($vtiger_purchaseorder == "1") {
   $add = intval($var1) * $var2;
   return time() + $add;
} else {
   $add = intval($var1) * intval($vtiger_purchaseorder);
   return time() + $add;
}

These function could be used inside the delay task to wait one day, or the amount of days in the Purchase Order Field of an invoice.

Here you could see one limitation of my implementation:

If you want use mathematical operations, you have to use parentheses to become the correct result, because the system ignore basic mathematical rules.

Other examples

Wait until same day next month

return date("Y-m-d", strtotime("+1 month"));

Wait 5 days

$days = 5;
$date = strtotime($datefield);
return date("Y-m-d", $date + (86400 * $days))