AnsweredAssumed Answered

Overriding Teams

Question asked by community-support on Jan 13, 2014
Latest reply on Jan 13, 2014 by community-support
[B]Premise[/B]:

We have a custom field in our Users module business_unit_c

for users who have that field set to 'PG' we need to override teams when records are created.

The end result will be that Accounts, Contacts and Address Locations (a custom module) will be shared with others (Global), while all other records will be 'PG' only.

I set the PG primary team in before_save logic hooks and the Team Set in the after_save logic hooks as suggested here:

  http://support.sugarcrm.com/02_Docume...

[PHP]

$hook_array['before_save'][] = Array(1, 'setPGPrimaryTeam', 'custom/entry_points/setPGTeams.php','setPGTeams', 'setPGPrimaryTeam');

$hook_array['after_save'][] = Array(1,'setPGTeamSet', 'custom/entry_points/setPGTeams.php','setPGTeams', 'setPGTeamSet');

[/PHP]

This works fine when I create records from the interface but here comes the [B]Problem[/B]:

I have a script that creates special temporary tables and loads all Salesforce records from the csv backups into those mysql tables.

Then another that reads those records and puts the data in the appropriate sugar tables, links the records as they were linked in Salesforce etc. all using sugar beans.

In short it imports all Salesforce data into Sugarcrm.

It all works like a charm except for one thing, in some records (not on all records) the Team Sets are not set as they should be based on the logic hooks. A team that has nothing to do with the current user running the script or any of the users who are being assigned records, used as created_by or modified_by, appears on the Accounts, Contacts and Address records.

As for other records (Targets, Meetings, Notes, Leads etc) the primary team is set fine but the team set includes the created_by user's personal team (not a big deal but it shows the logic hook is not doing what I expect).

Clearly the global $current_user is me. And I do have the business unit set to 'PG'

Given the long winded premiseÉ Can anyone see anything wrong with the hook below?

Logic Hook Code:

[PHP]

include_once('modules/Users/User.php');

class setPGTeams

{

    function setPGPrimaryTeam($bean, $event, $arguments){

       //before save, set the primary team

       global $current_user;

       $bu = $current_user->business_unit_c;

       $module = $bean->module_dir;

       $global_team_mods = array('Accounts', 'Contacts','addlo_Address_Locations');

       $user_id = $current_user->id;

       if($bu=='PG' && !in_array($module, $global_team_mods)){

          $pg_team = new Team();

          $pg_team_id = $pg_team->retrieve_team_id('PG');

          $bean->team_id=$pg_team_id;

       //replace business region with WRI for PG records in Account, Contact or addlo_Address_Locations

       }elseif($bu=='PG' && in_array($module, $global_team_mods)){

          $bean->team_id='1'; //Global

       }

    }

    function setPGTeamSet($bean, $event, $arguments){

       //after save replace the team set

       global $current_user;

       $bu = $current_user->business_unit_c;

       $module = $bean->module_dir;

       $global_team_mods = array('Accounts', 'Contacts','addlo_Address_Locations');

       $user_id = $current_user->id;

       $pg_team = new Team();

       $pg_team_id = $pg_team->retrieve_team_id('PG');

       //PG Team only

       if($bu=='PG' && !in_array($module, $global_team_mods)){

          $team_array = array($pg_team_id);

          $bean->load_relationship('teams');

          $bean->teams->replace($team_array);

       }elseif($bu=='PG' && in_array($module, $global_team_mods)){

          $team_array = array('1',$pg_team_id);

          $bean->load_relationship('teams');

          $bean->teams->replace($team_array);

       }

    }

}

[/PHP]

Thanks!

Francesca

(from Francescas)

Outcomes