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

Adding records from custom object to Campaign members list

I need to add community members of a community to the campaign members list.

I tried querying in my own way but i cound not execute successfully.


this was the error it was giving


Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Attempted to add a campaign member where either the member id 'null' or the campaign id 'null' is null.: []


I was not sure what is member id whether it is a contact id or community id or any other fields are missing..


I appreciate any help on this.





can you please provide the code?


Thanks Vishal for ur reply.


Here is the code. Pls let me know


CreateCampignMembers(String SubscriptionID, Strign CampignID)

 Subscription_Member__c[] SubscriptionMember_array = [Select id,Subscribed__c,Contact__c,Contact__r.HasOptedOutOfEmail from  Subscription_Member__c where subscription__C='a0QT0000000ndaT'  and Subscribed__c = true and Contact__r.HasOptedOutOfEmail = false ];
 system.debug('size: ' + SubscriptionMember_array.size());
 for (Subscription_Member__c sm:SubscriptionMember_array)
 system.debug('Sub Mem id: ' +;
 system.debug('Sub Mem Subscribed: ' + sm.Subscribed__c);
 system.debug('Sub Mem HasOptedOutOfEmail: ' + sm.Contact__r.HasOptedOutOfEmail);
 List <CampaignMember> CampaignMemberList = new List <CampaignMember>();

 Campaign SelcCampaign = [Select id from  Campaign where id='701T0000000Bw0m'];
 Lead SelcLead = [Select id from  Lead where id='00QT00000092xpB'];
 Contact SelcContact = [Select id,AccountId,Birthdate,Department,Description,Email,Fax,FirstName,LastName,MailingCity,MailingCountry,MailingPostalCode,MailingState,MailingStreet,MobilePhone,Name,OtherCity,OtherCountry from  Contact where id='003T000000yTmZr'];
 //Integer CampignIterator = 0;
 //Id campID = Camp_Array[CampignIterator].Id;
 CampaignMember CampMeb;
 /*for (Subscription_Member__c sma:SubscriptionMember_array)
            system.debug(' END of  the  script  :: In the Loop :: Start ');
   CampMeb = new CampaignMember(Campaign=SelcCampaign,Contact=SelcContact,Status='Sent');
   system.debug(' END of  the  script  :: In the Loop ');
        //CampaignMember CampMeb = new CampaignMember(Campaign='701T0000000Bw0m',Contact=sma.Contact__c);

        //insert CampaignMember_array;
  //insert CampMeb; */

  CampMeb = new CampaignMember(Campaign=SelcCampaign,Contact=SelcContact,Status='Sent', Lead =SelcLead);
  //CampMeb = new CampaignMember(Campaign=SelcCampaign,Status='Sent');
  insert CampMeb;

 system.debug(' END of  the  script ');


In order to be saved Campaign member must have :









Campaign Member Fields

The available fields vary according to which Salesforce Edition you have.

These are the fields (in alphabetical order) that make up a campaign member. Most campaign member fields are derived from the lead or contact record from which the campaign member was created and must be edited from the corresponding lead or contact record. Some of these fields may not be visible or editable depending on your page layout and field-level security settings. (Field-level security is available in Enterprise, Unlimited, and Developer Editions only.)

Field Description
CampaignThe campaign to which the lead or contact belongs.
Campaign Member CurrencyThe default currency for currency amount fields on the campaign member. Amounts display in the campaign member currency and are also converted to the user’s personal currency. Available only for organizations that use multiple currencies.
Campaign Member TypeThe record type of the campaign member, which determines the page layout available for the campaign member. (Read only)
CityCity portion of the lead or contact's address.
Company (Account)The company, if the campaign member is based on a lead, or the account, if the campaign member is based on a contact.
Contact The name of the contact, if the campaign member is based on a contact.
CountryCountry portion of the lead or contact's address.
Created ByUser who created the campaign member. (Read only)
Created DateDate the campaign member was created. (Read only)
DescriptionDescription of the lead or contact.
Do Not CallIndicates if the person does not want to be contacted via telephone.
EmailEmail address of the lead or contact.
Email Opt OutThe person's email address will not be included in mass emails.
FaxFax number of the lead or contact.
Fax Opt OutIndicates if the person has requested not to be included in broadcast faxes.
First NameFirst name of the lead or contact.
First Responded DateDate the campaign member first responded. (Read only)
Last Modified ByUser who last changed the campaign member record. (Read only)
Last Modified DateDate the campaign member record was last modified. (Read only)
Last NameLast name of the lead or contact.
LeadThe name of the lead, if the campaign member is based on a lead.
Lead SourceSource of lead, for example, Advertisement, Partner, or Web. Entry is selected from a picklist of available values, which are set by an administrator. Each picklist value can have up to 40 characters.
MobileMobile phone number for the lead or contact.
PhonePhone number for the lead or contact.
RespondedIndicates if the campaign member has responded.
SalutationThe salutation for addressing the person, for example, Mr., Ms., Dr., or Prof.
State/ProvinceState or province portion of the lead or contact's address.
StatusThe status of the campaign member, for example, sent or responded.
StreetStreet portion of the lead or contact's address.
TitleThe title of the lead or contact, for example, President.
Zip/Postal CodeZip or postal code portion of the lead or contact's address.