The general idea is that the calculated field calls a custom function within a custom PHP file. The function in question in turn returns a value/data that matches the data type of the field.
So, for a situation like this, we would typically want to return an integer value, to be displayed in a field that is also of type integer.
Your function would look something like this:
PHP Code:
function calculatedValue($focus, $field, $value, $view)
{
$temp = 10;
$temp2 = 2;
$result = $temp / $temp2;
return $result;
}
This is a very simple example, but the important part is the return portion as it gives us the result of 10 / 2, which in turn is passed on to the calculated field. Now to extend that to use SQL queries, we simply add our logic for that before the return and leverage the already existing Sugar connection, as such:
PHP Code:
function caculatedValue($focus, $field, $value, $view)
{
//Calculate record count
$query = "SELECT COUNT(*) as the_count FROM notes ";
$results = $focus->db->query($query, true);
$row = $focus->db->fetchByAssoc($results);
$total = $row['the_count'];
return $total;
}
Bookmarks