Guest User

Untitled

a guest
Mar 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. ```
  2. trigger SchedulePayment on Opportunity(after insert, after update) {
  3. if (Trigger.isInsert) {
  4. for (Opportunity o: Trigger.new) {
  5. Decimal expectedRevenue = o.ExpectedRevenue;
  6. Date paymentDate = o.Payment_Start_Date__c;
  7. Integer monthsBetweenStartAndEnd = o.Payment_Start_Date__c.monthsBetween(o.CloseDate);
  8. for (Integer i = 0; i < monthsBetweenStartAndEnd; i++) {
  9. // Create Payments when opportunity is created
  10. Payment_Schedule__c payment = new Payment_Schedule__c();
  11. payment.Amount__c = expectedRevenue / monthsBetweenStartAndEnd;
  12. payment.Payment_Date__c = paymentDate;
  13. payment.Opportunity__c = o.id;
  14. insert payment;
  15. // Assure we get the next month for the next payment record
  16. paymentDate = paymentDate.addMonths(1);
  17.  
  18. }
  19. }
  20.  
  21. } else if (Trigger.isUpdate) {
  22. for (Opportunity o: trigger.new) {
  23. Decimal expectedRevenue = o.ExpectedRevenue;
  24. Date paymentDate = o.Payment_Start_Date__c;
  25. Date endDate = o.CloseDate;
  26. Integer monthsBetweenStartAndEnd = o.Payment_Start_Date__c.monthsBetween(o.CloseDate);
  27. // query related payment records.
  28. List < Payment_Schedule__c > payments = [select NAME, Payment_Date__c from Payment_Schedule__c where Opportunity__c != null and Opportunity__c in: trigger.new];
  29.  
  30. Date lastScheduledPayment = payments.get(payments.Size() - 1).Payment_Date__c;
  31. Integer extraMonths = payments.Size() - monthsBetweenStartAndEnd;
  32. Integer monthsToAdd = monthsBetweenStartAndEnd - payments.Size();
  33. // this is to make sure we insert the correct months for new records on update
  34. boolean addMonths = true;
  35. for (Payment_Schedule__c payment: payments) {
  36. // remove records if time is shortened.
  37. if (extraMonths > 0) {
  38. delete payment;
  39. extraMonths--;
  40. }
  41.  
  42. // add records if time has been extended.
  43. else if (extraMonths < 0) {
  44. // this is to make sure we insert the correct months for new records on update
  45. if (addMonths == true) {
  46. paymentDate = paymentDate.addMonths(monthsToAdd);
  47. }
  48.  
  49. Payment_Schedule__c newPayment = new Payment_Schedule__c();
  50. newPayment.Amount__c = expectedRevenue / monthsBetweenStartAndEnd;
  51. newPayment.Payment_Date__c = paymentDate;
  52. newPayment.Opportunity__c = o.id;
  53. insert newPayment;
  54. addMonths = false;
  55. paymentDate = paymentDate.addMonths(1);
  56. // update the revenue of previous inserted records when a new record is inserted.
  57. payment.Amount__c = expectedRevenue / monthsBetweenStartAndEnd;
  58. update payment;
  59.  
  60.  
  61. }
  62. else if (extraMonths == 0) {
  63. // update payment records.
  64. payment.Payment_Date__c = paymentDate;
  65. payment.Amount__c = expectedRevenue / monthsBetweenStartAndEnd;
  66. update payment;
  67. paymentDate = paymentDate.addMonths(1);
  68.  
  69. }
  70.  
  71. }
  72. }
  73.  
  74.  
  75. }
  76.  
  77. }
  78.  
  79. }
  80. ```
Add Comment
Please, Sign In to add comment