• jcasazza
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

Hello, 

 

I am trying to prevent a duplicate record from being inserted so I am using the sObject.addError() method in a before insert trigger. The trigger was written to handle bulk operations. In my test class When I insert a duplicate record, I catch the exception from the addError() call. When I run a bulk test,  the behavior is not what I expected.

 

This is an example, not the actual code:

Case newCase = new case(Subject = 'Subject1', SuppliedEmail='test@test.com'; Origin="Email to Case'));

insert new Case;

 

List<Case> caseList = new List<Case>();

caseList.add(new case(Subject = 'Subject1', SuppliedEmail='test@test.com'; Origin="Email to Case'));

caseList.add(new case(.Subject = 'Subject2', SuppliedEmail='testx@test.com'; Origin="Email to Case'));

caseList.add(new case(Subject = 'Subject3', SuppliedEmail='testy@test.com'; Origin="Email to Case'));

caseList.add(new case(Subject = 'Subject4', SuppliedEmail='testz@test.com'; Origin="Email to Case'));

insert caseList;

 

When the insert caseList is executed, I am expecting the first record to fail due to the addError call that will be executed because it is a duplicate of the case created in the first insert operation. But I am also expecting the other three cases to be created.

 

When I query the Case object for the other 3 cases, my assertion fails because I am expecting 1 record to exist for 'Subject2' but the actual is 0.

 

I didn't think addError caused a rollback on all records in a bulk transaction. Am I missing something?

 

Thanks in advance,

 

Jim

Hello, 

 

I am trying to prevent a duplicate record from being inserted so I am using the sObject.addError() method in a before insert trigger. The trigger was written to handle bulk operations. In my test class When I insert a duplicate record, I catch the exception from the addError() call. When I run a bulk test,  the behavior is not what I expected.

 

This is an example, not the actual code:

Case newCase = new case(Subject = 'Subject1', SuppliedEmail='test@test.com'; Origin="Email to Case'));

insert new Case;

 

List<Case> caseList = new List<Case>();

caseList.add(new case(Subject = 'Subject1', SuppliedEmail='test@test.com'; Origin="Email to Case'));

caseList.add(new case(.Subject = 'Subject2', SuppliedEmail='testx@test.com'; Origin="Email to Case'));

caseList.add(new case(Subject = 'Subject3', SuppliedEmail='testy@test.com'; Origin="Email to Case'));

caseList.add(new case(Subject = 'Subject4', SuppliedEmail='testz@test.com'; Origin="Email to Case'));

insert caseList;

 

When the insert caseList is executed, I am expecting the first record to fail due to the addError call that will be executed because it is a duplicate of the case created in the first insert operation. But I am also expecting the other three cases to be created.

 

When I query the Case object for the other 3 cases, my assertion fails because I am expecting 1 record to exist for 'Subject2' but the actual is 0.

 

I didn't think addError caused a rollback on all records in a bulk transaction. Am I missing something?

 

Thanks in advance,

 

Jim