function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AashikaAashika 

Using Asynchronous Apex in the test class and enable it on the org

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];          
            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());
        }
    }
}
///////////////////////////////////////////////////////////////////
 
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();         
            //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; 
    }

    @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
 
 
 
canonwcanonw

I had exactly the same issue several months ago.

At that time, @future keyword was not available in production.

The reason to us asynchronous method is this.  Triggers don't allow these sObjects to together with other types in an DML operation.

  • Group
  • GroupMember
  • QueueSObject
  • User
  • UserRole
  • UserTerritory
  • Territory

And this is documented in...
The Apex Language Reference Guide, under "Chapter 9 - Reference" , "sObjects That Cannot Be Used Together in DML Operations" p167.

Perhaps you should call support regarding to general release of @future.