I have a class method where I am essentially trying to match Campaign members with a campaign based on a mailing ID. In order to do this, I was originally iterating through the list of all mailings returned by a SOQL statement and then within this loop I tried to match the mailings with the campaigns and create campaign member objects based on the matches.
As this was causing the row limit to be reached with the query results, I tried to then in the outer loop iterate through a list of campaign objects and within that loop, use the select statement to get mailing objects using the mailing id from the current campaign in the where clause:
public List<CampaignMember> addCampaignMembers(){
/*Add campaign members to mailings for contact
*/
List<Campaign> campaigns = upsertCampaigns();
List<CampaignMember> mems = new List<CampaignMember>();
List<Mailings__C> allMailings = new List<Mailings__C>();
for(Campaign cmp : campaigns){
//match campaigns and members based on mailing id for each iterations
List<Mailings__C> members = [select Available_Mailing__C, Contact__C from Mailings__C where Available_Mailing__C = cmp.Mailing_ID__c];
for(Mailings__C mc : members){
CampaignMember mem = new CampaignMember();
//CampaignMember mem = new CampaignMember(Campaign = cmp.Campaign_Key__c, Contact= mc.Contact );
mem.CampaignId = cmp.Campaign_Key__c;
mem.ContactId = mc.Contact__C;
mems.add(mem);
}
}
return mems;
}
For some reason, I'm getting the following errors in the developers console with the select statement:
Unexpected token 'cmp.Mailing_ID__c'
expecting a colon, found 'cmp.Mailing_ID__c'
Can variables not be used in this way inside of a select statement? If not, how should the query be structured?
Available_Mailing__C =: cmp.Mailing_ID__c
cmp.Mailing_ID__c
will store at most one value at a time.campaigns
? Even if you don't think you'll ever have it process that many, you can't predict that someone in the future won't try to repurpose your method to do so.