When duplicating a record also duplicate related records

When I duplicate a record I also want to duplicate related records

In 6.5 this was fairly simple

I did it using an after save logig hook

I checked  for $_REQUEST['duplicateId']  and $_REQUEST['module'] == 'zz_expenses_head')

But this doesn't work in 7

I can't see how to identify a duplicated record in 7

My 6.5 after save code is:

require_once('data/SugarBean.php');
require_once('include/utils.php');

class zz_expenses_headAfterSave {
    function zz_expenses_headAfterSave (&$bean, $event, $arguments)
    {
        #duplicate expenses & detail
        if ( $_REQUEST['duplicateId'] && $_REQUEST['module'] == 'zz_expenses_head') {

            $eh = new zz_expenses_head;
            $eh->retrieve($_REQUEST['duplicateId']);

            $details = $eh->get_linked_beans('zz_expenses_detail','zz_expenses_detail');

            foreach($details as $detail) {

                $ndetail = new zz_expenses_detail;
                $ndetail->zz_expenses_head_id = $bean->id;
                $ndetail->name = $detail->name;
                $ndetail->description = $detail->description;
                $ndetail->cost = $detail->cost;
                $ndetail->passenger = $detail->passenger;
                  $ndetail->total = $detail->total;
                  $ndetail->vat = $detail->vat;
                  $ndetail->distance = $detail->distance;
                  $ndetail->distance_type = $detail->distance_type;
                  $ndetail->zz_vat_codes_id = $detail->zz_vat_codes_id;
                  $ndetail->zz_exp_analysis_id = $detail->zz_exp_analysis_id;
                 $ndetail->parent_type = $detail->parent_type;
                 $ndetail->parent_id = $detail->parent_id;
                 $ndetail->assigned_user_id = $bean->assigned_user_id;
                 $ndetail->skip = 'Y';
                $ndetail->save();

            } #end foreach
        } #end if
    } #end function
} #end class
Parents
  • That did the trick :)

    My code is:

    if (preg_match('/picture_duplicateBeanId/', $_SERVER['QUERY_STRING'])){
       $split = explode('&',$_SERVER['QUERY_STRING']);
       foreach($split as $k) {
           if (preg_match('/picture_duplicateBeanId/', $k)){
              $aid = explode('=',$k);
              $id = $aid[1];
              continue;
           }
        }
       $eh = new zz_expenses_head;
       $eh->retrieve($id);
       $details = $eh->get_linked_beans('zz_expenses_detail','zz_expenses_detail');
        foreach($details as $detail) {
           $ndetail = new zz_expenses_detail;
           $ndetail->zz_expenses_head_id = $bean->id;
            $ndetail->name = $detail->name;
            $ndetail->description = $detail->description;
       .....
           $ndetail->save();
        }
    }
                                                



  • Another way to detect the copy action (and the source ID) is to check $_REQUEST['after_create']['copy_rel_from'] for the ID of the source record.  This is the parameter set by clients/base/views/create/create.js that signals clients/base/api/ModuleApi.php's processAfterCreateOperations() to copy relationships from the source record.  In theory this could be more reliable, as it is not dependent on specific field names, though I'm not sure if it makes much difference in practice.
Reply
  • Another way to detect the copy action (and the source ID) is to check $_REQUEST['after_create']['copy_rel_from'] for the ID of the source record.  This is the parameter set by clients/base/views/create/create.js that signals clients/base/api/ModuleApi.php's processAfterCreateOperations() to copy relationships from the source record.  In theory this could be more reliable, as it is not dependent on specific field names, though I'm not sure if it makes much difference in practice.
Children
No Data