如何在运行测试类时停止执行托管触发器?
通常,当触发器运行时,我们会检查用户拥有哪种配置文件,如果是我们不希望触发器运行的配置文件,那么我们会在运行任何其他代码之前退出触发器。
问题:我们安装了一个从其他公司购买的 SF 软件包,它的所有代码对我们来说都是不可见的,并且不可编辑。除了通过 UI 手动禁用这些触发器之外,我们如何阻止其中一些触发器运行?我想在运行测试类时暂时禁用它们。
正在考虑做这样的事情,但收到一条错误消息“ApexTrigger 上不允许 DML”。
ApexTrigger at = [select id from ApexTrigger where name='SomeTriggerName'];
at.status = 'Inactive';
update at;
Usually, when a trigger runs, we check what kind of a profile the user has, and if it's the kind where we don't want the triggers to run, then we exit the trigger before running any other code.
Problem: we have a SF package installed that we purchased from some other company, and all of its code is invisible to us, and is not editable. How can we stop some of those triggers from running other than manually disabling them through UI? I want to temporarily disable them while running a test class.
Was thinking about doing something like this, but got an error saying "DML not allowed on ApexTrigger."
ApexTrigger at = [select id from ApexTrigger where name='SomeTriggerName'];
at.status = 'Inactive';
update at;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试过做类似的事情,但被卡住了。我不认为有办法在不让托管软件包的所有者更新 Apex 代码的情况下完成您所要求的操作。
您在问题之前列出的方法是一个很好的解决方案;不过,我建议另外使用自定义设置。您可以建议触发器的所有者/开发人员在执行触发器之前实施自定义设置检查。这是我能为自己的一些触发器想出的最佳解决方案。
如果可以更新 ApexTrigger 对象那就太好了,但 Salesforce 不允许这样做。
I've tried doing something similar, and got stuck. I don't believe there's a way to do what you're asking without having the owner of the managed package update the Apex Code.
The approach you listed before the problem is a great solution; I would recommend using Custom Settings in addition, though. You could recommend to the owner/developer of the Trigger to implement a Custom Settings check before executing the Trigger(s). That's the best solution I could come up with for some of my own Triggers.
It would be great if the ApexTrigger object could be updated, but Salesforce doesn't allow it.
我相信您唯一的选择是卸载或取消部署托管包。
I believe your only options are to uninstall or undeploy the managed package.