AnsweredAssumed Answered

new bean relationship fields don't save

Question asked by Tom Noel on Feb 27, 2018
Latest reply on Feb 27, 2018 by Tom Noel

I have an after_relationship_add logic hook that triggers when the Account on a Contact is changed.  The logic hook then fetches related "Trade Show Attendance" records and creates duplicates adding the Contact in the relationship field.

 

My problem is that while duplicating these records none of the relationship fields are saving.  My code below:

I'm developing in a vagrant 7.10+ environment and this Sugar instance is on 7.11.  My operating system is Mac OS version 10.13.3.  I was curious if this was an environment issue so I deployed to an ondemand sandbox and was still receiving the same issue.  Any help would be appreciated on how to properly set relationship fields on a newBean in a logic hook.

<?php
class contactAttendee
{
    function contactAttendee($bean, $event, $arguments)
    {


        if($arguments['link'] == 'contacts'){

            $query = new SugarQuery();
            $query->from(BeanFactory::getBean('TSA_Trade_Show_Attendance'));
            $accounts = $query->join('accounts_tsa_trade_show_attendance_1')->joinName();
            $query->select(array("id"));
            $query->where()->equals("$accounts.id",$arguments['id']);
            $results = $query->execute();

            if(!empty($results)){


                foreach($results as $key => $tradeshowattendance){

                    //related Trade show attendance record
                    $ts_attendance = BeanFactory::getBean('TSA_Trade_Show_Attendance', $tradeshowattendance['id']);

                    $exclude = array(
                        'id',
                        'date_entered',
                        'date_modified'
                    );

                    $new_trade_show = BeanFactory::newBean('TSA_Trade_Show_Attendance');


                    //Trade Show relationship add
                    if ($new_trade_show->load_relationship('ts_trade_show_tsa_trade_show_attendance_1'))
                    {
                        $new_trade_show->ts_trade_show_tsa_trade_show_attendance_1->add($ts_attendance->ts_trade_show_tsa_trade_show_attendance_1ts_trade_show_ida);
                    }

                    //Contact relationship add
                    if ($new_trade_show->load_relationship('contacts_tsa_trade_show_attendance_1'))
                    {
                        $new_trade_show->contacts_tsa_trade_show_attendance_1->add($arguments['related_id']);
                    }

                    //Account relationship add
                    if ($new_trade_show->load_relationship('accounts_tsa_trade_show_attendance_1'))
                    {
                        $new_trade_show->accounts_tsa_trade_show_attendance_1->add($ts_attendance->accounts_tsa_trade_show_attendance_1accounts_ida);
                    }

                    foreach($ts_attendance->field_defs as $def){
                        if(!empty($def['name'])
                            && !in_array($def['name'], $exclude)){
                            $field = $def['name'];
                            $new_trade_show->{$field} = $ts_attendance->{$field};
                        }
                    }


                    $new_trade_show->save();

                }


            }


        }


        return;
    }
}
?>

I've also tried setting the relationship field directly rather than using the add function like so:

$new_trade_show->accounts_tsa_trade_show_attendance_1accounts_ida = $ts_attendance->accounts_tsa_trade_show_attendance_1accounts_ida;

 

Thanks,

Tom Noel

Outcomes