Bean missing a field_def

New to SuiteCRM I am investigating an issue where a Report condition date does not get formatted correctly.

I have got to the function below, called from function save_lines in \modules\AOR_Conditions\AOR_Condition.php where the date is sent and should be returned in the correct format.

\modules\AOW_WorkFlow\aow_utils.php

function fixUpFormatting($module, $field, $value)
{
    global $timedate, $beanFiles, $beanList;

    require_once($beanFiles[$beanList[$module]]);
    $bean = new $beanList[$module];

    static $boolean_false_values = array('off', 'false', '0', 'no');
    switch ($bean->field_defs[$field]['type']) { // HERE IS THE ERROR

The $field being referenced is one called order_date and it is not found in the $bean->field_defs causing the error.

What I don’t understand is how the $bean is created and why the relevant field_def is not added. Somewhere in the BeanFactory?

Any pointers greatly appreciated.

Does that field exist? I can’t find any reference to order_date anywhere…

Is it a custom field? In a custom module?

And other custom fields in that module work ok?

Yes it is a custom module and a custom field. The Report I created was one field to show order_date, and one condition based on order_date. The field shows all the order_dates but the condition fails because of the error above.

Drilling down into it I got to where I did above, but I can’t see where the $bean gets populated from.

I see that line used regularly in the software but can’t see where it actually populates the bean.

Check the call stack! Are you using an IDE such as PhpStorm or Eclipse or another?

I use VS Code with xdebug. I have spent along time tracing this issue and I have come to the conclusion that this is a bug, but related to my original question.

The issue lies with the line below in function fixUpFormatting($module, $field, $value)

$bean->field_defs[$field]

It seems that this will always fail when a report condition is based on a field related to the Report Module the report is based on. For example:

Here the report is based on the module Contacts and the condition is Contacts:Tasks - Due Date. Because Task’s Due Date will not feature in the $bean->field_defs array it will fail and a correctly formatted date is not returned.

The result on Save or Update:

image

The date entered in the condition is not saved and the condition fails.

Note that choosing Date Created or Date Modified in the related module will bypass this error and looks like it is working due to the fact these field_defs do appear in the field_def array. However these defs are actually from Contacts, not Tasks.

More testing to do and working on a fix.

This seems to fix the issue. It checks to see if $field is in $bean->field_defs and if not it will check to see if it is in a date format. If it is, it will continue on to _convert the date.

You can also remove the subsequent preg_matches as it has already been checked for a date format:

function fixUpFormatting($module, $field, $value)
{
    global $timedate, $beanFiles, $beanList;
    include

    require_once($beanFiles[$beanList[$module]]);
    $bean = new $beanList[$module];

    static $boolean_false_values = array('off', 'false', '0', 'no');

    $type="";
    if ($bean->field_defs[$field]) {
        // $field exists in $bean->field_defs. Probably because in root report module
        $type=$bean->field_defs[$field]['type'];
    }else{
        // $field does not exists in $bean->field_defs it will have come from a date in a related module
        // Check to see if it has a date format

        $date_part = substr($value, 0, 10);

        // Allowing for different date separators
        $date_part = str_replace(".", "/", $date_part);
        $date_part = str_replace("-", "/", $date_part);

        $is_date = false;

        $date_formats= array (
            '^([0-9]{4})/[0-9]{2}/[0-9]{2}', // 'Y/m/d'
            '^([0-9]{2})/[0-9]{2}/[0-9]{4}'  // 'd/m/Y' or m/d/Y
        );

        foreach ($date_formats as $regexp) {
            if (preg_match ('|' . $regexp . '|', $date_part)) {
                $is_date = true;
                break;
            }
        }

        if($is_date) {
            $type='datetimecombo';
        }
        else {
            $type="unknown"; // Other kind of error
        }
    }

    switch ($type) {
    case 'datetime': ......
1 Like