function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sony PSPSony PSP 

Code optimized


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){
        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

        //update Account
        if(updatedAccountList.size() > 0){
                update updatedAccountList;


Alain CabonAlain Cabon

What is the exact problem with the bulk upload? (time out, heap size limit ?) This code is bulkified yet

Nishad KNishad K
Hi Sony,

Best Practices for Designing Bulk ProgramsThe following are the best practices for this design pattern:
  • Minimize the number of data manipulation language (DML) operations by adding records to collections and performing DML operations against these collections.
  • Minimize the number of SOQL statements by preprocessing records and generating sets, which can be placed in single SOQL statement used with the IN clause.
Nachiket Deshpande 33Nachiket Deshpande 33
What issue you are facing here ? however ypu can refere following link to undersatnd the limitations for relationship queries.

Sony PSPSony PSP
its says that System.LimitException: Apex CPU time limit exceeded