Send SMS immediate from CiviRules fails for anonymous due to lack of $sourceContactID
Overview
As noted in Stackexchange, CiviRules fails to send an immediate SMS if the condition is triggered by the anonymous user (e.g. follow-up to a webform). The workaround is to delay the action in CiviRules. Jaap pointed out the cause, "the delayed task gets a $sourceContactId = CRM_Core_Session::getLoggedInContactID(); (sites/all/modules/civicrm/CRM/Activity/BAO/Activity.php) whereas the webform triggered one doesn't."
The workaround isn't attractive. The typical user expects to receive a confirmation SMS right away. To simulate this w. the workaround we'd have to crank our cron heartbeat from 15 minutes down to 1 minute, and even that delay is quite unfriendly.
Reproduction steps
- Grant anonymous users the "CiviCRM: send SMS" permission in Drupal
- Create a civicrm_webform that collects a cellphone number, allow submit by anonymous
- Create a CiviRule that detects creation of that webform submission activity, trigger on the source contact
- Set action to send an SMS, immediately
- Open an incognito browser window and submit the form
Current behavior
No SMS activities are created and no SMS sent.
The following line is added to the CiviCRM log: Nov 14 09:02:19 [error] Civirules api action exception: Not enough data to create activity object. API call: Sms.send with params: provider_id="6", template_id="294", alternative_receiver_phone_number="", contact_id="3027"
Expected behavior
SMS sent, activities created with source contact per the webform's match or creation of a contact.
Environment information
CiviCRM 5.43.1, CiviRules 2.3, Drupal 7.82
Comments
This issue is to provide context for a PR I'm hoping to submit right away. The PR detects the absence of $sourceContactID after the aforementioned assignment and stubs in the ID of the first contact. This is perhaps too broad a hammer, for instance CiviRules already sends emails sourced by this anonymous user contact ID without a problem. It appears the email logic corrects for this problem somewhere upstream of Activity.php, but the SMS logic doesn't.