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();
        }
    }
                                                



  • I am trying to only perform code during the first before_save...only during the create of a record. I thought that checking isUpdate would allow me to do this but I never go into the code. Here is the code in question. Any help would be appreciated:

    <?phpclass LinkMCAData_class
    {
     function LinkMCAData_method($bean, $events, $arguments)
     {

       //check if this is the first save
       if (isset($arguments['isUpdate']) && $arguments['isUpdate'] == false)
       {
     
     $GLOBALS['log']->info("LinkMCAData:: New Rec");
         //This is the first save, do this stuff...
     .
     .
     .
       }
       else
        //This is a subsequent save, fall out of the code
       {
        $GLOBALS['log']->info("LinkMCAData:: Existing Rec");
       }
      }

    }
    ?>
  • Hi Dianna,

    Here is the code you can try

    //check if this is the first save
    if(empty($bean->fetched_row)) {
    {

    $GLOBALS['log']->info("LinkMCAData:: New Rec");
     //This is the first save, do this stuff...
    .
    }
    else
    //This is a subsequent save, fall out of the code
    {
    $GLOBALS['log']->info("LinkMCAData:: Existing Rec");
    }
    Let us know if this helps.

    Regards.
    Hats
Reply Children
No Data