Workflow action to populate field

Hi!

I have a situation that I need a suggestion for:

  • I created new custom module for handling deliveries using module builder
  • this module is related to accounts: accounts --> deliveries | one-to-many, and I have the field Accounts in deliveries edit form
  • deliveries module has address fields on its own

What I would like to acchieve is to create a workflow that will populate address fields in deliveries module with address data from accounts module when deliveries record is saved (applied on new record and edited record save).

Is there a way this could be done with Workflow module?

I would highly appreciate any suggestion on this…

Thank you in advance!!!

Mario

This may not be possible directly from workflows module, but this is very well possible using logic hooks. Since a few people have been asking this question and still unable to do it, I’ll just post a sort of detailed guide over here.

Step 1: Define a before_save logic hook in custom/modules//logic_hooks.php


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/<module>/before_save_class.php',
       
        //The class the method is in.
        'before_save_class',
       
        //The method to call.
        'before_save_method'
    );

Step 2: Create a logic hook before_save_class.php in custom/modules//

 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
           $account = new Account();
           $account->retrieve($bean->account_id_c);
           //Set the values from accounts into deliveries
           // $bean is your current record being saved. 
           // $account is the related account.
           $bean->address_1 = $account->billing_address_street;
           $bean->address_2 = $account->billing_address_city;
           //Same stuff in order to set other values

        }
    }

The above code will copy the address fields from related account to the address fields in deliveries.
That’s pretty much it. Hope it helps!

Hi Arsalan,
I’ve been using your post in order to try and get my own logic hook working. I’ve created a custom module named Candidates. In this module I have two fields called name and candidate_no and I’ve created a relationship one to many with the tasks module. In the task module I’ve created two custom fields named candidate_name_c and candidate_no_c and customised the tasks dashlet to display these two new fields.

What I want to do is when creating a task using the sub panel in the Candidates Module is to have the candidate_name_c and candidate_no_c within tasks to be populated with the data from name and candidate_no from the Candidates Module. This is so on the dashlet users can see the candidate name and number the task relates to. What I’ve done is created a before_save logic hook in custom/modules/Tasks/logic_hooks.php. The file is as follows

<?php hook_array[] = Array(1, 'before_save_candidates', 'custom/modules/candidates/before_save_class.php', before_save_class', before_save_method'); ?>

I’ve then created a logic hook before_save_class.php in custom/modules/candidates/. The file is as follows

<?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class before_save_class { function before_save_method($bean, $event, $arguments) { //canidate field in Tasks is candidate_tasks //We will use beans to fetch the related candidate details $candidate = new Task(); $candidate->retrieve($bean->candidate_tasks); //Set the values from Candidates into Tasks // $bean is your current record being saved. // $candidate is the related candidate. $bean->candidate_name_c = $candidate->name; $bean->candidate_no_c = $candidate->candidate_no; //Same stuff in order to set other values } } ?>

After running a repair and trying to create a task and clicking save nothing happens. No task is created and no error shown. Checking the event logs I see the following entries

[Fri Sep 08 13:54:54.988730 2017] [:error] [pid 2628:tid 1968] [client 172.16.102.26:58713] PHP Fatal error: Cannot use [] for reading in C:\xampp\htdocs\suitecrm\custom\modules\Tasks\logic_hooks.php on line 2, referer: http://idweb01/suitecrm/index.php?action=ajaxui
[Fri Sep 08 13:57:32.180624 2017] [:error] [pid 2628:tid 1940] [client 172.16.102.26:58771] PHP Fatal error: Cannot use [] for reading in C:\xampp\htdocs\suitecrm\custom\modules\Tasks\logic_hooks.php on line 2, referer: http://idweb01/suitecrm/index.php?action=ajaxui
[Fri Sep 08 14:06:47.754984 2017] [:error] [pid 2628:tid 1960] [client 172.16.102.26:58926] PHP Fatal error: Cannot use [] for reading in C:\xampp\htdocs\suitecrm\custom\modules\Tasks\logic_hooks.php on line 2, referer: http://idweb01/suitecrm/index.php?action=ajaxui

Not sure what I’ve done wrong but any help would be appreciated

I haven’t read your complete code but the below code is not proper

<?php

hook_array[] = Array(1, 'before_save_candidates', 'custom/modules/candidates/before_save_class.php', before_save_class', before_save_method');

?>

It should be

<?php

hook_array['before_save'][] = Array(1, 'before_save_candidates', 'custom/modules/candidates/before_save_class.php', before_save_class', before_save_method');

?>

Notice the ‘before_save’ after hook_array

Hi Arsalan,
I’ve resubmitted my code as requested.
My logic_hook.php file is as follows


<?php

hook_array['before_save'][] = Array(1, 'before_save_candidates', 'custom/modules/candidates/before_save_class.php', before_save_class', before_save_method');

?>

My before_save_class.php is as follows


<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class before_save_class
{
function before_save_method($bean, $event, $arguments)
{
//canidate field in Tasks is candidate_tasks
//We will use beans to fetch the related candidate details
$candidate = new Task();
$candidate->retrieve($bean->candidate_tasks);
//Set the values from Candidates into Tasks
// $bean is your current record being saved.
// $candidate is the related candidate.
$bean->candidate_name_c = $candidate->name;
$bean->candidate_no_c = $candidate->candidate_no;
//Same stuff in order to set other values

}
}
?>

[strike]Please take a look at $hook_array in logic_hook.php. You are missing the first index which is

$hook_array['before_save'][]

[/strike]

Edit: Oh, I see you have edited the code. Let me review and get back to you.

Hi Arsalan,
I see the error now. Missing the $ when calling my initial $hook_array
I’ll retest and hopefully this will solve my issue.

Thanks for the assist.

Hi Arsalan,
I’ve retested and I’m still unable to create a new task.
The error that is logged is as follows

[Mon Sep 11 08:31:13.906469 2017] [:error] [pid 2628:tid 1948] [client 172.16.102.26:51949] PHP Catchable fatal error: Object of class Link2 could not be converted to string in C:\xampp\htdocs\suitecrm\data\SugarBean.php on line 3845, referer: http://idweb01/suitecrm/index.php?action=ajaxui

Not sure where I’m going wrong here or whether I’m referencing the wrong tables.
I’ve attached images of my setup along with my code.

\custom\modules\CAN_Candidate\before_save_class.php contains the following code


<?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 candidate field in tasks is candidate_tasks
           //We will use beans to fetch the related candidate's details
           $candidate = new Task();
           $candidate->retrieve($bean->can_candidate_tasks_c);
           //Set the values from candidates into tasks
           // $bean is your current record being saved. 
           // $candidate is the related candidate.
           $bean->candidate_name_c = $candidate->name;
           $bean->candidate_no_c = $candidate->candidate_no;
           //Same stuff in order to set other values

        }
    }
?>

\custom\modules\Tasks\logic_hooks.php file contains the following code


<?php
$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_candidate',
       
        //The PHP file where your class is located.
        'custom/modules/CAN_Candidate/before_save_class.php',
       
        //The class the method is in.
        'before_save_class',
       
        //The method to call.
        'before_save_method'
    );
?>

You can see on the attached Capture1.png the tasks subpanel should has two custom fields that should be filled with Candidate Name and Candidate No from the Canidate module but this is not happening.

Let me know if there’s anything else I can provide to help with fixing the issue.

reopen this topic, I’m trying to solve the problem for 3 days.
I want in listview lead to bring up the start date of events

date_start is field in events date start

custom/module/leads/logic_hooks.php

	
	
<?php

hook_array['before_save'][] = Array(1, 'before_save_leads', 'custom/modules/leads/my.php', before_save_class', before_save_method');

?>	

custom/module/leads/my.php


 class astocazzo_c
    {
        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
           $events = new Events();
           $events->retrieve($bean->date_start);
           //Set the values from accounts into deliveries
           // $bean is your current record being saved. 
           // $account is the related leads.
           $bean->date_start = $leads->astocazzo_c;
                      //Same stuff in order to set other values

        }
    }

I know this is an old topic, but just seeing it now in my feed. I’ve done exactly this and an after save hook is not the best way to do it. You can have it populate live on screen when the shipping account is selected.

EXAMPLE:

Created a module: trans_Shipments

Created a Relate Field: ship_to which relates to accounts.

in /custom/Extenstion/modules/trans_Shipments/Ext/Vardefs/sugarfield_ship_to.php

<?php
$dictionary['trans_Shipments']['fields']['ship_to']['populate_list'] = array('name', 'id', 'shipping_address_street');
$dictionary['trans_Shipments']['fields']['ship_to']['field_list'] = array('account_name', 'account_id_c', 'shipping_address_street');

This will populate the address when the “ship to” is selected. The ship to is an account.