Hello frinds,
Today i am talking about , how to solve mixd DML error in Apex,
This error mostly come when , yiu are writing Apex Test .
When are you insert Setup and non-setup object records in single transaction.
Step-1- Writing Apex test class @isTest public class MIXEDDML { @isTest public static void TestData(){ UserRole obj=new UserRole(Name= 'ABC'); insert obj; Profile pf= [Select Id from profile where Name='System Administrator']; String orgId=UserInfo.getOrganizationId(); String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ; User uu=new User(firstname = 'ABC', lastName = 'XYZ', email = 'ak288@test.org', Username = 'ak288@test.org', EmailEncodingKey = 'ISO-8859-1', Alias = 'tst12', TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', LanguageLocaleKey = 'en_US', ProfileId = pf.Id, UserRoleId = obj.Id); insert uu; Contact con = new Contact(); con.LastName = 'Test con'; insert con; } }
Step-2- Run Apex test class
Step-3- How to avoid or how resolve this issue create one anothe apex class and method using @future annotation public class TestFutureMethod { @future public static void TestFutur(){ UserRole obj=new UserRole(Name= 'ABC'); insert obj; Profile pf= [Select Id from profile where Name='System Administrator']; String orgId=UserInfo.getOrganizationId(); String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ; User uu=new User(firstname = 'ABC', lastName = 'XYZ', email = 'ak288@test.org', Username = 'ak288@test.org', EmailEncodingKey = 'ISO-8859-1', Alias = 'tst12', TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', LanguageLocaleKey = 'en_US', ProfileId = pf.Id, UserRoleId = obj.Id); insert uu; //return uu; } } This Apex class called in Apex test class @isTest public class MIXEDDML { @isTest public static void TestData(){ TestFutureMethod.TestFutur(); Contact con = new Contact(); con.LastName = 'Test con'; insert con; } }
Step-4- Run Apex test class
Using this technique, you can avoid this error and handle this issue.
Thanks!
Ashish Madhukar
Step-1- Writing Apex test class @isTest public class MIXEDDML { @isTest public static void TestData(){ UserRole obj=new UserRole(Name= 'ABC'); insert obj; Profile pf= [Select Id from profile where Name='System Administrator']; String orgId=UserInfo.getOrganizationId(); String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ; User uu=new User(firstname = 'ABC', lastName = 'XYZ', email = 'ak288@test.org', Username = 'ak288@test.org', EmailEncodingKey = 'ISO-8859-1', Alias = 'tst12', TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', LanguageLocaleKey = 'en_US', ProfileId = pf.Id, UserRoleId = obj.Id); insert uu; Contact con = new Contact(); con.LastName = 'Test con'; insert con; } }
Step-2- Run Apex test class
Step-3- How to avoid or how resolve this issue create one anothe apex class and method using @future annotation public class TestFutureMethod { @future public static void TestFutur(){ UserRole obj=new UserRole(Name= 'ABC'); insert obj; Profile pf= [Select Id from profile where Name='System Administrator']; String orgId=UserInfo.getOrganizationId(); String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ; User uu=new User(firstname = 'ABC', lastName = 'XYZ', email = 'ak288@test.org', Username = 'ak288@test.org', EmailEncodingKey = 'ISO-8859-1', Alias = 'tst12', TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', LanguageLocaleKey = 'en_US', ProfileId = pf.Id, UserRoleId = obj.Id); insert uu; //return uu; } } This Apex class called in Apex test class @isTest public class MIXEDDML { @isTest public static void TestData(){ TestFutureMethod.TestFutur(); Contact con = new Contact(); con.LastName = 'Test con'; insert con; } }
Step-4- Run Apex test class
Using this technique, you can avoid this error and handle this issue.
Thanks!
Ashish Madhukar
Insert the user object inside the system.runas user
ReplyDelete