Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. /**
  2. * @author Ben Edwards (ben@edwards.nz)
  3. * @date 8th December 2016
  4. * @description Controller for the CampaignAddMembersExtCon page
  5. *
  6. **/
  7. public with sharing class CampaignAddMembersExtCon {
  8.  
  9. // The Campaign loaded in from the StandardController
  10. public Campaign theCampaign {get;set;}
  11.  
  12. // The list of campaign members for the page
  13. public List<CampaignMemberWrapper> campaignMembers {get;set;}
  14.  
  15. // The Go TO Page selected from the page
  16. public Integer goToPageNumber {get;set;}
  17.  
  18. // CONSTRUCTOR
  19. public CampaignAddMembersExtCon(ApexPages.StandardController stdController) {
  20.  
  21. theCampaign = (Campaign) stdController.getRecord();
  22.  
  23. // Load the campaign members for the page
  24. getCampaignMembersForPage();
  25. }
  26.  
  27. /**
  28. * @author Ben Edwards (ben@edwards.nz)
  29. * @description StandardSetController to handle pagination for the all campaign members
  30. * @return StandardSetController of products
  31. **/
  32. public ApexPages.StandardSetController campaignMembersSetCon {
  33.  
  34. get {
  35.  
  36. if (campaignMembersSetCon == null) {
  37.  
  38. // Load all the products into the standard set controller
  39. campaignMembersSetCon = new ApexPages.StandardSetController(Database.getQueryLocator([
  40. SELECT
  41. Id
  42. FROM
  43. CampaignMember
  44. WHERE
  45. CampaignId = :theCampaign.Id
  46. ]));
  47.  
  48. // Set the page size (defaulted to 20)
  49. campaignMembersSetCon.setPageSize(20);
  50.  
  51. return campaignMembersSetCon;
  52. }
  53. return campaignMembersSetCon;
  54. }
  55. set;
  56. }
  57.  
  58.  
  59. /**
  60. * @author Ben Edwards (ben@edwards.nz)
  61. * @description Store a map of all selected campaign members, so they are included in the save
  62. * @return Map of CampaignMember Id
  63. **/
  64. public Map<Id, CampaignMemberWrapper> allMembersCached {
  65. get {
  66. if (allMembersCached == null) {
  67. allMembersCached = new Map<Id, CampaignMemberWrapper>();
  68. }
  69. return allMembersCached;
  70. }
  71. set;
  72. }
  73.  
  74.  
  75. /**
  76. * @author Ben Edwards (ben@edwards.nz)
  77. * @description The list of All Products to create
  78. **/
  79. public void getCampaignMembersForPage() {
  80.  
  81. // Create a new list
  82. campaignMembers = new List<CampaignMemberWrapper>();
  83.  
  84. // Load just the records from the current page
  85. for (CampaignMember cm :(List<CampaignMember>) campaignMembersSetCon.getRecords()) {
  86.  
  87. // If the product already exists in the map
  88. if (allMembersCached.containsKey(cm.Id)) {
  89.  
  90. // Get the cached value from the map
  91. campaignMembers.add(allMembersCached.get(cm.Id));
  92. }
  93. else {
  94.  
  95. // Create a new one and default to false
  96. campaignMembers.add(new CampaignMemberWrapper(cm, false);
  97. }
  98. }
  99. }
  100.  
  101.  
  102. /**
  103. * @author Ben Edwards (ben@edwards.nz)
  104. * @description When the "next" button is pressed on the pagination
  105. **/
  106. public void nextPage() {
  107.  
  108. // Update the set of selected products, to cache user's selections
  109. // across multiple pagess
  110. updateSelectionMap();
  111.  
  112. // Get the next set of values
  113. campaignMembersSetCon.next();
  114.  
  115. // Refresh the products list
  116. getCampaignMembersForPage();
  117. }
  118.  
  119.  
  120. /**
  121. * @author Ben Edwards (ben@edwards.nz)
  122. * @description When the "previous" button is pressed on the pagination
  123. **/
  124. public void previousPage() {
  125.  
  126. // Update the set of selected products, to cache user's selections
  127. // across multiple pagess
  128. updateSelectionMap();
  129.  
  130. // Get the next set of values
  131. campaignMembersSetCon.previous();
  132.  
  133. // Refresh the products list
  134. getCampaignMembersForPage();
  135. }
  136.  
  137.  
  138. /**
  139. * @author Ben Edwards (ben@edwards.nz)
  140. * @description Go to a specific page, selected from the page
  141. **/
  142. public void goToPage () {
  143.  
  144. // Update the set of selected campaign members, to cache user's selections
  145. // across multiple page
  146. updateSelectionMap();
  147.  
  148. // Get the next set of values
  149. campaignMembersSetCon.setPageNumber(Integer.valueOf(goToPageNumber));
  150.  
  151. // Refresh the products list
  152. getCampaignMembersForPage();
  153. }
  154.  
  155.  
  156. /**
  157. * @author Ben Edwards (ben@edwards.nz)
  158. * @description Updates the set of selected campagin members
  159. **/
  160. private void updateSelectionMap () {
  161.  
  162. // Iterate over the maps
  163. for (CampaignMemberWrapper member :campaignMembers) {
  164.  
  165. // Store the value back into the map
  166. allMembersCached.put(member.campaignMember.Id, member);
  167. }
  168. }
  169.  
  170.  
  171. /**
  172. * @author Ben Edwards (ben@edwards.nz)
  173. * @description Save all records and return back to the parent Opportunity
  174. * @return Return back to the parent Opportunity
  175. **/
  176. public PageReference saveAll () {
  177.  
  178. PageReference pageRef;
  179.  
  180. // Update the selected All Products before saving
  181. updateSelectionMap();
  182.  
  183. try {
  184.  
  185. List<CampaignMember> updateList = new List<CampaignMember>();
  186.  
  187. // Iterate over the cached campaign members to do update
  188. for (CampaignMemberWrapper cmWrapped :allMembersCached) {
  189.  
  190. if (cmWrapped.isChecked) {
  191.  
  192. updateList.add(cmWrapped.campaignMember);
  193. }
  194. }
  195.  
  196. update updateList;
  197. }
  198. catch (Exception ex) {
  199.  
  200. ApexPages.addMessages(ex);
  201.  
  202. }
  203.  
  204. return pageRef;
  205. }
  206.  
  207.  
  208.  
  209. /**
  210. * @description Wrap the CampaignMember into a wrapper class in order to provide checkbox
  211. **/
  212. public class CampaignMemberWrapper {
  213.  
  214. // Allows the user to check/uncheck the line item from the page
  215. public Boolean isChecked {get;set;}
  216.  
  217. // The various SObject records to create/update
  218. public CampaignMember campaignMember {get;set;}
  219.  
  220. // Constructor for instantiate an Opp Line
  221. public LineItemWrapper (CampaignMember member, Boolean checked) {
  222.  
  223. this.isChecked = checked;
  224.  
  225. // Load the CampaignMember
  226. this.campaignMember = member;
  227. }
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement