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
s.mafoders.mafoder 

DML Mixed exception on Before Update Trigger On Account

Hello , 

 

I have a trigger which purpose is when updating an account owner with an inactive user , we should activate this user first , update the account and then deactivate the user . 

 

I used in my before trigger on account 2 methods , on that activate the user , and an other methode (annoted with @future) to update the account , But I still get the DML exception : 

 

Here is the code of the trigger : 

 

trigger AccountBeforeInser on Account (before update) 
{
	  
	
	
	//TR001 :
	//whene inserting an Account by dataloader batch the trigger fill lookup fields
	
	   
	       
	       Set<Id> UsersIds = new Set<Id>();
	       Set<Id> AccIds = new Set<Id>();
	      
	       for(Account account : Trigger.new)
	       {
	             
	                  UsersIds.add(account.OwnerId);
	                  AccIds.add(account.Id);
	                
	                  
	       }
	       
	       if(UsersIds.size()>0)
	       {
	       		
	          
	           TR001Account.FillLookupFields(UsersIds);
	           TR001Account.updateAccounts(AccIds);
	          
	       
	       }
	       


}

 And the class TR001Account : 

 

public with sharing class TR001Account 
{
	  public static void FillLookupFields(Set<Id> UsersIds)
	  {
	  	List<User> users=[select Id,IsActive from User where Id IN :UsersIds];
	  	
	 
	   
	    for(User user:users)
	    {
	    	if(user.IsActive==false)
	    	user.IsActive=true;
	    	
	    	
	    	
	   
	    	
	    	
	    }
	  	
	  	update users;
  
	  	
	  }
	  @future
	  public static void updateAccounts(Set<ID> accountIds)
	  {
	  	
			  List<Account> accountsToUpdate =	[Select Name from Account where Id IN : accountIds];
			  for(Account acc :accountsToUpdate)
			  {
			  	acc.Name='true future';
			  }
			  update accountsToUpdate;
	  	
	  }
	  
	  
	  
	  
	  
	  

}

 I really rely on your Help !!

rajjjjrajjjj

I also got Mixed DML Exception error before when i am working on a set up and non set up object and trying to copy records between them. So i went for future annotation.

 

I have tried to assign an Inactive user.....but it is not showing up to select an Inactive user.

 

So i have runned your code by assigning an activer user only...But i am getting other exception which is

 

System.AsyncException: Future method cannot be called from a future or batch method:

 

 

 

And also in the future in ur code, you are not trying to update the new owner....instead you are trying to assign some value to account name.

 

 

vishal@forcevishal@force

since it is a before update trigger, you can update the Account.Name directly in your trigger.new loop where you're calling the filling the sets.

 

And then make the method which updates the user as a future method. So your trigger will first update the Account and then call a future method which will update the user if he's inactive. This should solve both the problems :

 

1. Mixed DML operation error

2. A future method can't be called from another future method. (this is happening because once your future method updates Account, trigger is called again which again invokes that method - a recursion).

 

Your trigger code should be

trigger AccountBeforeInser on Account (before update) {
	//TR001 :
	//whene inserting an Account by dataloader batch the trigger fill lookup fields
 	Set<Id> UsersIds = new Set<Id>();
for(Account account : Trigger.new){
 	UsersIds.add(account.OwnerId);
 	account.Name = 'true future';
	 }
	 if(UsersIds.size()>0) {
	TR001Account.FillLookupFields(UsersIds);
	 }
}

 The code in red line will help avoid recursion as it updates the Account through trigger itself.

 

And now, your class code should be

 

public with sharing class TR001Account 
{
	@future
	public static void FillLookupFields(Set<Id> UsersIds) {
	  	List<User> users=[select Id,IsActive from User where Id IN :UsersIds];
		for(User user:users){
	    		if(!user.IsActive)
	    			user.IsActive=true;
		 }
	  	if(!users.isEmpty())
			update users;
	}
}

 

 

This should work, let me know!

 

 

Ker DeveloperKer Developer

I have the follwing error : cannot perform operation with Inactive users

 

 

in fact the update of account that should be @future , because i shoul activate the user first , thats why i made the method on account future

 

Help pleaaase