Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Ben Edwards (ben@edwards.nz)
- * @date 8th December 2016
- * @description Controller for the CampaignAddMembersExtCon page
- *
- **/
- public with sharing class CampaignAddMembersExtCon {
- // The Campaign loaded in from the StandardController
- public Campaign theCampaign {get;set;}
- // The list of campaign members for the page
- public List<CampaignMemberWrapper> campaignMembers {get;set;}
- // The Go TO Page selected from the page
- public Integer goToPageNumber {get;set;}
- // CONSTRUCTOR
- public CampaignAddMembersExtCon(ApexPages.StandardController stdController) {
- theCampaign = (Campaign) stdController.getRecord();
- // Load the campaign members for the page
- getCampaignMembersForPage();
- }
- /**
- * @author Ben Edwards (ben@edwards.nz)
- * @description StandardSetController to handle pagination for the all campaign members
- * @return StandardSetController of products
- **/
- public ApexPages.StandardSetController campaignMembersSetCon {
- get {
- if (campaignMembersSetCon == null) {
- // Load all the products into the standard set controller
- campaignMembersSetCon = new ApexPages.StandardSetController(Database.getQueryLocator([
- SELECT
- Id
- FROM
- CampaignMember
- WHERE
- CampaignId = :theCampaign.Id
- ]));
- // Set the page size (defaulted to 20)
- campaignMembersSetCon.setPageSize(20);
- return campaignMembersSetCon;
- }
- return campaignMembersSetCon;
- }
- set;
- }
- /**
- * @author Ben Edwards (ben@edwards.nz)
- * @description Store a map of all selected campaign members, so they are included in the save
- * @return Map of CampaignMember Id
- **/
- public Map<Id, CampaignMemberWrapper> allMembersCached {
- get {
- if (allMembersCached == null) {
- allMembersCached = new Map<Id, CampaignMemberWrapper>();
- }
- return allMembersCached;
- }
- set;
- }
- /**
- * @author Ben Edwards (ben@edwards.nz)
- * @description The list of All Products to create
- **/
- public void getCampaignMembersForPage() {
- // Create a new list
- campaignMembers = new List<CampaignMemberWrapper>();
- // Load just the records from the current page
- for (CampaignMember cm :(List<CampaignMember>) campaignMembersSetCon.getRecords()) {
- // If the product already exists in the map
- if (allMembersCached.containsKey(cm.Id)) {
- // Get the cached value from the map
- campaignMembers.add(allMembersCached.get(cm.Id));
- }
- else {
- // Create a new one and default to false
- campaignMembers.add(new CampaignMemberWrapper(cm, false);
- }
- }
- }
- /**
- * @author Ben Edwards (ben@edwards.nz)
- * @description When the "next" button is pressed on the pagination
- **/
- public void nextPage() {
- // Update the set of selected products, to cache user's selections
- // across multiple pagess
- updateSelectionMap();
- // Get the next set of values
- campaignMembersSetCon.next();
- // Refresh the products list
- getCampaignMembersForPage();
- }
- /**
- * @author Ben Edwards (ben@edwards.nz)
- * @description When the "previous" button is pressed on the pagination
- **/
- public void previousPage() {
- // Update the set of selected products, to cache user's selections
- // across multiple pagess
- updateSelectionMap();
- // Get the next set of values
- campaignMembersSetCon.previous();
- // Refresh the products list
- getCampaignMembersForPage();
- }
- /**
- * @author Ben Edwards (ben@edwards.nz)
- * @description Go to a specific page, selected from the page
- **/
- public void goToPage () {
- // Update the set of selected campaign members, to cache user's selections
- // across multiple page
- updateSelectionMap();
- // Get the next set of values
- campaignMembersSetCon.setPageNumber(Integer.valueOf(goToPageNumber));
- // Refresh the products list
- getCampaignMembersForPage();
- }
- /**
- * @author Ben Edwards (ben@edwards.nz)
- * @description Updates the set of selected campagin members
- **/
- private void updateSelectionMap () {
- // Iterate over the maps
- for (CampaignMemberWrapper member :campaignMembers) {
- // Store the value back into the map
- allMembersCached.put(member.campaignMember.Id, member);
- }
- }
- /**
- * @author Ben Edwards (ben@edwards.nz)
- * @description Save all records and return back to the parent Opportunity
- * @return Return back to the parent Opportunity
- **/
- public PageReference saveAll () {
- PageReference pageRef;
- // Update the selected All Products before saving
- updateSelectionMap();
- try {
- List<CampaignMember> updateList = new List<CampaignMember>();
- // Iterate over the cached campaign members to do update
- for (CampaignMemberWrapper cmWrapped :allMembersCached) {
- if (cmWrapped.isChecked) {
- updateList.add(cmWrapped.campaignMember);
- }
- }
- update updateList;
- }
- catch (Exception ex) {
- ApexPages.addMessages(ex);
- }
- return pageRef;
- }
- /**
- * @description Wrap the CampaignMember into a wrapper class in order to provide checkbox
- **/
- public class CampaignMemberWrapper {
- // Allows the user to check/uncheck the line item from the page
- public Boolean isChecked {get;set;}
- // The various SObject records to create/update
- public CampaignMember campaignMember {get;set;}
- // Constructor for instantiate an Opp Line
- public LineItemWrapper (CampaignMember member, Boolean checked) {
- this.isChecked = checked;
- // Load the CampaignMember
- this.campaignMember = member;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement