7

I'm attempting to move an integration away from using Custom SObjects and to using Platform Events out of a concern for storage space (and also so our eventual automation of this integration won't have to delete 500k rows before each run).

I have the Platform Event, a Platform Event Trigger, and a PlatformEventSubscriberConfig (which is still a real hassle to create and edit) to change the running user away from "Automated Process User". I've made myself the running user and the subscriber config has taken effect, as I can see the logs when I publish an appropriate Platform Event from anonymous apex.

When I publish a Platform Event from the unit test meant to stress the Platform Event Trigger, however, Salesforce appears to be ignoring my configuration.

In my case, that's causing a test method to run afoul of a validation rule (PE causes an Account name to be changed, and only certain profiles are allowed to do that. The "Automated Process" profile is currently not in the pass-list).

I can, and have, worked around the issue for this one particular test failure, but it seems wrong to me that Salesforce isn't honoring the configuration I set up. I imagine that this is a side-effect of how Salesforce is currently isolating the Event Bus for tests.

Is there a way to force Salesforce to set a running user for a Platform Event Trigger in a unit test (or force Salesforce to honor the configuration I've already set up)?

2
  • Is this for your unpackaged metadata, a managed package, a second-generation managed package, or an unlocked package? Is this going to be used for deployments, or just for building package versions? Have you considered using a Custom Permission so you can just assign it to the Automated User during testing (which would also be a best practice for validation rules, instead of using profiles to control this)?
    – sfdcfox
    Sep 3 at 16:18
  • @sfdcfox This is currently unpackaged metadata, but eventually will be part of an unlocked package. The plan is to icnlude the subscriber config in deployments/installs. I have not yet considered custom permissions (I really should, though that'd be a project for another time).
    – Derek F
    Sep 3 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.