+ Start a Discussion

Cloning CampaignMembers

According to the documentation,

CampaignId field on campaignmember record, is not updatable, its only createable.



How would I then copy to a table and then append versus update?


I'm trying this, but getting an error:

List<CampaignMember> Members = [SELECT id, contactid,status from CampaignMember where campaignid = :camp.id ];

// Change campaign ids to the newly cloned campaign id
// This loop below gives me error msg: Field is not writeable: CampaignMember.CampaignId
for(CampaignMember m : members){
   m.Campaignid = Newcamp1.id;
// Update the database
 insert members;


thanks, Michele


CampaignMember serves as a junction between leads/contacts and campaigns.  If you just want to clone the campaign members, you should do something like this.  If you are trying to reparent them, it looks like you cannot so you should just clone them and then delete the old ones.  

List<CampaignMember> clonedMembers = new List<CampaignMember>();

for(CampaignMember m : members){
   CampaignMember clone = new CampaignMember();
   clone.CampaignId = m.CampaignId;
   if (m.ContactId != null) {
	clone.ContactId = m.ContactId;
   else {
	clone.LeadId = m.LeadId;
insert clonedMembers;