Saturday, 15 June 2013

MIXED DML Exception from APEX Batch class

Batch class execution fail when you are updating set up(Eg. User) and non-setup(Eg. Custom Sobject) object in same context in apex.

There is a solution in batch class for this:

In batch class we have public void finish() method , we can utilize this method to get new apex context. First either setup object or non setup object update add in execute() method And later add other object update in void finish() method.

Also to pass the variable from execute method to finish method , declare the variable name as global. Also implement the Database.Stateful interface in the same batch class to maintain the variable value.
Hope this will help some one who stuck in writing apex code where setup and non-setup object update is required.