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;

    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?

  • 1
    You need a colon. Available_Mailing__C =: cmp.Mailing_ID__c Mar 1, 2018 at 17:48
  • 4
    Before you even think about putting this in production, do yourself a favor and bulkify. As written, you'll run into SOQL governor limits that may prevent you from performing critical data updates.
    – sfdcfox
    Mar 1, 2018 at 17:50
  • @sfdcfox What needs to be bulkified? cmp.Mailing_ID__c will store at most one value at a time. Mar 1, 2018 at 21:20
  • 1
    @loremIpsum1771 You have a query in a loop. What happens if you have 200 records in 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.
    – sfdcfox
    Mar 1, 2018 at 21:35

1 Answer 1


The functionality you're looking for is called Apex binding. It is covered in both the Trailhead module on SOQL and the Apex Developer Guide reference on SOQL.

I'd really recommend you take advantage of some of these excellent introductory resources to the platform. Your last few questions suggest you're having a hard time with some of the basics, which is understandable - Salesforce is a large, very complex platform and it's different in some key ways from most other development environments. Fortunately, Salesforce also has excellent resources to help you get up to speed. I'd really urge you to take full advantage.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .