TCOSMM-32: Fix delay based date calculation - #3
Open
shahrukh-compuco wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors CRM_Civirules_Delay_DelayBasedOnDateField to simplify custom field retrieval, replacing a switch statement with a dynamic APIv4 call and adding robust error logging. Feedback suggests adding explicit null checks for $customField and $customData to prevent PHP 8.x warnings when no records are found.
erawat
approved these changes
Jul 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
When a CiviRule action that creates a case activity (e.g. "Add activity to case") has its Activity Date configured as "Delay based on date field" using a Case custom date field, the calculated date was being ignored and the activity was always created with today's date. This PR fixes the delay calculation so the configured date field is respected for Case (and all other supported entities), not just Contact and Activity.
Before
After
Technical Details
Root cause was in
CRM_Civirules_Delay_DelayBasedOnDateField::addInCustomField():switchthat only handledActivity, with every other entity falling through to aCivi\Api4\Contact::get()call. For a Case custom field this queried Contact with the case ID and a custom field that doesn't exist on Contact, threw, and the emptycatchswallowed the exception — sodelayTo()fell back tonow.delayTo()passed$triggerData->getEntity()(the trigger's primary entity) into the lookup instead of$this->entity(the entity the delay was configured for), even though both$dataand the field prefix are derived from$this->entity.Changes:
delayTo()now passes$this->entityintoaddInCustomField().ucfirst()guards against triggers that register lowercase entity names (e.g. the Birthday cron trigger registers'contact') — APIv4 entity names are case-sensitive, so without this the previously-working Birthday path would regress.checkPermissions => FALSE(andCustomField::get(FALSE)): delayed actions run via the queue/cron, often with no logged-in user, where the old permission-checked calls could silently fail the same way.catch (API_Exception)widened tocatch (Exception)—API_Exceptionis a deprecated alias (removed in newer cores) — and the catch now logs a warning viaCivi::log('civirules').$data['id']and empty custom field values (previous behaviour preserved: fall back tonow).All callers of
delayTo()were audited (Engine::getActionDelay(),Activity/Addincl. theAddToCasesubclass,Activity/Edit,CreateActivityFromEvent,Case/SetDateFieldOnCase) — the change is fully internal to the delay class and the previously-working Contact/Activity paths issue identical queries to before.Core overrides
None
Comments