AnsweredAssumed Answered

When duplicating a record also duplicate related records

Question asked by Mike Solomon on Jul 23, 2014
Latest reply on Jul 29, 2015 by Dianna Dianna
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

Outcomes