You need to sign in to do that
Don't have an account?

Code optimized
Hi,
is there a way to optimized this code? I am having problem with bulk upload and these needs to be optimized, can you instruct me on what should be the proper way?
is there a way to optimized this code? I am having problem with bulk upload and these needs to be optimized, can you instruct me on what should be the proper way?
public static void populateLastestEnrollment(List<Case> pCase){ Id EnrollmentRecordType = Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('Enrollment').getRecordTypeId(); Id DiscontinuedmentRecordType = Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('Unenroll From GPS').getRecordTypeId(); Id AdultPatient = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Adult Patient').getRecordTypeId(); Id MinorPatient = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Minor Patient').getRecordTypeId(); Id RECORDTYPEID_RELATIONSHIP_INDIVIDUAL = Schema.SObjectType.Relationship__c.getRecordTypeInfosByName().get('Individual Relationship').getRecordTypeId(); List<Account> updatedAccountList = new List<Account>(); Set<Id> caseAccountId = new Set<Id>(); //Get the parent Account of the case for(Case cs : pCase){ caseAccountId.add(cs.AccountId); } List<Account> caseAccountList = [SELECT Id, Latest_Enrollment_Unenrollment_Case__c, Consent_Provided__c,Consent_Version__c, Consent_Provided_Date__c, Enrollment_Status_Text__c, Enrollment_Form_Received_Date__c, RecordTypeId, X18th_Birthdate__c, PersonMobilePhone, PersonHomePhone, (SELECT Id, GPS_Enrollment_Status_Detail__c, Date_Consent_Provided__c, GPS_Enrollment_Status__c, Enrollment_Type__c, Enrollment_Form_Received_Date__c, Consent_Version__c, CaseNumber, Consent_Provided__c, Consent_Provided_By__c, CreatedDate, Status, RecordTypeId, Case_Sub_Status__c FROM Cases ORDER BY CreatedDate ASC) FROM Account WHERE Id IN: caseAccountId]; //loop through all child records for(Account a : caseAccountList){ //Checks if case object has records if(a.Cases.size() > 0){ // find out which one is the most recent relevant case for a given patient account for(Case c : a.Cases){ if(c.CreatedDate >= mostRecentCase.CreatedDate || mostRecentCase.Id == null){ if(c.RecordTypeId == EnrollmentRecordType || (c.RecordTypeId == DiscontinuedmentRecordType && c.Status == 'Closed' && c.Case_Sub_Status__c == 'Completed')){ mostRecentCase = c; } } } } // If there is no relevant case available, then make the auto populated fields null: if(mostRecentCase.Id == null){ a.Latest_Enrollment_Unenrollment_Case__c = null; a.Consent_Provided_Date__c = null; a.Consent_Expiration_Date__c = null; a.Consent_Provided__c = null; a.Consent_Version__c = null; a.Enrollment_Form_Received_Date__c = null; a.Enrollment_Status_Text__c = 'Never Enrolled'; } else if(mostRecentCase.RecordTypeId == EnrollmentRecordType && (mostRecentCase.Enrollment_Type__c != 'New Consent' || mostRecentCase.Date_Consent_Provided__c != null)){ a.Consent_Provided__c = mostRecentCase.Consent_Provided__c; a.Consent_Provided_Date__c = mostRecentCase.Date_Consent_Provided__c; a.Consent_Version__c = mostRecentCase.Consent_Version__c; if (a.Consent_Provided__c == null){ a.Consent_Expiration_Date__c = null; } else if (a.Consent_Provided__c == 'Verbal Consent') { a.Consent_Expiration_Date__c = a.Consent_Provided_Date__c.addDays(30); } else if (a.Consent_Provided__c == 'Written Consent') { 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 a.Consent_Expiration_Date__c = a.X18th_Birthdate__c.addDays(30); } else { a.Consent_Expiration_Date__c = a.Consent_Provided_Date__c.addYears(10); } } a.Consent_Expiration_Workflow_Reset__c = false; a.Enrollment_Status_Text__c = mostRecentCase.GPS_Enrollment_Status__c; a.Enrollment_Form_Received_Date__c = mostRecentCase.Enrollment_Form_Received_Date__c; a.Latest_Enrollment_Unenrollment_Case__c = mostRecentCase.Id; } else if(mostRecentCase.RecordTypeId == DiscontinuedmentRecordType){ a.Consent_Provided__c = null; a.Consent_Provided_Date__c = null; a.Consent_Expiration_Date__c = null; a.Consent_Version__c = null; a.Enrollment_Status_Text__c = mostRecentCase.GPS_Enrollment_Status__c; a.Latest_Enrollment_Unenrollment_Case__c = mostRecentCase.Id; } //Make sure that only 1 record will update the Parent updatedAccountList.add(a); } //update Account if(updatedAccountList.size() > 0){ update updatedAccountList; } }
What is the exact problem with the bulk upload? (time out, heap size limit ?) This code is bulkified yet
Alain
Best Practices for Designing Bulk ProgramsThe following are the best practices for this design pattern:
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_query_limits.htm
Thanks,
Nachiket