+ Start a Discussion
MicheleMcgeoyMicheleMcgeoy 

Field is not writeable: CampaignMember.CampaignId

I am having trouble changing the CampaignId in CampaignMember object. I get the following error: Field is not writeable: CampaignMember.CampaignId. The error comes from the for loop at the very bottom.

Any ideas???

Thanks, Michele

 

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

    public void insertRecord(){
// Clone the campaign record
        Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
        insert NewCamp;

        Campaign NewCamp1 = [SELECT Type, Status FROM Campaign WHERE Name = 'Test Campaign record'];
        NewCamp1.Type = 'Class Attendance';
        NewCamp1.IsActive = true;      
        NewCamp1.Name = camp.name + camp.startdate + 7;
        NewCamp1.Parentid = camp.id;
        NewCamp1.Startdate = camp.startdate + 7;
        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.End_Time__c = camp.End_Time__c;
        NewCamp1.Description = camp.Description;
        
        update NewCamp1;

// Clone all records in CampaignMember
// Select records in CampaignMember from new campaign
//Create list of members for new campaign above.
 

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;
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
digamber.prasaddigamber.prasad

Hi,

 

Could you please try below code having clone functionality:-

 

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

    public void insertRecord(){
		// Clone the campaign record
		Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
		insert NewCamp;

		Campaign NewCamp1 = [SELECT Type, Status FROM Campaign WHERE Name = 'Test Campaign record'];
		NewCamp1.Type = 'Class Attendance';
		NewCamp1.IsActive = true;      
		NewCamp1.Name = camp.name + camp.startdate + 7;
		NewCamp1.Parentid = camp.id;
		NewCamp1.Startdate = camp.startdate + 7;
		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.End_Time__c = camp.End_Time__c;
		NewCamp1.Description = camp.Description;

		update NewCamp1;

		// Clone all records in CampaignMember
		// Select records in CampaignMember from new campaign
		//Create list of members for new campaign above.


		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
		// This loop below gives me error msg: Field is not writeable: CampaignMember.CampaignId
		for(CampaignMember m : members){
			CampaignMember campMemb = m.clone(false, true);
			campMemb.Campaignid = Newcamp1.id;
			lstCampMember.add(campMemb);
		}

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

 

Please let me know if you have any question.

All Answers

digamber.prasaddigamber.prasad

Hi,

 

In below code, comment says you are updating members, where as you have queried it, so you must update the campaign member records.

 

// Update the database
 insert members;

 

However, if you want to update CampaignId field on campaignmember record, its not updatable, its only createable. Reason being it master in this relationship.

 

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

 

Let me know if you have any question.

MicheleMcgeoyMicheleMcgeoy

so what would the code look like to do a create instead of an update?

There must be some way for me to be able to clone records in the CampaignMember file.

 

Thanks, Michele

digamber.prasaddigamber.prasad

Hi,

 

Could you please try below code having clone functionality:-

 

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

    public void insertRecord(){
		// Clone the campaign record
		Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
		insert NewCamp;

		Campaign NewCamp1 = [SELECT Type, Status FROM Campaign WHERE Name = 'Test Campaign record'];
		NewCamp1.Type = 'Class Attendance';
		NewCamp1.IsActive = true;      
		NewCamp1.Name = camp.name + camp.startdate + 7;
		NewCamp1.Parentid = camp.id;
		NewCamp1.Startdate = camp.startdate + 7;
		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.End_Time__c = camp.End_Time__c;
		NewCamp1.Description = camp.Description;

		update NewCamp1;

		// Clone all records in CampaignMember
		// Select records in CampaignMember from new campaign
		//Create list of members for new campaign above.


		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
		// This loop below gives me error msg: Field is not writeable: CampaignMember.CampaignId
		for(CampaignMember m : members){
			CampaignMember campMemb = m.clone(false, true);
			campMemb.Campaignid = Newcamp1.id;
			lstCampMember.add(campMemb);
		}

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

 

Please let me know if you have any question.

This was selected as the best answer