• michelemcgeoy.ax1914
  • NEWBIE
  • 10 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 9
    Replies
I'm getting the error "Invalid type: CloneClassAttendance" in test code bolded below.
We're supposed to go live tomorrow morning and I'm stuck... don't mean to sound too desperate... but I am :(
Thanks, Michele

Here's the test code:
@isTest
private class CloneClassAttendanceTest {
//CloneClassAttendance myTest = new CloneClassAttendance();
//Public CloneClassAttendance(){}
// above line gives error  Invalid constructor name:
    static testMethod void myUnitTest() {   
        // Add test contact
        Contact c = new Contact(LastName='Test');
        insert c;
        // Add test campaign
        Campaign cp = new Campaign(Name='TestCampaign');
        insert cp;
        // Add campaign member status
        CampaignMemberStatus cms = new CampaignMemberStatus( CampaignId = cp.Id, Label = 'Attended', SortOrder = 3);
        insert cms;
        // Add campaign member
        CampaignMember cm = new CampaignMember( CampaignId = cp.Id, Status = 'Attended', ContactId = c.Id);
        insert cm;
     
         //Page reference change VF_PageName with the name of your visualforce page
         PageReference cloneAttendance = Page.CloneClassAttendance;
         //Set the current page
        Test.setCurrentPage(cloneAttendance);
         //Set the id param on the page to the campaign id
         ApexPages.currentPage().getParameters().put('id', cp.Id);
         //Instantiate standard controller with the campaign record
         ApexPages.StandardController s = new ApexPages.StandardController(cp);
         //Instantiate extension class passing the standard controller
         //Line below gives error: Invalid type: CloneClassAttendance       
         CloneClassAttendance cca = new CloneClassAttendance(s);

         //Call your extensions methods here...
         cca.InsertRecord();
    }
}

Here's the Apex code I'm trying to test:
public class CloneClassAttendance
{
    private final Campaign theCampaign;

    public CloneClassAttendance( ApexPages.StandardController stdController )
    {
        theCampaign = (Campaign)stdController.getRecord();
    }

    //  returns the day-of-week for the given date in 2001 - 2099
    //  this works because 2001 started on a Monday
    //  0 = Sunday, ... 6 = Saturday
    private static Integer dayOfWeek( Date theDate )
    {
        Integer theYear = theDate.year() - 2001;
        return Math.mod( theYear + theYear/4 + theDate.dayOfYear(), 7 );
    }

    public void insertRecord()
    {
        Map<Date,Campaign> map_newCampaigns = new Map<Date,Campaign>();

        Date newDate = theCampaign.CloneStart__c;
        while ( newDate <= theCampaign.CloneEnd__c )
        {
            map_newCampaigns.put
            (   newDate,
                new Campaign
                (   Type                = 'Class Attendance',
                    IsActive            = true,
                    StartDate           = newDate,
                    EndDate             = newDate,
                    ParentId            = theCampaign.Id,
                    Name                = theCampaign.Name + '-' + newDate.format(),
                    Description         = theCampaign.Description,
                    Teacher__c          = theCampaign.Teacher__c,
                    Day_of_Week__c      = theCampaign.Day_of_Week__c,
                    Hours_Per_Class__c  = theCampaign.Hours_Per_Class__c,
                    Location__c         = theCampaign.Location__c,
                    Start_Time__c       = theCampaign.Start_Time__c,
                    End_Time__c         = theCampaign.End_Time__c,
                    Waitlist__c         = theCampaign.Waitlist__c,
                    Max_Capacity__c     = theCampaign.Max_Capacity__c,
                    Status              = 'Completed'
                )
            );
            newDate = newDate.addDays( 7 );
        }
        insert map_newCampaigns.values();

        List<CampaignMemberStatus>  list_newCampaignMemberStatus    = new List<CampaignMemberStatus>();
        List<CampaignMember>        list_newCampaignMembers         = new List<CampaignMember>();

        newDate = theCampaign.CloneStart__c;  
        while ( newDate <= theCampaign.CloneEnd__c )
        {
            Campaign newCampaign = map_newCampaigns.get( newDate );

            for ( CampaignMemberStatus status :
                [   SELECT  Id, HasResponded, IsDefault, Label, SortOrder
                    FROM    CampaignMemberStatus
                    WHERE   (   CampaignId = :theCampaign.Id
                            AND IsDeleted = false
                            )
                ]
                )
            {

                list_newCampaignMemberStatus.add
                (   new CampaignMemberStatus
                    (   CampaignId      = newCampaign.Id,
                        HasResponded    = status.HasResponded,
                        IsDefault       = status.IsDefault,
                        Label           = status.Label,
// the following line is a workaround due to the fact that when the campaign is cloned it automatically
// add CampaignMemberStatus of Sent and Received with sortorder 1 and 2. This was creating a duplicate bug.
                        SortOrder       = status.SortOrder +2
                    )
                );
System.debug( 'CampaignMemberStatus: CampaignId= ' + newCampaign.Id +' Label = ' + status.Label + ', SortOrder = ' + status.SortOrder );
            }

            for ( CampaignMember member :
                [   SELECT  Id, ContactId, Status
                    FROM    CampaignMember
                    WHERE   CampaignId = :theCampaign.Id
                ]
                )
            {
                list_newCampaignMembers.add
                (   new CampaignMember
                    (   CampaignId      = newCampaign.Id,
                        ContactId       = member.ContactId,
                        Status          = member.Status
                    )
                );
            }

            newDate = newDate.addDays( 7 );
        }

        insert list_newCampaignMemberStatus;
        insert list_newCampaignMembers;
    }
}

Here's the Visual Force page:
<apex:page standardController="Campaign" extensions="CloneClassAttendance" action="{!insertRecord}">
  <h1>Cloning Attendance</h1>
  <p> Class name is {! campaign.name} </p>
  <p> Class id is:   {! campaign.id} </p>
  <p> Class start date is:   {! campaign.startdate} </p>
  <p> Class end date is:   {! campaign.enddate} </p>
  <p> Next week is: {! campaign.startdate+7} </p>
  <p> Class Status is {! campaign.status} </p>
  <p> Class teacher is {! campaign.teacher__c} </p>
  <p> Class DOW is {! campaign.Day_of_Week__c} </p>
  <p> Class Hours_Per_Class__c is {! campaign.Hours_Per_Class__c} </p>
  <p> Class Location is {! campaign.Location__c} </p>
  <p> Class Start time is {! campaign.Start_Time__c} </p>
  <p> Class End time is {! campaign.End_Time__c} </p>
  <p> Class Description is {! campaign.Description} </p>
  <p> Class Waitlist is {! campaign.Waitlist__c} </p>
  <p> Class Max_Capacity is {! campaign.Max_Capacity__c} </p>
  <p> Clone start date is:   {! campaign.clonestart__c} </p>
  <p> Class end date is:   {! campaign.cloneend__c} </p>
</apex:page>
I've got code that works in Developer mode and now need to bring it into production... of course we're supposed to go live tomorrow...
I'm very new to Apex so please excuse the basic question. My code gets called from a visual force page and when I run the test code it can't seem to find the method. I get the error: Method does not exist or incorrect signature

Here is the test code:
@isTest
private class CloneClassAttendanceTest {
//Public CloneClassAttendance(){}
// above line gives error  Invalid constructor name:
    static testMethod void myUnitTest() {
       
        // Add test contact
        Contact c = new Contact(LastName='Test');
        insert c;
        // Add test campaign
        Campaign cp = new Campaign(Name='TestCampaign');
        insert cp;
        // Add campaign member status
        CampaignMemberStatus cms = new CampaignMemberStatus( CampaignId = cp.Id, Label = 'Attended', SortOrder = 3);
        insert cms;
        // Add campaign member
        CampaignMember cm = new CampaignMember( CampaignId = cp.Id, Status = 'Attended', ContactId = c.Id);
        insert cm;
       
        // Call CloneClassAttendance
        InsertRecord();
        // Above line gives error: Method does not exist or incorrect signature
    }
}

Here is that Apex Class:
public class CloneClassAttendance
{
    private final Campaign theCampaign;

    public CloneClassAttendance( ApexPages.StandardController stdController )
    {
        theCampaign = (Campaign)stdController.getRecord();
    }

    //  returns the day-of-week for the given date in 2001 - 2099
    //  this works because 2001 started on a Monday
    //  0 = Sunday, ... 6 = Saturday
    private static Integer dayOfWeek( Date theDate )
    {
        Integer theYear = theDate.year() - 2001;
        return Math.mod( theYear + theYear/4 + theDate.dayOfYear(), 7 );
    }

    public void insertRecord()
    {
        Map<Date,Campaign> map_newCampaigns = new Map<Date,Campaign>();

        Date newDate = theCampaign.CloneStart__c;
        while ( newDate <= theCampaign.CloneEnd__c )
        {
            map_newCampaigns.put
            (   newDate,
                new Campaign
                (   Type                = 'Class Attendance',
                    IsActive            = true,
                    StartDate           = newDate,
                    EndDate             = newDate,
                    ParentId            = theCampaign.Id,
                    Name                = theCampaign.Name + '-' + newDate.format(),
                    Description         = theCampaign.Description,
                    Teacher__c          = theCampaign.Teacher__c,
                    Day_of_Week__c      = theCampaign.Day_of_Week__c,
                    Hours_Per_Class__c  = theCampaign.Hours_Per_Class__c,
                    Location__c         = theCampaign.Location__c,
                    Start_Time__c       = theCampaign.Start_Time__c,
                    End_Time__c         = theCampaign.End_Time__c,
                    Waitlist__c         = theCampaign.Waitlist__c,
                    Max_Capacity__c     = theCampaign.Max_Capacity__c,
                    Status              = 'Completed'
                )
            );
            newDate = newDate.addDays( 7 );
        }
        insert map_newCampaigns.values();

        List<CampaignMemberStatus>  list_newCampaignMemberStatus    = new List<CampaignMemberStatus>();
        List<CampaignMember>        list_newCampaignMembers         = new List<CampaignMember>();

        newDate = theCampaign.CloneStart__c;  
        while ( newDate <= theCampaign.CloneEnd__c )
        {
            Campaign newCampaign = map_newCampaigns.get( newDate );

            for ( CampaignMemberStatus status :
                [   SELECT  Id, HasResponded, IsDefault, Label, SortOrder
                    FROM    CampaignMemberStatus
                    WHERE   (   CampaignId = :theCampaign.Id
                            AND IsDeleted = false
                            )
                ]
                )
            {

                list_newCampaignMemberStatus.add
                (   new CampaignMemberStatus
                    (   CampaignId      = newCampaign.Id,
                        HasResponded    = status.HasResponded,
                        IsDefault       = status.IsDefault,
                        Label           = status.Label,
// the following line is a workaround due to the fact that when the campaign is cloned it automatically
// add CampaignMemberStatus of Sent and Received with sortorder 1 and 2. This was creating a duplicate bug.
                        SortOrder       = status.SortOrder +2
                    )
                );
System.debug( 'CampaignMemberStatus: CampaignId= ' + newCampaign.Id +' Label = ' + status.Label + ', SortOrder = ' + status.SortOrder );
            }

            for ( CampaignMember member :
                [   SELECT  Id, ContactId, Status
                    FROM    CampaignMember
                    WHERE   CampaignId = :theCampaign.Id
                ]
                )
            {
                list_newCampaignMembers.add
                (   new CampaignMember
                    (   CampaignId      = newCampaign.Id,
                        ContactId       = member.ContactId,
                        Status          = member.Status
                    )
                );
            }

            newDate = newDate.addDays( 7 );
        }

        insert list_newCampaignMemberStatus;
        insert list_newCampaignMembers;
    }
}

Here is the VisualForce Page:
<apex:page standardController="Campaign" extensions="CloneClassAttendance" action="{!insertRecord}">

  <h1>Cloning Attendance</h1>
  <p> Class name is {! campaign.name} </p>
  <p> Class id is:   {! campaign.id} </p>
  <p> Class start date is:   {! campaign.startdate} </p>
  <p> Class end date is:   {! campaign.enddate} </p>
  <p> Next week is: {! campaign.startdate+7} </p>
  <p> Class Status is {! campaign.status} </p>
  <p> Class teacher is {! campaign.teacher__c} </p>
  <p> Class DOW is {! campaign.Day_of_Week__c} </p>
  <p> Class Hours_Per_Class__c is {! campaign.Hours_Per_Class__c} </p>
  <p> Class Location is {! campaign.Location__c} </p>
  <p> Class Start time is {! campaign.Start_Time__c} </p>
  <p> Class End time is {! campaign.End_Time__c} </p>
  <p> Class Description is {! campaign.Description} </p>
  <p> Class Waitlist is {! campaign.Waitlist__c} </p>
  <p> Class Max_Capacity is {! campaign.Max_Capacity__c} </p>
  <p> Clone start date is:   {! campaign.clonestart__c} </p>
  <p> Class end date is:   {! campaign.cloneend__c} </p>
</apex:page>
I am trying to Clone a campaign with all it's members. I am successful in cloning the campaign and all of it's members, but when I try and close the MemberStatus object i get:
error: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: [] Error is in expression '{!insertRecord}' in component <apex:page> in page cloneclassattendance

I'm very new to Apex, so sorry if this seems like a basic question. Is there a way that I can write the contents of the list to an external table so I can see the data and try and trace the error?

Here is the code below:

public class CloneClassAttendance {
    private final Campaign camp;
   
    public CloneClassAttendance(ApexPages.StandardController stdController){
        this.camp = (Campaign)stdController.getRecord();
    }

   public void insertRecord(){
        // TODO - make sure Start date and DOW are same
        //If (DAY_IN_WEEK(Camp.startdate) = 2) {
        // do something
        //} else {
        // do nothing
        //}

        Date NewDate = Camp.Startdate;
        while (NewDate <= camp.enddate) { 
            // Clone the campaign record
            Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
            insert NewCamp;
            // If you add new fields that you want to clone, you also have to add to the page. Not sure why...
            Campaign NewCamp1 = [SELECT Id, Type, Status FROM Campaign WHERE Name = 'Test Campaign record'];
            NewCamp1.Type = 'Class Attendance';
            NewCamp1.IsActive = true;     
            NewCamp1.Name = camp.name + +'-'+NewDate.format();
            NewCamp1.Parentid = camp.id;
            NewCamp1.Startdate = NewDate;
            NewCamp1.enddate = NewDate;
            NewCamp1.teacher__c = camp.teacher__c;
            NewCamp1.Day_of_Week__c = camp.Day_of_Week__c;
            NewCamp1.Hours_Per_Class__c = camp.Hours_Per_Class__c;
            NewCamp1.Location__c = camp.Location__c;
            NewCamp1.Start_Time__c = camp.Start_Time__c;
            NewCamp1.End_Time__c = camp.End_Time__c;
            NewCamp1.Description = camp.Description;
            NewCamp1.Waitlist__c = camp.Waitlist__c;
            NewCamp1.Max_Capacity__c = camp.Max_Capacity__c;
            update NewCamp1;
           
            // Clone all records in CampaignMemberStatus
            // Select records in CampaignMemberStatus from new campaign
            List<CampaignMemberStatus> MembersStatus = [SELECT id, campaignid, label, SortOrder, HASRESPONDED,isDefault
            from CampaignMemberStatus where campaignid = :camp.id ];
            List<CampaignMemberStatus> lstCampMemberStatus = new List<CampaignMemberStatus>();
            // Change campaign ids to the newly cloned campaign id
            for(CampaignMemberStatus ms : MembersStatus){
                CampaignMemberStatus campMembStatus = ms.clone(false, true);
                campMembStatus.Campaignid = Newcamp1.id;
                lstCampMemberStatus.add(campMembStatus);
            }

            if(lstCampMemberStatus.size() > 0)
            // This is where error occurs. Newcamp1.id should be set to the campaign that was just added. It seems to work fine below. But the error
            // says "duplicates value on record with id: <unknown>"
            insert lstCampMemberStatus;
                

            // Clone all records in CampaignMember
            // Select records in CampaignMember from new campaign
            //Create list of members for new campaign above.
            //Code below tested and works properly

            List<CampaignMember> Members = [SELECT id, contactid,status from CampaignMember where campaignid = :camp.id ];
            List<CampaignMember> lstCampMember = new List<CampaignMember>();
            // Change campaign ids to the newly cloned campaign id
            for(CampaignMember m : Members){
            CampaignMember campMemb = m.clone(false, true);
            campMemb.Campaignid = Newcamp1.id;
            lstCampMember.add(campMemb);
            }

            if(lstCampMember.size() > 0)
            insert lstCampMember;

            Newdate = NewDate + 7;  

        }
    }  
}

Thanks, Michele
I'm getting the error "Invalid type: CloneClassAttendance" in test code bolded below.
We're supposed to go live tomorrow morning and I'm stuck... don't mean to sound too desperate... but I am :(
Thanks, Michele

Here's the test code:
@isTest
private class CloneClassAttendanceTest {
//CloneClassAttendance myTest = new CloneClassAttendance();
//Public CloneClassAttendance(){}
// above line gives error  Invalid constructor name:
    static testMethod void myUnitTest() {   
        // Add test contact
        Contact c = new Contact(LastName='Test');
        insert c;
        // Add test campaign
        Campaign cp = new Campaign(Name='TestCampaign');
        insert cp;
        // Add campaign member status
        CampaignMemberStatus cms = new CampaignMemberStatus( CampaignId = cp.Id, Label = 'Attended', SortOrder = 3);
        insert cms;
        // Add campaign member
        CampaignMember cm = new CampaignMember( CampaignId = cp.Id, Status = 'Attended', ContactId = c.Id);
        insert cm;
     
         //Page reference change VF_PageName with the name of your visualforce page
         PageReference cloneAttendance = Page.CloneClassAttendance;
         //Set the current page
        Test.setCurrentPage(cloneAttendance);
         //Set the id param on the page to the campaign id
         ApexPages.currentPage().getParameters().put('id', cp.Id);
         //Instantiate standard controller with the campaign record
         ApexPages.StandardController s = new ApexPages.StandardController(cp);
         //Instantiate extension class passing the standard controller
         //Line below gives error: Invalid type: CloneClassAttendance       
         CloneClassAttendance cca = new CloneClassAttendance(s);

         //Call your extensions methods here...
         cca.InsertRecord();
    }
}

Here's the Apex code I'm trying to test:
public class CloneClassAttendance
{
    private final Campaign theCampaign;

    public CloneClassAttendance( ApexPages.StandardController stdController )
    {
        theCampaign = (Campaign)stdController.getRecord();
    }

    //  returns the day-of-week for the given date in 2001 - 2099
    //  this works because 2001 started on a Monday
    //  0 = Sunday, ... 6 = Saturday
    private static Integer dayOfWeek( Date theDate )
    {
        Integer theYear = theDate.year() - 2001;
        return Math.mod( theYear + theYear/4 + theDate.dayOfYear(), 7 );
    }

    public void insertRecord()
    {
        Map<Date,Campaign> map_newCampaigns = new Map<Date,Campaign>();

        Date newDate = theCampaign.CloneStart__c;
        while ( newDate <= theCampaign.CloneEnd__c )
        {
            map_newCampaigns.put
            (   newDate,
                new Campaign
                (   Type                = 'Class Attendance',
                    IsActive            = true,
                    StartDate           = newDate,
                    EndDate             = newDate,
                    ParentId            = theCampaign.Id,
                    Name                = theCampaign.Name + '-' + newDate.format(),
                    Description         = theCampaign.Description,
                    Teacher__c          = theCampaign.Teacher__c,
                    Day_of_Week__c      = theCampaign.Day_of_Week__c,
                    Hours_Per_Class__c  = theCampaign.Hours_Per_Class__c,
                    Location__c         = theCampaign.Location__c,
                    Start_Time__c       = theCampaign.Start_Time__c,
                    End_Time__c         = theCampaign.End_Time__c,
                    Waitlist__c         = theCampaign.Waitlist__c,
                    Max_Capacity__c     = theCampaign.Max_Capacity__c,
                    Status              = 'Completed'
                )
            );
            newDate = newDate.addDays( 7 );
        }
        insert map_newCampaigns.values();

        List<CampaignMemberStatus>  list_newCampaignMemberStatus    = new List<CampaignMemberStatus>();
        List<CampaignMember>        list_newCampaignMembers         = new List<CampaignMember>();

        newDate = theCampaign.CloneStart__c;  
        while ( newDate <= theCampaign.CloneEnd__c )
        {
            Campaign newCampaign = map_newCampaigns.get( newDate );

            for ( CampaignMemberStatus status :
                [   SELECT  Id, HasResponded, IsDefault, Label, SortOrder
                    FROM    CampaignMemberStatus
                    WHERE   (   CampaignId = :theCampaign.Id
                            AND IsDeleted = false
                            )
                ]
                )
            {

                list_newCampaignMemberStatus.add
                (   new CampaignMemberStatus
                    (   CampaignId      = newCampaign.Id,
                        HasResponded    = status.HasResponded,
                        IsDefault       = status.IsDefault,
                        Label           = status.Label,
// the following line is a workaround due to the fact that when the campaign is cloned it automatically
// add CampaignMemberStatus of Sent and Received with sortorder 1 and 2. This was creating a duplicate bug.
                        SortOrder       = status.SortOrder +2
                    )
                );
System.debug( 'CampaignMemberStatus: CampaignId= ' + newCampaign.Id +' Label = ' + status.Label + ', SortOrder = ' + status.SortOrder );
            }

            for ( CampaignMember member :
                [   SELECT  Id, ContactId, Status
                    FROM    CampaignMember
                    WHERE   CampaignId = :theCampaign.Id
                ]
                )
            {
                list_newCampaignMembers.add
                (   new CampaignMember
                    (   CampaignId      = newCampaign.Id,
                        ContactId       = member.ContactId,
                        Status          = member.Status
                    )
                );
            }

            newDate = newDate.addDays( 7 );
        }

        insert list_newCampaignMemberStatus;
        insert list_newCampaignMembers;
    }
}

Here's the Visual Force page:
<apex:page standardController="Campaign" extensions="CloneClassAttendance" action="{!insertRecord}">
  <h1>Cloning Attendance</h1>
  <p> Class name is {! campaign.name} </p>
  <p> Class id is:   {! campaign.id} </p>
  <p> Class start date is:   {! campaign.startdate} </p>
  <p> Class end date is:   {! campaign.enddate} </p>
  <p> Next week is: {! campaign.startdate+7} </p>
  <p> Class Status is {! campaign.status} </p>
  <p> Class teacher is {! campaign.teacher__c} </p>
  <p> Class DOW is {! campaign.Day_of_Week__c} </p>
  <p> Class Hours_Per_Class__c is {! campaign.Hours_Per_Class__c} </p>
  <p> Class Location is {! campaign.Location__c} </p>
  <p> Class Start time is {! campaign.Start_Time__c} </p>
  <p> Class End time is {! campaign.End_Time__c} </p>
  <p> Class Description is {! campaign.Description} </p>
  <p> Class Waitlist is {! campaign.Waitlist__c} </p>
  <p> Class Max_Capacity is {! campaign.Max_Capacity__c} </p>
  <p> Clone start date is:   {! campaign.clonestart__c} </p>
  <p> Class end date is:   {! campaign.cloneend__c} </p>
</apex:page>
I've got code that works in Developer mode and now need to bring it into production... of course we're supposed to go live tomorrow...
I'm very new to Apex so please excuse the basic question. My code gets called from a visual force page and when I run the test code it can't seem to find the method. I get the error: Method does not exist or incorrect signature

Here is the test code:
@isTest
private class CloneClassAttendanceTest {
//Public CloneClassAttendance(){}
// above line gives error  Invalid constructor name:
    static testMethod void myUnitTest() {
       
        // Add test contact
        Contact c = new Contact(LastName='Test');
        insert c;
        // Add test campaign
        Campaign cp = new Campaign(Name='TestCampaign');
        insert cp;
        // Add campaign member status
        CampaignMemberStatus cms = new CampaignMemberStatus( CampaignId = cp.Id, Label = 'Attended', SortOrder = 3);
        insert cms;
        // Add campaign member
        CampaignMember cm = new CampaignMember( CampaignId = cp.Id, Status = 'Attended', ContactId = c.Id);
        insert cm;
       
        // Call CloneClassAttendance
        InsertRecord();
        // Above line gives error: Method does not exist or incorrect signature
    }
}

Here is that Apex Class:
public class CloneClassAttendance
{
    private final Campaign theCampaign;

    public CloneClassAttendance( ApexPages.StandardController stdController )
    {
        theCampaign = (Campaign)stdController.getRecord();
    }

    //  returns the day-of-week for the given date in 2001 - 2099
    //  this works because 2001 started on a Monday
    //  0 = Sunday, ... 6 = Saturday
    private static Integer dayOfWeek( Date theDate )
    {
        Integer theYear = theDate.year() - 2001;
        return Math.mod( theYear + theYear/4 + theDate.dayOfYear(), 7 );
    }

    public void insertRecord()
    {
        Map<Date,Campaign> map_newCampaigns = new Map<Date,Campaign>();

        Date newDate = theCampaign.CloneStart__c;
        while ( newDate <= theCampaign.CloneEnd__c )
        {
            map_newCampaigns.put
            (   newDate,
                new Campaign
                (   Type                = 'Class Attendance',
                    IsActive            = true,
                    StartDate           = newDate,
                    EndDate             = newDate,
                    ParentId            = theCampaign.Id,
                    Name                = theCampaign.Name + '-' + newDate.format(),
                    Description         = theCampaign.Description,
                    Teacher__c          = theCampaign.Teacher__c,
                    Day_of_Week__c      = theCampaign.Day_of_Week__c,
                    Hours_Per_Class__c  = theCampaign.Hours_Per_Class__c,
                    Location__c         = theCampaign.Location__c,
                    Start_Time__c       = theCampaign.Start_Time__c,
                    End_Time__c         = theCampaign.End_Time__c,
                    Waitlist__c         = theCampaign.Waitlist__c,
                    Max_Capacity__c     = theCampaign.Max_Capacity__c,
                    Status              = 'Completed'
                )
            );
            newDate = newDate.addDays( 7 );
        }
        insert map_newCampaigns.values();

        List<CampaignMemberStatus>  list_newCampaignMemberStatus    = new List<CampaignMemberStatus>();
        List<CampaignMember>        list_newCampaignMembers         = new List<CampaignMember>();

        newDate = theCampaign.CloneStart__c;  
        while ( newDate <= theCampaign.CloneEnd__c )
        {
            Campaign newCampaign = map_newCampaigns.get( newDate );

            for ( CampaignMemberStatus status :
                [   SELECT  Id, HasResponded, IsDefault, Label, SortOrder
                    FROM    CampaignMemberStatus
                    WHERE   (   CampaignId = :theCampaign.Id
                            AND IsDeleted = false
                            )
                ]
                )
            {

                list_newCampaignMemberStatus.add
                (   new CampaignMemberStatus
                    (   CampaignId      = newCampaign.Id,
                        HasResponded    = status.HasResponded,
                        IsDefault       = status.IsDefault,
                        Label           = status.Label,
// the following line is a workaround due to the fact that when the campaign is cloned it automatically
// add CampaignMemberStatus of Sent and Received with sortorder 1 and 2. This was creating a duplicate bug.
                        SortOrder       = status.SortOrder +2
                    )
                );
System.debug( 'CampaignMemberStatus: CampaignId= ' + newCampaign.Id +' Label = ' + status.Label + ', SortOrder = ' + status.SortOrder );
            }

            for ( CampaignMember member :
                [   SELECT  Id, ContactId, Status
                    FROM    CampaignMember
                    WHERE   CampaignId = :theCampaign.Id
                ]
                )
            {
                list_newCampaignMembers.add
                (   new CampaignMember
                    (   CampaignId      = newCampaign.Id,
                        ContactId       = member.ContactId,
                        Status          = member.Status
                    )
                );
            }

            newDate = newDate.addDays( 7 );
        }

        insert list_newCampaignMemberStatus;
        insert list_newCampaignMembers;
    }
}

Here is the VisualForce Page:
<apex:page standardController="Campaign" extensions="CloneClassAttendance" action="{!insertRecord}">

  <h1>Cloning Attendance</h1>
  <p> Class name is {! campaign.name} </p>
  <p> Class id is:   {! campaign.id} </p>
  <p> Class start date is:   {! campaign.startdate} </p>
  <p> Class end date is:   {! campaign.enddate} </p>
  <p> Next week is: {! campaign.startdate+7} </p>
  <p> Class Status is {! campaign.status} </p>
  <p> Class teacher is {! campaign.teacher__c} </p>
  <p> Class DOW is {! campaign.Day_of_Week__c} </p>
  <p> Class Hours_Per_Class__c is {! campaign.Hours_Per_Class__c} </p>
  <p> Class Location is {! campaign.Location__c} </p>
  <p> Class Start time is {! campaign.Start_Time__c} </p>
  <p> Class End time is {! campaign.End_Time__c} </p>
  <p> Class Description is {! campaign.Description} </p>
  <p> Class Waitlist is {! campaign.Waitlist__c} </p>
  <p> Class Max_Capacity is {! campaign.Max_Capacity__c} </p>
  <p> Clone start date is:   {! campaign.clonestart__c} </p>
  <p> Class end date is:   {! campaign.cloneend__c} </p>
</apex:page>
I am trying to Clone a campaign with all it's members. I am successful in cloning the campaign and all of it's members, but when I try and close the MemberStatus object i get:
error: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: [] Error is in expression '{!insertRecord}' in component <apex:page> in page cloneclassattendance

I'm very new to Apex, so sorry if this seems like a basic question. Is there a way that I can write the contents of the list to an external table so I can see the data and try and trace the error?

Here is the code below:

public class CloneClassAttendance {
    private final Campaign camp;
   
    public CloneClassAttendance(ApexPages.StandardController stdController){
        this.camp = (Campaign)stdController.getRecord();
    }

   public void insertRecord(){
        // TODO - make sure Start date and DOW are same
        //If (DAY_IN_WEEK(Camp.startdate) = 2) {
        // do something
        //} else {
        // do nothing
        //}

        Date NewDate = Camp.Startdate;
        while (NewDate <= camp.enddate) { 
            // Clone the campaign record
            Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
            insert NewCamp;
            // If you add new fields that you want to clone, you also have to add to the page. Not sure why...
            Campaign NewCamp1 = [SELECT Id, Type, Status FROM Campaign WHERE Name = 'Test Campaign record'];
            NewCamp1.Type = 'Class Attendance';
            NewCamp1.IsActive = true;     
            NewCamp1.Name = camp.name + +'-'+NewDate.format();
            NewCamp1.Parentid = camp.id;
            NewCamp1.Startdate = NewDate;
            NewCamp1.enddate = NewDate;
            NewCamp1.teacher__c = camp.teacher__c;
            NewCamp1.Day_of_Week__c = camp.Day_of_Week__c;
            NewCamp1.Hours_Per_Class__c = camp.Hours_Per_Class__c;
            NewCamp1.Location__c = camp.Location__c;
            NewCamp1.Start_Time__c = camp.Start_Time__c;
            NewCamp1.End_Time__c = camp.End_Time__c;
            NewCamp1.Description = camp.Description;
            NewCamp1.Waitlist__c = camp.Waitlist__c;
            NewCamp1.Max_Capacity__c = camp.Max_Capacity__c;
            update NewCamp1;
           
            // Clone all records in CampaignMemberStatus
            // Select records in CampaignMemberStatus from new campaign
            List<CampaignMemberStatus> MembersStatus = [SELECT id, campaignid, label, SortOrder, HASRESPONDED,isDefault
            from CampaignMemberStatus where campaignid = :camp.id ];
            List<CampaignMemberStatus> lstCampMemberStatus = new List<CampaignMemberStatus>();
            // Change campaign ids to the newly cloned campaign id
            for(CampaignMemberStatus ms : MembersStatus){
                CampaignMemberStatus campMembStatus = ms.clone(false, true);
                campMembStatus.Campaignid = Newcamp1.id;
                lstCampMemberStatus.add(campMembStatus);
            }

            if(lstCampMemberStatus.size() > 0)
            // This is where error occurs. Newcamp1.id should be set to the campaign that was just added. It seems to work fine below. But the error
            // says "duplicates value on record with id: <unknown>"
            insert lstCampMemberStatus;
                

            // Clone all records in CampaignMember
            // Select records in CampaignMember from new campaign
            //Create list of members for new campaign above.
            //Code below tested and works properly

            List<CampaignMember> Members = [SELECT id, contactid,status from CampaignMember where campaignid = :camp.id ];
            List<CampaignMember> lstCampMember = new List<CampaignMember>();
            // Change campaign ids to the newly cloned campaign id
            for(CampaignMember m : Members){
            CampaignMember campMemb = m.clone(false, true);
            campMemb.Campaignid = Newcamp1.id;
            lstCampMember.add(campMemb);
            }

            if(lstCampMember.size() > 0)
            insert lstCampMember;

            Newdate = NewDate + 7;  

        }
    }  
}

Thanks, Michele