Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. @isTest static void testCreateSchedule() {
  2.  
  3. // Create Revenue Pipeline record
  4. Revenue_Pipeline__c revPipe = new Revenue_Pipeline__c();
  5. revPipe.Weighted_Product_Price__c = 1200;
  6. revPipe.Product_Start_Date__c = date.newInstance(2016, 08, 1);
  7. revPipe.Product_End_Date__c = date.newInstance(2017, 07, 1);
  8. revPipe.Name = 'Test Pipeline';
  9. revPipe.CurrencyIsoCode = 'USD';
  10. insert revPipe;
  11.  
  12. // Get the Revenue Pipeline record with the schedule
  13. Revenue_Pipeline_Schedule__c revPipeS = [SELECT Id, Amount__c
  14. FROM Revenue_Pipeline_Schedule__c
  15. WHERE Date__c = :date.newInstance(2016, 10, 1)
  16. LIMIT 1];
  17.  
  18. // Schedule record should be 1200/12 or 100
  19. System.assertEquals(100, revPipeS.Amount__c);
  20. }
  21.  
  22. @isTest static void testCheckRecord() {
  23.  
  24. // Create Revenue Pipeline record
  25. Revenue_Pipeline__c revPipe = new Revenue_Pipeline__c();
  26. revPipe.Weighted_Product_Price__c = 1200;
  27. revPipe.Product_Start_Date__c = date.newInstance(2016, 08, 1);
  28. revPipe.Product_End_Date__c = date.newInstance(2017, 07, 1);
  29. revPipe.Name = 'Test Pipeline';
  30. revPipe.CurrencyIsoCode = 'USD';
  31. insert revPipe;
  32.  
  33. // Edit Revenue Pipeline record to change the Price
  34. Test.startTest();
  35. revPipe.Weighted_Product_Price__c = 600;
  36. update revPipe;
  37. Test.stopTest();
  38.  
  39. // Get the Revenue Pipeline record with the schedule
  40. Revenue_Pipeline_Schedule__c revPipeS = [SELECT Id, Amount__c
  41. FROM Revenue_Pipeline_Schedule__c
  42. WHERE Date__c = :date.newInstance(2016, 10, 1) AND
  43. Revenue_Pipeline__c = :revPipe.Id
  44. LIMIT 1];
  45. // Schedule record should be 600/12 or 50
  46. System.debug('Amount is:: ' + revPipeS.Amount__c);
  47. //System.assertEquals(50, revPipeS.Amount__c);
  48. }
  49.  
  50. public static void createRevSchedule(Map<Id, Revenue_Pipeline__c> newMap)
  51. {
  52. List<Revenue_Pipeline_Schedule__c> newSchedule = new List<Revenue_Pipeline_Schedule__c>();
  53. for(Revenue_Pipeline__c revPipe : newMap.values()) {
  54. Integer numOfMonths = revPipe.Product_Start_Date__c.monthsBetween(revPipe.Product_End_Date__c) + 1;
  55. Integer n = revPipe.Subscription_Type__c == 'Subscription' ? 1 : 0;
  56. for(Integer i=0; i < numOfMonths; i++) {
  57. Revenue_Pipeline_Schedule__c revSchedule = new Revenue_Pipeline_Schedule__c();
  58. revSchedule.Amount__c = revPipe.Weighted_Product_Price__c/numOfMonths;
  59. revSchedule.Date__c = revPipe.Product_Start_Date__c.addMonths(i + n);
  60. revSchedule.Name = revPipe.Name + ' - ' + revSchedule.Date__c.year() + '-' + revSchedule.Date__c.month();
  61. revSchedule.Revenue_Pipeline__c = revPipe.Id;
  62. revSchedule.CurrencyIsoCode = revPipe.CurrencyIsoCode;
  63. newSchedule.add(revSchedule);
  64. }
  65. }
  66. if(newSchedule.size() > 0){
  67. try{
  68. insert newSchedule;
  69. } catch (DmlException ex) {
  70. ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, ex.getMessage()));
  71. }
  72. }
  73. }
  74.  
  75. // Checks if the fields were changed in the newMap compared to OldMap
  76. public void checkUpdate(Map<Id, Revenue_Pipeline__c> newMap, Map<Id, Revenue_Pipeline__c> oldMap)
  77. {
  78. Map<Id, Revenue_Pipeline__c> revPipeline = new Map<Id, Revenue_Pipeline__c>();
  79.  
  80. // Check if the fields changed
  81. for(Revenue_Pipeline__c revPipe : newMap.values())
  82. {
  83. if(
  84. revPipe.Product_Start_Date__c != oldMap.get(revPipe.Id).Product_Start_Date__c ||
  85. revPipe.Product_End_Date__c != oldMap.get(revPipe.Id).Product_End_Date__c ||
  86. revPipe.Product_Price__c != oldMap.get(revPipe.Id).Product_Price__c ||
  87. revPipe.Weighted_Product_Price__c != oldMap.get(revPipe.Id).Weighted_Product_Price__c)
  88. {
  89. revPipeline.put(revPipe.Id, revPipe);
  90. }
  91. }
  92. if(revPipeline.size() > 0)
  93. {
  94. deleteSchedule(revPipeline);
  95. }
  96. }
  97.  
  98. // Delete Revenue_Pipeline_Schedule records
  99. public void deleteSchedule(Map<Id, Revenue_Pipeline__c> revPipeline)
  100. {
  101. List<Revenue_Pipeline_Schedule__c> scheduleToDelete = [SELECT Id,
  102. Revenue_Pipeline__r.Id
  103. FROM Revenue_Pipeline_Schedule__c
  104. WHERE Revenue_Pipeline__r.Id In :revPipeline.keyset()];
  105. try{
  106. delete scheduleToDelete;
  107. }
  108. catch (DmlException ex){
  109. ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, ex.getMessage()));
  110. }
  111. createRevSchedule(revPipeline);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement