Fill a custom flield with a logic hook (contract + project number), but bean-->retrieve fails

Hi,
I added to project a new text-field “project_number_c”.
In contract I added a relationship-field “projekt_c”, where I select the project. After the selection, I want to display the project-number too, so I added a text-field “projektnummer_c” to contract.

I added to the folder “\custom\modules\AOS_Contracts” two files: logic_hooks.php

<?php
// Do not store anything in this file that is not part of the array or the hook version.  This file will	
// be automatically rebuilt in the future. 
//$hook_version = 1; 
$hook_array['before_save'][] = Array(
        //Processing index. For sorting the array.
        1,
       
        //Label. A string value to identify the hook.
        'before_save example',
       
        //The PHP file where your class is located.
        'custom/modules/AOS_Contracts/before_save_class.php',
       
        //The class the method is in.
        'before_save_class',
       
        //The method to call.
        'before_save_method'
    );


?>

and before_save_class.php

<?php

//if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class before_save_class
    {
        function before_save_method($bean, $event, $arguments)
        {
            //lets assume the account field in deliveries is account_id_c
           //We will use beans to fetch the related account's details
           $project = new Project();
           $project->retrieve($bean->project);
           //Set the values from accounts into deliveries
           // $bean is your current record being saved. 
           // $account is the related account.
           $bean->projektnummer_c = $project->project_number_c;
           //Same stuff in order to set other values

        }
    }

//}
?>

But I think the retrieve-line fails, because just the default-value of the project number is displayed and not the number of the selected project.
What is wrong?

Regards, MT

Can you see what’s in $bean->project ?

Use a debugger, or write it out to the suitecrm.log

Maybe it’s text, instead of the id.

In that case, you might try something like this

$project->retrieve_by_string_fields(array('name' => $bean->project ));

I didn’t check field names or anything, I am just writing this off the top of my head, but I hope it helps you find the way

Thank you for your fast response!

I tried it and changed it to:


$project = new Project();
$project->retrieve($bean->projekt_c);
$GLOBALS['log']->fatal($bean->projekt_c);

Now I can see the correct “name” of the project in the log :slight_smile:

But if I look inside $project with


 $GLOBALS['log']->fatal($bean->project);

nothing is in the log-file.

If I look inside project_number_c


$GLOBALS['log']->fatal($project->project_number_c);

the default project-number is shows, but not the number of the project.

I hope you can tell me why :wink:

Edit:

 $project->retrieve_by_string_fields(array('name' => $bean->project ));

Result is also the default value of the project number.

It’s better to print to the logs with print_r or vardump, so you get type information and so that arrays show correctly. So, try:

$GLOBALS['log']->fatal(print_r($bean->project));

You can even print out the whole $bean

Another thing - try this in an after_save hook instead.

1 Like
 $GLOBALS['log']->fatal($bean->projekt_c);

Returns the name of the project

$GLOBALS['log']->fatal(print_r($bean->projekt_c));

Returns just “1”

If I chance to after_save by “$hook_array[‘after_save’][] …”, then project number is completly empty.

Dont wonder why I´m writing projekt_c instead of project_c, it´s just because of (partially) used german language.

Solved! This works:


<?php

//if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class before_save_class
    {
        function before_save_method($bean, $event, $arguments)
        {
            //lets assume the account field in deliveries is account_id_c
           //We will use beans to fetch the related account's details
           $project = new Project();
	   $project->retrieve_by_string_fields(array('name' => $bean->projekt_c ));

           //Set the values from accounts into deliveries
           // $bean is your current record being saved. 
           // $account is the related account.
           $bean->projektnummer_c = $project->project_number_c;
           //Same stuff in order to set other values

        }
    }

//}
?>

Thank you very much!