如何避免创建用户的 Salesforce 测试中的 MIXED_DML_OPERATION 错误
有时,在 Salesforce 测试中,您需要创建用户对象以作为特定类型的用户运行部分测试。
然而,自 Salesforce Summer 08 更新以来,尝试在同一测试中创建用户对象和普通对象(例如帐户)会导致以下错误:
MIXED_DML_OPERATION,更新非设置对象后不允许对设置对象进行 DML 操作(反之亦然):用户,原始对象:帐户
注意,从 Eclipse/Force.com 运行测试时不会发生该错误IDE,但当您部署到 Salesforce,然后从 Salesforce 内运行测试时,确实会发生这种情况。
如何重写测试以避免此错误?
这是导致错误的测试的简单示例:
static testMethod void test_mixed_dmlbug() {
Profile p = [select id from profile where name='(some profile)'];
UserRole r = [Select id from userrole where name='(some role)'];
User u = new User(alias = 'standt', email='[email protected]',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
timezonesidkey='America/Los_Angeles',
username='[email protected]');
Account a = new Account(Firstname='Terry', Lastname='Testperson');
insert a;
System.runAs(u) {
a.PersonEmail = '[email protected]';
update a;
}
}
Sometimes in Salesforce tests you need to create User objects to run part of the test as a speciifc type of user.
However since the Salesforce Summer 08 update, attempts to create both User objects and normal objects (such as Accounts) in the same test lead to the following error:
MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Account
Note that the error doesn't happen when you run the tests from Eclipse/Force.com IDE, but it does happen when you deploy to Salesforce and then run the tests from within Salesforce.
How do I re-write my tests to avoid this error?
Here's a simple example of a test that causes the error:
static testMethod void test_mixed_dmlbug() {
Profile p = [select id from profile where name='(some profile)'];
UserRole r = [Select id from userrole where name='(some role)'];
User u = new User(alias = 'standt', email='[email protected]',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
timezonesidkey='America/Los_Angeles',
username='[email protected]');
Account a = new Account(Firstname='Terry', Lastname='Testperson');
insert a;
System.runAs(u) {
a.PersonEmail = '[email protected]';
update a;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我猜这里还没有多少 Salesforce 人员。
我找到了一个解决方案,我不知道为什么它有效,但它有效。
访问普通对象的测试的所有部分都需要包装在显式使用当前用户的 System.runAs 中,如下所示:
因此,问题中给出的示例 text_mixed_dmlbug 方法将变为:
然后 MIXED_DML_OPERATION 错误停止发生。
Not many Salesforce people on here yet, I guess.
I found a solution, I don't know why it works, but it works.
All parts of the test that access normal objects need to be wrapped in a System.runAs that explicitly uses the current user, like this:
So, the example text_mixed_dmlbug method given in the question, would become:
Then the MIXED_DML_OPERATION errors stop happening.
看来您已经找到了解决方法。我只是想尝试弄清楚为什么会出现此错误。
我认为您遇到了这个问题(根据 http://www .salesforce.com/us/developer/docs/apexcode/Content/apex_dml_non_mix_sobjects.htm):
此外,Summer 08 发行说明(该链接是 PDF)说:
It seems like you've found a workaround. I just wanted to try and clear up why you where getting this error.
I think you are running into this issue (per http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_non_mix_sobjects.htm):
In addition, the Summer 08 Release notes (that link is a PDF) say:
此行为实际上记录在 salesforce 文档中: http: //www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_dml_non_mix_sobjects.htm?SearchType。阅读其中写着“重要
主要的例外是当您在测试中使用 runAs 方法时”
This behavior is actually documented in the salesforce documentation: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_dml_non_mix_sobjects.htm?SearchType. Read where it say "Important
The primary exception to this is when you are using the runAs method in a test"
刚刚在文档中找到了这个:
因此,
RunAs
解决方法似乎不是解决方法,但 Salesforce 认为它是解决混合 DML 问题的唯一方法。希望这有帮助
参考
Just found this in the documentation:
So it looks like the
RunAs
workaround is not a workaround but is assumed by Salesforce as the only way of going by the mixed DML issue.Hope this helps
Reference
当尝试在 apex 中的单个事务中创建用户和其他对象记录时,此错误非常常见。
apex 类/触发器中的解决方法:遇到错误时使用 future 方法创建用户
测试类中的解决方法:不要尝试创建新的用户数据,而是使用 ))>
代码片段位于 -
https://thesalesforcedev.blogspot.com/2019/07/ mixdmloperation-dml-operation-on.html
This error is so common when attempting to create user and other objects records in a single transaction in apex.
Workaround in apex class/trigger : use future method for creating user when encountered the error
Workaround in test class : don't try creating a new user data, instead use ))>
code-snippet at -
https://thesalesforcedev.blogspot.com/2019/07/mixeddmloperation-dml-operation-on.html