Event Added, CiviRule triggered before custom data for the Event is available, conditional fails as a result. Recommend changing trigger to use the hook_civicrm_postCommit instead of hook_civicrm_post where data is partially available
As a site builder, I expect that the "Event Added" trigger would be executed after the Event is saved and all related data for the Event is available, ie. priceset, custom fields, location etc. so that this can be used in the Conditional for the CiviRule.
However, the "Event Added" trigger uses the hook_civicrm_post which is called by $event = self::add($params);
which is BEFORE the priceset and custom fields are saved for the Event. In fact, before the transaction commit is performed. See code below. Only some of the Event data is available. The impact is that any Conditionals for the CiviRule which refer to Custom Fields will fail.
civicrm/CRM/Event/BAO/Event.php
/**
* Add the event.
*
* @param array $params
* Reference array contains the values submitted by the form.
*
* @return CRM_Event_DAO_Event
*/
public static function add(&$params) {
$financialTypeId = NULL;
if (!empty($params['id'])) {
CRM_Utils_Hook::pre('edit', 'Event', $params['id'], $params);
if (empty($params['skipFinancialType'])) {
$financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['id'], 'financial_type_id');
}
}
else {
CRM_Utils_Hook::pre('create', 'Event', NULL, $params);
}
$event = new CRM_Event_DAO_Event();
...
civicrm/CRM/Event/BAO/Event.php
/**
* Create the event.
*
* @param array $params
* Reference array contains the values submitted by the form.
*
* @return object
*/
public static function create(&$params) {
$transaction = new CRM_Core_Transaction();
if (empty($params['is_template'])) {
$params['is_template'] = 0;
}
// check if new event, if so set the created_id (if not set)
// and always set created_date to now
if (empty($params['id'])) {
if (empty($params['created_id'])) {
$session = CRM_Core_Session::singleton();
$params['created_id'] = $session->get('userID');
}
$params['created_date'] = date('YmdHis');
// Clone from template
if (!empty($params['template_id'])) {
$copy = self::copy($params['template_id']);
$params['id'] = $copy->id;
unset($params['template_id']);
}
}
$event = self::add($params);
CRM_Price_BAO_PriceSet::setPriceSets($params, $event, 'event');
if (is_a($event, 'CRM_Core_Error')) {
CRM_Core_DAO::transaction('ROLLBACK');
return $event;
}
$contactId = CRM_Core_Session::getLoggedInContactID();
if (!$contactId) {
$contactId = $params['contact_id'] ?? NULL;
}
// Log the information on successful add/edit of Event
$logParams = [
'entity_table' => 'civicrm_event',
'entity_id' => $event->id,
'modified_id' => $contactId,
'modified_date' => date('Ymd'),
];
CRM_Core_BAO_Log::add($logParams);
if (!empty($params['custom']) &&
is_array($params['custom'])
) {
CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_event', $event->id);
}
$transaction->commit();
return $event;
}
Proposed Change
If the "Event Added" trigger is changed to use hook_civicrm_postCommit then this will execute the trigger AFTER the Event is saved to the database and all related data is available. This would be preferred and would then match the expectation that all Event data is available and therefore can be used in the Conditionals for the CiviRule.
Also relates to #142 (closed)
Agileware Ref: CIVIRULES-10