+ Start a Discussion
RajeevlsHydRajeevlsHyd 

Test Class Error while creating leads

I am getting an error while running the test class , Leads are not getting created in the system ..

 

Test class snipet :


  Lead testLead = Class_Test.createLead('Test Lead', 'Test Company', '02s30000000000dAAB', 'Core');
    testLead.OwnerId = user.Id;
    insert testLead;

    system.assertEquals(testLead.OwnerId, uSBS.Id);  

 ---------------------

Class_Test :

 

public static Lead createLead(String lastName, String company, Id divId, String status){
   
    Datetime t = Datetime.now();
    Date da = Date.newInstance(t.year(), t.month(), t.day());
    Lead lead = new Lead(LastName = lastName, Company = company, Division = divId, Status = status, Phone = '1234', Start_Date__c = da);
   
    return lead;
  } 

 

Error :

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, EZAssign.CS_Lead: execution of AfterInsert

caused by: System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, EZAssign.CS_UserInfo: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0 with id 005e00000012MJJAA2; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Lead: []

(EZAssign)
: []

(EZAssign)


: []

 

 

Any idea how to rectify this error

Best Answer chosen by Admin (Salesforce Developers) 
chiranjeevitgchiranjeevitg

Hi rajeev,

 

In the same test class are you doing any dml operation on user/profile/roles? If so, this exception occurs. For this you have to run the user/profile insertion under System.runAs() context.

 

check this link it has detail description

 

http://www.tgerm.com/2012/04/mixeddmloperation-dml-operation-on.html

 

 

Thanks & Regards,

Chiranjeevi T G

All Answers

p vrp vr

 Need to see complete Lead Trigger Code - Are there any other objects getting updated in Lead Trigger? if so, other object trigger code too.

chiranjeevitgchiranjeevitg

Hi rajeev,

 

In the same test class are you doing any dml operation on user/profile/roles? If so, this exception occurs. For this you have to run the user/profile insertion under System.runAs() context.

 

check this link it has detail description

 

http://www.tgerm.com/2012/04/mixeddmloperation-dml-operation-on.html

 

 

Thanks & Regards,

Chiranjeevi T G

This was selected as the best answer
RajeevlsHydRajeevlsHyd

Thanks Chiranjeevi