[SalesForce] How to get Campaign member status Value based on Campaign

I have created one vf page which will show list of Campaign member based on particular condition in page block.
In each campaign member I have one status field which I need to update.
The Campaign member status values are depended on each and every Campaign which is parent of Campaign member.
As of now I am getting all status value.
I want to know how I can display relevant status value for each campaign member.
below is my query:

List<CampaignMember> = [Select Contact.firstname,Campaign.name,campaign.status,
                         Contact.lastname,status
                         FROM CampaignMember LIMIT 1000];

Best Answer

You have to query the CampaignMemberStatus object, and create lists based on what you find there. Probably something like this:

Map<Id, SelectOption[]> statusValues = new Map<Id, SelectOption[]>();
for(CampaignMemberStatus record: [SELECT CampaignId, Label FROM CampaignMemberStatus ORDER BY SortOrder ASC]) {
    if(statusValues.containsKey(record.CampaignId)) {
        statusValues.get(record.CampaignId).add(new SelectOption(record.Label, record.Label));
    } else {
        statusValues.put(record.CampaignId, new SelectOption[] { new SelectOption(record.Label, record.Label) });
    }
}

From there, just iterate over the appropriate list from the map, based on the CampaignMember record's CampaignId.

Related Topic