• darpan
  • 0 Points
  • Member since 2013

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1

I have written a trigger on the Campaign Member object that gives users access to our subscription service when their campagn status changes to 'Free Trial'. The trigger works without fail but the test is failing because of this error: 


System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []


Here is my code:

public with sharing class Test_Trial_Campaign {
	static testMethod void test(){
		list<Id> IdLst = new list<Id>();
		list<CampaignMember> cMemL = new list<CampaignMember>();
		Campaign testC = new Campaign(isActive = true, ActualCost = 0, Name = 'SystemTest');
		insert testC;
		CampaignMemberStatus status = new CampaignMemberStatus(CampaignId = testC.Id, HasResponded = true, label = 'Free Trial', SortOrder = 2);
		insert new list<CampaignMemberStatus>{status};
		for (Integer i=0; i<10; i++){
			Lead testL = new Lead(FirstName = 'Test', LastName = 'Testerino'+i, email = 'test@testing.co'+i, Trial_Product__c = 'SUB-RTE-01');
			insert testL;
			CampaignMember mem = new CampaignMember(CampaignId = testC.Id, LeadId = testL.Id, Status = 'Free Trial');
			insert mem;
			CampaignMember testerino = [SELECT Id, Status from CampaignMember where id =:mem.Id];

		list<Lead> lList = [SELECT Id, isConverted From Lead Where Id IN: IdLst];
		for (Lead l: lList){
			system.assert(l.IsConverted == true);


It fails when attempting to insert the CampaignMemberStatus. I have tried multiple workarounds, all of which have failed. Any insight is greatly apprechiated.