Mapping TextField between two modules

I want to add a “Referred By” field in Accounts module, and map it with “Referred By” in Leads module.

I tried two scenarios one using vardefs and the webhooks
1- for the vardefs the only type worked is relate but in editview it brings error as the mapped value is not a relate value.

$dictionary['Account']['fields']['referred_by_c'] = array(
    'name' => 'referred_by_c',
    'type' => 'relate',
    'source' => 'non-db',
    'vname' => 'LBL_REFERRED_BY',
    'rname' => 'refered_by',
    'id_name' => 'parent_id',
    'module' => 'Leads',
    'link' => 'leads',
    'table' => 'leads',
    'isnull' => 'true',
    'massupdate' => false,
    'duplicate_merge' => 'disabled',
    'audited' => false,
    'reportable' => true,
    'unified_search' => false,
    'importable' => 'true',
);

2- webhook in leads but it do not work

<?php
class MapReferredToHook {
    public function mapField($bean, $event, $arguments) {
        // Check if the Lead is converted and associated with an Account
        if ($bean->converted && !empty($bean->parent_id)) {
            // Load the related Account record
            $account = BeanFactory::getBean('Accounts', $bean->parent_id);

            if ($account) {
                // Update the Hook field in the Account
                $account->referred_by_c = $bean->refered_by; // Ensure 'refered_by' matches the actual field name
                $account->save();
            }
        }
    }
}

What are you trying to do?

If you mean when a lead converts to an account the data gets copied over, all you have to do is ensure the field name and type are the same in both modules. The conversion process takes care of this. It will populate it once on conversion.

If you mean you ALWAYS want to ensure the lead field matches Account field, then you need to write a scheduled job to compare both fields and make them equal as a scheduled job.

The other option is to create a non-db field in the Account and have it populated by function. ie: equal to the related lead field.

I’m thinking you want #1, that’s the easiest.

The referred by field is a custom field in accounts module but a system field in leads module.
The data is not copied when converting a lead.

I think there should be an after save logic hook entry in the file at custom/modules/Leads/logic_hook.php for the logic hook.

Please refer

As mentioned I’d create a field in Account EXACTLY the same as the one in leads and it will take care of itself. This is the easiest.

OR…

You can do a logic hook for new Accounts to populate the field from the related lead.

You may also be able to do this with workflow.

The other option is to equate the fields in the convertdefs.php. This is how I normally match fields.

By far the easiest is option #1. you might run into the fact that the field is reffereredby (or whatever it is in the lead) and it’s referedby_c in the Account. Vardefs you can edit the _c field and just remove it and re-name it. (you’ll have to re-populate it after, so have a download of all the records.) Search this forum for changing field name. There are a few detailed posts on how to do.

I created the fields manually out of the studio to be exactly the same as their corresponding fields in Leads module. It worked!
Thanks for your help.

If that worked for you, please mark it as a solution so others know it’s solved and how it was solved.

The solution was to manually create the field by:
1- creating the file:
/custom/Extension/modules/Accounts/Ext/Vardefs/_override_sugarfield_refered_by.php

2- Then adding the same definition in the “refered_by” file in “Leads” module, and it was as follows:

<?php
 // created: 2025-01-05 10:27:14
$dictionary['Account']['fields']['refered_by']['help']='Identifies who refered the lead';
$dictionary['Account']['fields']['refered_by']['comments']='Identifies who refered the lead';
$dictionary['Account']['fields']['refered_by'] = array(
     'name' => 'refered_by',
     'vname' => 'LBL_REFERED_BY',
     'type' => 'varchar',
     'len' => '100',
     'merge_filter' => 'enabled',
);

 ?>

3- Updated the language file in:
custom/Extension/modules/Accounts/Ext/Language/_override_en_us.lang

to add:

$mod_strings['LBL_REFERED_BY'] = 'Referred By:';

4- Lastly, performed “Quick Repair and Rebuild”, and made sure that its column was created in the database.

2 Likes