You need to sign in to do that
Don't have an account?
VictorBV
MIXED_DML_OPERATION in a future method - How is it possible?
Hi,
I am provisioning Customer Portal via Apex so I need to create users dinamically.
After some reading and developing I faced this problem that puzzles me.
I got next error executing testing
TestUserCreationForContact.singleInserTest | 277.0 | System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, La operación DML en el objeto de configuración no está permitida después de actualizar un objeto no configurado (o viceversa): User objeto original: Account: [] | Class.Utilities.createNominalUser: line 53, column 3 External entry point |
I show you the method createNominalUser
@future public static void createNominalUser(String alias, String email, String emailEncodingKey, String lastName, String languageLocaleKey, String localesKey,
ID profile, String timeZonesIdKey, String userName, ID userRole)
{
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.EmailHeader.triggerUserEmail = true;
User user = new User(alias = alias, email = email, emailencodingkey = emailEncodingKey, lastname = lastName, languagelocalekey = languageLocaleKey,
localesidkey = localesKey, profileid = profile, timezonesidkey = timeZonesIdKey, username = userName, userRoleId = userRole);
user.setOptions(dmo);
insert user; //line 53
System.debug('Usuario nominal creado, Id:' + user.Id);
}
How is that possible?
How can I fix it?
Ok.
I used some strange trick I found... and I dont know why neither how but it worked.
I just used System.runAs to wrap Test, like this
And it worked.
Someone can explain why that worked to solve the problem?
Thanks very much
All Answers
Hi,
Are you trying to insert/update/delete some other object's record in the same @future method?
Because in an apex class/method, where you are performing DML operation in the User, you can't perform DML operation on other objects.
If so, try removing the corresponding objects DML statement.
Thanks
Hi Sureka,
Thanks for your quick answer.
As you can see, I only insert a user in my @future method.
I post the method which calls the creation, maybe there is something wrong with it
Thanks in advance
Ok.
I used some strange trick I found... and I dont know why neither how but it worked.
I just used System.runAs to wrap Test, like this
And it worked.
Someone can explain why that worked to solve the problem?
Thanks very much
See
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_dml_non_mix_sobjects.htm?SearchType=Stem
Specifically "The primary exception to this is when you are using the runAs
method in a test. For more information, see System Methods"
Thanks
Ryne