Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. public static void populateLastestEnrollment(List<Case> pCase){
  2. Id EnrollmentRecordType = Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('Enrollment').getRecordTypeId();
  3. Id DiscontinuedmentRecordType = Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('Unenroll From GPS').getRecordTypeId();
  4. Id AdultPatient = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Adult Patient').getRecordTypeId();
  5. Id MinorPatient = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Minor Patient').getRecordTypeId();
  6. Id RECORDTYPEID_RELATIONSHIP_INDIVIDUAL = Schema.SObjectType.Relationship__c.getRecordTypeInfosByName().get('Individual Relationship').getRecordTypeId();
  7.  
  8. List<Account> updatedAccountList = new List<Account>();
  9. Set<Id> caseAccountId = new Set<Id>();
  10.  
  11. //Get the parent Account of the case
  12. for(Case cs : pCase){
  13. caseAccountId.add(cs.AccountId);
  14. }
  15.  
  16. List<Account> caseAccountList = [SELECT Id, Latest_Enrollment_Unenrollment_Case__c, Consent_Provided__c,Consent_Version__c, Consent_Provided_Date__c,
  17. Enrollment_Status_Text__c, Enrollment_Form_Received_Date__c, RecordTypeId, X18th_Birthdate__c, PersonMobilePhone, PersonHomePhone,
  18. (SELECT Id, GPS_Enrollment_Status_Detail__c, Date_Consent_Provided__c, GPS_Enrollment_Status__c, Enrollment_Type__c,
  19. Enrollment_Form_Received_Date__c, Consent_Version__c, CaseNumber, Consent_Provided__c, Consent_Provided_By__c,
  20. CreatedDate, Status, RecordTypeId, Case_Sub_Status__c FROM Cases ORDER BY CreatedDate ASC)
  21. FROM Account WHERE Id IN: caseAccountId];
  22.  
  23. //loop through all child records
  24. for(Account a : caseAccountList){
  25. //Checks if case object has records
  26. if(a.Cases.size() > 0){
  27. // find out which one is the most recent relevant case for a given patient account
  28. for(Case c : a.Cases){
  29. if(c.CreatedDate >= mostRecentCase.CreatedDate || mostRecentCase.Id == null){
  30. if(c.RecordTypeId == EnrollmentRecordType ||
  31. (c.RecordTypeId == DiscontinuedmentRecordType
  32. && c.Status == 'Closed'
  33. && c.Case_Sub_Status__c == 'Completed')){
  34. mostRecentCase = c;
  35. }
  36. }
  37. }
  38. }
  39.  
  40. // If there is no relevant case available, then make the auto populated fields null:
  41. if(mostRecentCase.Id == null){
  42. a.Latest_Enrollment_Unenrollment_Case__c = null;
  43. a.Consent_Provided_Date__c = null;
  44. a.Consent_Expiration_Date__c = null;
  45. a.Consent_Provided__c = null;
  46. a.Consent_Version__c = null;
  47. a.Enrollment_Form_Received_Date__c = null;
  48. a.Enrollment_Status_Text__c = 'Never Enrolled';
  49. } else if(mostRecentCase.RecordTypeId == EnrollmentRecordType &&
  50. (mostRecentCase.Enrollment_Type__c != 'New Consent' || mostRecentCase.Date_Consent_Provided__c != null)){
  51. a.Consent_Provided__c = mostRecentCase.Consent_Provided__c;
  52. a.Consent_Provided_Date__c = mostRecentCase.Date_Consent_Provided__c;
  53. a.Consent_Version__c = mostRecentCase.Consent_Version__c;
  54. if (a.Consent_Provided__c == null){
  55. a.Consent_Expiration_Date__c = null;
  56. } else if (a.Consent_Provided__c == 'Verbal Consent') {
  57. a.Consent_Expiration_Date__c = a.Consent_Provided_Date__c.addDays(30);
  58. } else if (a.Consent_Provided__c == 'Written Consent') {
  59. if (a.X18th_Birthdate__c.addDays(30) < a.Consent_Provided_Date__c.addYears(10) && a.X18th_Birthdate__c > mostRecentCase.Date_Consent_Provided__c){ // Sheri updated add days
  60. a.Consent_Expiration_Date__c = a.X18th_Birthdate__c.addDays(30);
  61. } else {
  62. a.Consent_Expiration_Date__c = a.Consent_Provided_Date__c.addYears(10);
  63. }
  64. }
  65. a.Consent_Expiration_Workflow_Reset__c = false;
  66. a.Enrollment_Status_Text__c = mostRecentCase.GPS_Enrollment_Status__c;
  67. a.Enrollment_Form_Received_Date__c = mostRecentCase.Enrollment_Form_Received_Date__c;
  68. a.Latest_Enrollment_Unenrollment_Case__c = mostRecentCase.Id;
  69. } else if(mostRecentCase.RecordTypeId == DiscontinuedmentRecordType){
  70. a.Consent_Provided__c = null;
  71. a.Consent_Provided_Date__c = null;
  72. a.Consent_Expiration_Date__c = null;
  73. a.Consent_Version__c = null;
  74. a.Enrollment_Status_Text__c = mostRecentCase.GPS_Enrollment_Status__c;
  75. a.Latest_Enrollment_Unenrollment_Case__c = mostRecentCase.Id;
  76. }
  77. //Make sure that only 1 record will update the Parent
  78. updatedAccountList.add(a);
  79. }
  80.  
  81. //update Account
  82. if(updatedAccountList.size() > 0){
  83. update updatedAccountList;
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement