You need to sign in to do that
Don't have an account?

Enable Asynchronous Apex feature on Unlimited Edition org to fix the error for MIXED_DML_OPERATION
Hi there,
Following is the test class which is throwing the MIXED_DML_OPERATION error:-
// This class includes a test method for Trigger created on JRM_Project__c object.
Global class Automate_ProjectNumberClass {
static TestMethod Void testProjectNumberAutomation()
{
try
{
//1st Step is to create a new project.
JRM_Project__c project = new JRM_Project__c(Name = 'name');
insert project;
//2nd Step get the Region from the USER table, as this is the main criteria.
User[] loggedUser = [Select Region__c from User where Id =:UserInfo.getUserId()];
loggedUser[0].Region__c = 'Americas';
update loggedUser[0];
static TestMethod Void testProjectNumberAutomation()
{
try
{
//1st Step is to create a new project.
JRM_Project__c project = new JRM_Project__c(Name = 'name');
insert project;
//2nd Step get the Region from the USER table, as this is the main criteria.
User[] loggedUser = [Select Region__c from User where Id =:UserInfo.getUserId()];
loggedUser[0].Region__c = 'Americas';
update loggedUser[0];
insert project; // Once the User Region is determined simply insert the Project record
//Updating the exisitng region which is Americas to a new region and again inserting to Project.
// Similiar steps are repeated for each region.
/*loggedUser[0].Region__c = 'Asia Pacific';
update loggedUser[0];
insert project;
loggedUser[0].Region__c = 'Caspian';
update loggedUser[0];
insert project;*/
}
catch(System.DmlException e)
{
System.debug(e.getMessage());
}
}
//Updating the exisitng region which is Americas to a new region and again inserting to Project.
// Similiar steps are repeated for each region.
/*loggedUser[0].Region__c = 'Asia Pacific';
update loggedUser[0];
insert project;
loggedUser[0].Region__c = 'Caspian';
update loggedUser[0];
insert project;*/
}
catch(System.DmlException e)
{
System.debug(e.getMessage());
}
}
}
///////////////////////////////////////////////////////////////////
Next, I found a workaround to this problem and it seems like it will work. .This feature is called - Asynchronous Apex. Therefore the same test class was edited in the following way, but the problem is when it gets executed it complains to enable this feature. However, it is a Sandbox with unlimited edition so this feature should be there. Then what could be missing???
Here is the code:-
// This class includes a test method for Trigger created on JRM_Project__c object.
Global class Automate_ProjectNumberClass {
static TestMethod Void testProjectNumberAutomation()
{
try
{
//1st Step is to create a new project.
insertProject();
static TestMethod Void testProjectNumberAutomation()
{
try
{
//1st Step is to create a new project.
insertProject();
//2nd Step get the Region from the USER table, as this is the main criteria.
updateUserRegion('Americas');
}
catch(System.DmlException e)
{
System.debug(e.getMessage());
}
}
static void insertProject()
{
JRM_Project__c project = new JRM_Project__c(Name = 'name');
insert project;
}
updateUserRegion('Americas');
}
catch(System.DmlException e)
{
System.debug(e.getMessage());
}
}
static void insertProject()
{
JRM_Project__c project = new JRM_Project__c(Name = 'name');
insert project;
}
@future
static void updateUserRegion(String region)
{
User[] loggedUser = [Select Region__c from User where Id =:UserInfo.getUserId()];
loggedUser[0].Region__c = region;
update loggedUser[0];
}
}
////////////////////////////////////////////////////////////////////
Please advice.
Thanks