Custom field from accounts to contacts

Hi everyone,

A little help required. On my “accounts” module I have a custom field named “borough”. I wish to be able to create email “target lists” for “contacts” associated with “accounts” based on the “borough” field.

This means when I search for new contacts to add to my target list the “borough” field will appear in the contact search.

I have tried some methods mentioned in the forum but they do not seem to work with custom fields.

I am new to this forum so if I have posted in the wrong area I apologise.

Many thanks for any help!

Darrel

I have added borough as a relate field “borough_c”. When this shows up it populates with the account name and not the borough. As in the image enclosed on my post all I m looking for is to be able use the borough in the contacts search. I am sure this must be easy and I am missing a trick.

Once again thanks for any help.

So if I am getting this correct. What you require is, lets say there is an account with borough_c = X (borough_c is a simple text field)
Every contact related to this account will also have borough_c = X
When you add contacts to target lists, you want to use X to filter contacts. Am I right?

In this case, your approach needs to be altered slightly. Contacts are already “related” to Accounts so you do not need another relate field, just a simple text field.
What you need to do is, whenever you are saving a Contact, use a logic hook to fetch borough_c from Accounts and put the same value in borough_c of Contacts.

Wrote a small sample code over here https://suitecrm.com/forum/suitecrm-7-0-discussion/12823-workflow-action-to-populate-field#43249
Take a look.

Thanks so much Arsalan, I will try your code over the weekend and let you know how I get on.

Many thanks

Darrel

OK so I have implemented the following.

added the following to the logic_hooks.php file:

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

Created the before_save_class.php file and added -

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->borough_c);
       //Set the values from accounts into deliveries
       // $bean is your current record being saved. 
       // $account is the related account.
       $bean->borough_c = $account->borough_c;
       //Same stuff in order to set other values

    }
}

Now when I save a contact I get the following:

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->borough_c); //Set the values from accounts into deliveries // $bean is your current record being saved. // $account is the related account. $bean->borough_c = $account->borough_c; //Same stuff in order to set other values } }

I have obviously done something incorrectly. Many thanks once again for your help.

I am guessing you forgot to add the PHP tags <?php and ?> in your before_save_class.php file
Also $account->retrieve($bean->borough_c), this needs to be the ID field of the account which is related to contact. If you are using the default field it will be
$account->retrieve($bean->account_id)

I am pretty sure borough_c is not the field which contains the account’s ID. Its just your custom field.

What a muppet! just realised about the tags! thanks

Thanks so much, this works perfectly. Will I have to re-save all my contacts indvidualy or is there a quick way? This obviously only updates when they have been saved again. I have several thousand contacts so it would obviously take a while.Thanks very much.

Logic Hooks work even when you import contacts. So exporting all your contacts and importing/updating them again seems to be a quick option.
Don’t forget to backup your database before importing.

1 Like

@darrelallam

Darrell,
I think that your method improves your situation but it is a little buggy:

  1. What happens when you update an account’s burrough?
    Possible solution: implement a logic hook for the Accounts module that updates the field also on all the account’s contacts

  2. What happens when you import an account or a contact?
    With respect to a contact Arsalan has provided an answer, but not for the import of an account.

  3. What happens when either account or contact are created in other ways (eg: convert lead, workflow, scheduler, direct database modification, etc…)
    Solution: you have to think of all the possible events and have a solution for each

Question:
Isn’t the functionality that let’s you add to a target list the contacts from account found in a search run form the accounts module, sufficient (see picture)?

If you use this functionality, you can search all your accounts in a given Burrough and then add all the contatcs related to these accounts to a target list.

Additionally you could run a search within your contacts and create one or more other target lists (eg: a suppression list) and use them accordingly

Other possible solutions:
Reports: you could create a report that finds all the contacts according to your criteria (including their account’s burrough) and then add the result to a target list: in this way you would avoid duplication and redundancy of data (which leads to the errors I highlighted above)

Workflow: although I am not 100% sure, I think that you could create a workflow that does what you want to achieve.

Custom scheduler: you could create a custom scheduler that does it too

Note that both the scheduler and the workflow solution will have moments in which the records are not up-to-date since both schedulers and workflow run at specific intervals and between those intervals data may be added, deleted or modified.

I would first try the add contacts to target list from the accounts module search (although this allows you to search only for accounts), and, as a second option, reports.

Hope this helps (and not confuses!)

2 Likes

Hi amariussi

Question:
Isn’t the functionality that let’s you add to a target list the contacts from account found in a search run form the accounts module, sufficient (see picture)?

The above feature I had completely missed and should be able to do exactly what I want. Many thanks Darrel

Hello,

I am trying to do the same thing, in my case I have 2 dropdown fields to populate from account to contact.
In account: account_type & industry
In contact: account_type_c & industry_c

But it doesn’t work, the fields are not populated. Any help!

I made it like this:
In custom\modules\Accounts\logic_hooks.php

$hook_array[‘before_save’][] = Array(1, ‘before_save_accounts’, ‘custom/modules/Contacts/before_save_class.php’, ‘before_save_class’, ‘before_save_method’);

In \custom\modules\Contacts\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 $account = new Account(); $account->retrieve($bean->account_id); //Set the values from accounts into deliveries // $bean is your current record being saved. // $account is the related account. $bean->industry_c = $account->industry; $bean->account_type_c = $account->account_type; //Same stuff in order to set other values } } ?>

Thanks for any help.