You need to sign in to do that
Don't have an account?
Jonathan Wolff 7
Query Campaign members in Apex class for Visualforce
Hello, I want to build a List button, that does create a pdf for every member of a campaign that is selected in List view. I cant achive to query the campaign member name (I am very unexperienced in this.) Could you change my code so so The repeat function shows a pdf side for every member in the selected campaign?
Visualforce
<apex:page standardController="Account" renderAs="pdf" recordSetVar="accounts" extensions="AccountListPDFController"> <apex:repeat value="{!accounts}" var="Account">
<div style="page-break-after:always;">
<h1>Welcome to APP!</h1>
<p>Thank you, <b><apex:outputText value=" {!Account.Name}"/>
</b>, for working with APP.</p> <p>Your account details are:</p> <table>
<tr><th>Account Name</th> <td>
<apex:outputText value="{!Account.Name}"/>
</td> </tr> <tr>
<th>Account Rep</th> <td><apex:outputText value="{!Account.Owner.Name}"/></td> </tr>
<tr><th> Customer Since</th> <td><apex:outputText value="{0,date,long}"> <apex:param value="{!Account.CreatedDate}"/>
</apex:outputText></td>
</tr>
</table>
</div>
</apex:repeat>
</apex:page>
APEX
public class CampaignListPDFController {
public List<Campaign> campaigns{get;private set;}
public List<CampaignMember> campaignMembers { get; private set; }
public CampaignListPDFController(ApexPages.StandardSetController stdSetController){
campaigns = (List<Campaign>) stdSetController.getSelected();
campaignMembers = [
SELECT Campaign.Name, Campaign.Startdate, Campaign.Type FROM CampaignMember
];
}
}
Visualforce
<apex:page standardController="Account" renderAs="pdf" recordSetVar="accounts" extensions="AccountListPDFController"> <apex:repeat value="{!accounts}" var="Account">
<div style="page-break-after:always;">
<h1>Welcome to APP!</h1>
<p>Thank you, <b><apex:outputText value=" {!Account.Name}"/>
</b>, for working with APP.</p> <p>Your account details are:</p> <table>
<tr><th>Account Name</th> <td>
<apex:outputText value="{!Account.Name}"/>
</td> </tr> <tr>
<th>Account Rep</th> <td><apex:outputText value="{!Account.Owner.Name}"/></td> </tr>
<tr><th> Customer Since</th> <td><apex:outputText value="{0,date,long}"> <apex:param value="{!Account.CreatedDate}"/>
</apex:outputText></td>
</tr>
</table>
</div>
</apex:repeat>
</apex:page>
APEX
public class CampaignListPDFController {
public List<Campaign> campaigns{get;private set;}
public List<CampaignMember> campaignMembers { get; private set; }
public CampaignListPDFController(ApexPages.StandardSetController stdSetController){
campaigns = (List<Campaign>) stdSetController.getSelected();
campaignMembers = [
SELECT Campaign.Name, Campaign.Startdate, Campaign.Type FROM CampaignMember
];
}
}
Querying any value related to the relationship field is very easy. You can use dot operator to get the values. If your campaingn member is contact then simply use contact.name or contact.firstName. Same way if Campaign member is Lead then use Lead.Name.
Later in your free time, please go with these modules to learn more about SOQL:
https://trailhead.salesforce.com/content/learn/modules/apex_database/apex_database_soql
https://www.tutorialspoint.com/apex/apex_soql.htm
Thanks,
Ashish Singh