Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. trigger product2Trigger on Product2 (
  2. after update
  3. ) {
  4. Product2Helper.AfterUpdate(trigger.New);
  5. }
  6.  
  7. public class Product2Helper {
  8. //TODO: After reviewing the existing code in the product2Helper class, you realize that it too can benefit from your constants class.
  9. // Modify Product2Helper to use the INVENTORY_ANNOUNCEMENTS constant instead of "group name" to ensure consistency in the app.
  10. /**
  11. * @name COLLABORATION_GROUP
  12. * @description List of CollaborationGroup used in both business and test logic
  13. **/
  14. static List<CollaborationGroup> COLLABORATION_GROUP = [
  15. SELECT Id
  16. FROM CollaborationGroup
  17. WHERE Name = :Constants.INVENTORY_ANNOUNCEMENTS
  18. OR Name = :('TEST'+Constants.INVENTORY_ANNOUNCEMENTS)
  19. LIMIT 1
  20. ];
  21.  
  22. /**
  23. * @name afterUpdate
  24. * @description called by product2 Trigger on After Update
  25. * @param List<Product2> newList
  26. * @param List<Product2> oldList
  27. **/
  28. public static void AfterUpdate(List<Product2> newList){
  29. //TODO: Next, complete the AfterUpdate method so that it uses the PostAlerts method when you determine
  30. // that a Product’s Quantity_Remaining__c field has dropped below the threshold value captured in the custom metadata records you created previously.
  31.  
  32. //ToDo: Declare a List of Product2 records named needsAnnouncement
  33.  
  34. //ToDo: Declare a Map of Strings to Inventory_Setting__mdt records
  35.  
  36. //ToDo: Loop through a query of Inventory_Setting__mdt records and populate the Map with Name as the key
  37.  
  38. //ToDo: Loop through the Products in newList
  39. // Use the corresponding Inventory Setting record to determine the correct Low Quantity Alert
  40. // If the Product's Quantity Remaining has been changed to less than the Low Quantity Alert
  41. // add it to the needsAnnouncement list
  42.  
  43. //ToDo: Pass records to the postAlerts method
  44.  
  45.  
  46. // TODO: After reading about the ConnectAPI, you realize that a Chatter Announcement is a better fit than a FeedItem post because an announcement acts more like an alert, in that it is timely and it expires.
  47. // You know that a low inventory value should prompt an announcement to be posted to the Inventory Announcements Chatter group, so an Apex trigger must be used.
  48. // After reviewing the legacy code, you realize the previous programmer attempted to write this business logic in the Product2Trigger, but that logic is faulty.
  49.  
  50. List<Product2> needsAnnouncement = new List<Product2>();
  51. Map<String, Inventory_Setting__mdt> stringToInventorySettingMap = new Map<String, Inventory_Setting__mdt>();
  52. for (Inventory_Setting__mdt setting : [SELECT DeveloperName, Id, Label, Language, Low_Quantity_Alert__c, MasterLabel, NamespacePrefix, QualifiedApiName FROM Inventory_Setting__mdt]) {
  53. stringToInventorySettingMap.put(setting.DeveloperName, setting);
  54. }
  55. System.debug('here');
  56. for ( Product2 p : newList ) {
  57. System.debug('there');
  58. if (stringToInventorySettingMap.containsKey(p.Family) && (p.Quantity_Remaining__c < stringToInventorySettingMap.get(p.Family).Low_Quantity_Alert__c)) {
  59. needsAnnouncement.add(p);
  60. System.debug('everywhere');
  61. }
  62. }
  63. PostAlerts(needsAnnouncement);
  64. }
  65.  
  66. /**
  67. * @name postAlerts
  68. * @description called by product2 Trigger on After Update
  69. * @param List<Product2> productList
  70. **/
  71. //TODO: Complete the PostAlerts method in Product2Helper to construct new AnnouncementInputs for the
  72. // Chatter Group and for use with the AnnouncementQueuable Apex class.
  73. public static void PostAlerts(List<Product2> productList){
  74. System.debug('PostAlerts');
  75. List<ConnectApi.AnnouncementInput> toPost = new List<ConnectApi.AnnouncementInput>();
  76. for ( Product2 p : productList ){
  77. System.debug('loop');
  78. ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
  79. ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
  80. ConnectApi.AnnouncementInput ann = new ConnectApi.AnnouncementInput();
  81.  
  82. // ToDo: Construct a new AnnouncementInput for the Chatter Group so that it
  83. // expires in a day
  84. // does not notify users via email.
  85. // and has a text body that includes the name of the product followed by the INVENTORY_LEVEL_LOW constant
  86. ann.sendEmails = false;
  87. ann.expirationDate = Date.today().addDays(1);
  88. ann.parentId = COLLABORATION_GROUP[0].Id;
  89.  
  90. textSegmentInput.text= p.Name + ' ' + Constants.INVENTORY_LEVEL_LOW;
  91.  
  92. messageBodyInput.messageSegments = new List<ConnectApi.TextSegmentInput>();
  93. messageBodyInput.messageSegments.add(textSegmentInput);
  94.  
  95. ann.body = messageBodyInput;
  96. toPost.add(ann);
  97. }
  98. // ToDo: Create and enqueue an instance of the announcementQueuable class with the list of Products
  99. System.debug('toPost: ' + toPost);
  100. AnnouncementQueueable q = new AnnouncementQueueable();
  101. q.toPost = toPost;
  102. System.enqueueJob(q);
  103. }
  104.  
  105. public class AnnouncementQueueable implements Queueable{
  106. public List<ConnectApi.AnnouncementInput> toPost;
  107.  
  108. public void execute(QueueableContext context) {
  109. PostAnnouncements(toPost);
  110. }
  111.  
  112.  
  113. //ToDo: Modify this class to implement the Queueable interface and call the postAnnouncements method.
  114. // Modify AnnouncementQueueable to implement the Queueable interface and call its postAnnouncements method.
  115. // Ensure that it requeues itself when it has more Announcements to post than limits allow.
  116.  
  117.  
  118.  
  119. /**
  120. * @name postAnnouncements
  121. * @description This method is provided for you to facilitate the Super Badge
  122. **/
  123. public static void PostAnnouncements(List<ConnectApi.AnnouncementInput> announcements){
  124. System.debug('Post Announcements: ' + announcements);
  125. while ( announcements.size() > 0 ){
  126. if ( Limits.getDMLStatements() < Limits.getLimitDMLStatements() && !test.isRunningTest() ){
  127. ConnectApi.AnnouncementInput a = announcements.remove(0);
  128. System.debug('a: ' + a);
  129. ConnectApi.Announcements.postAnnouncement('Internal', a);
  130. } else {
  131.  
  132. break;
  133. }
  134. }
  135. if ( announcements.size() > 0 && !test.isRunningTest() ){
  136. AnnouncementQueueable q = new AnnouncementQueueable();
  137. q.toPost = announcements;
  138. System.enqueueJob(q);
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement