I'm seeing some unusual behavior when using a beforeSave in a hook for meetings. All is working as expected when users create a meeting from the meeting entity, but when a user creates a meeting from the calendar or from the activities section on the account the hook does not run and the actions are not completed. The meeting record itself is being created, however. Is there a way to make sure the hook is running when adding a meeting from the different locations? Do I need to add additional copies of the hook for calendar and account?
Meeting hook works only from main entity create button
Collapse
X
-
I've upgraded my version to 5.0.3 successfully. I notice that my hooks for a meeting will only show up if the record is saved twice.
I wrote a beforeSave hook on meetings to check for the last held meeting and the next planned meeting and to write their ids to two corresponding link fields on both the account entity and a custom prospecting entity. Is that because there is not yet an id associated with the record? Should I be using afterSave instead?
If I edit the meeting and save it a second time then it seems to work properly.
Here is my Custom/Hooks/Meeting/MeetingAccount.php
namespace Espo\Custom\Hooks\Meeting;
use Espo\ORM\Entity;
class MeetingAccount extends \Espo\Core\Hooks\Base
{
public function beforeSave(Entity $meeting)
{
$ID = $meeting->get('id');
$parentID = $meeting->get('parentId');
$status = $meeting->get('status');
//////////////////////////////////////
//Open connection to Account based on parent id (accountid)
$accountList = $this->getEntityManager()->getRepository('Account')->where(array(
'id' => $parentID,
))->findOne();
/////////////////////////////////////
if ($accountList != ""){
$accountID = $accountList->get('id');
////////////////////////////////////// LAST MEETING
//Open connection to Meeting based on account id
//Check for an existing record already in the Meeting Database based on the account_id with HELD status
$meetingList = $this->getEntityManager()->getRepository('Meeting')->where(array(
'parentId' => $parentID,
'status' => 'Held'
))->order('dateStart', 'DESC')->findOne();
////////////////////////////////////
////////////////////////////////////// NEXT MEETING
//Open connection to Meeting based on account id
//Check for an existing record already in the Meeting Database based on the account_id with PLANNED status
$nextMeetingList = $this->getEntityManager()->getRepository('Meeting')->where(array(
'parentId' => $parentID,
'status' => 'Planned'
))->order('dateStart', 'ASC')->findOne();
/////////////////////////////////////
//Open connection to Account based on this account id
$account = $this->getEntityManager()->getEntity('Account', $accountID);
/////Update Last Meeting
if($meetingList != ""){
$meetingID = $meetingList->get('id');
$meetingStatus = $meetingList->get('status');
if($status == 'Held'){
$account->set('meetingId', $meetingID);
}
} else {
$account->set('meetingId', "");
}
/////Update Next Meeting
if($nextMeetingList != ""){
$nextMeetingID = $nextMeetingList->get('id');
$nextMeetingStatus = $nextMeetingList->get('status');
if($status == 'Planned'){
$account->set('nextMeetingId', $nextMeetingID);
}
} else {
$account->set('nextMeetingId', "");
}
/////Updated record
$this->getEntityManager()->saveEntity($account);
}
}
}
***Note: I removed the option to select a parent other than Account for the meeting. On Account entity, I added 2 custom link fields called meetingID and nextMeetingID. I then modified the entitydefs json for account to display the meeting start date for both of these fields. When searching for the single meetings, I probably should also check for deleted status of 0. This may be helpful code when work with hooks.Last edited by joy11; 01-30-2018, 07:34 PM.Comment
Comment