function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MicheleMcgeoyMicheleMcgeoy 

Cloning CampaignMembers

According to the documentation,

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

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_campaignmember.htm

 

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

MaxPowerForceMaxPowerForce

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;
	}
   clonedMembers.add(clone);
   }
insert clonedMembers;