0

I have the following conditions which closed the milestones based on the Status=Closed, Assume I have the 3 milestones which I wants to closed. If 100 agents starts working on it simultaneous, then it look to me that it wont worked. The steps no. 18, 20 and 22 will give SOQL 101 error

Could you please help me to bulkify the below code ?

String mileStoneName;
if(cs.SubCategory__c == 'Auth'){

                        if(cs.Status == XXXX && (cs.SubStatus__c == XXXX || cs.SubStatus__c == XXXX)){
                            caseIds.add(cs.Id);
                            if(cs.isAuthRequest == 'Yes'){
                                mileStoneName = 'Milestone1';
                            }else{
                                mileStoneName = 'Milestone2';
                            }
                        }
                        else if(cs.Status == YYYY && cs.SubStatus__c == MMMM){
                            caseIds.add(cs.Id);
                            mileStoneName = 'Milestone3';
                        }
                        else if(cs.Status == 'Closed'){
                            caseIds.add(cs.Id);
                            MilestoneUtils.completeMilestone(caseIds, 'Milestone3', System.now()); // line-18
                            if(cs.isAuthRequest == 'Yes'){
                                MilestoneUtils.completeMilestone(caseIds, 'Milestone1', System.now()); // line-20
                            }else{
                                MilestoneUtils.completeMilestone(caseIds, 'Milestone2', System.now()); // line-22
                            }
                        }
                    }

if(!caseIds.isEmpty() && !String.isEmpty(milestoneName)){
    MilestoneUtils.completeMilestone(caseIds, milestoneName, System.now());
}

The below code to complete the milestones

public static void completeMilestone(Set<Id> caseIds, String milestoneName, DateTime complDate){
        List<CaseMilestone> cmsToUpdate = [SELECT Id,completionDate 
                                            FROM CaseMilestone
                                            WHERE caseId in :caseIds 
                                            AND MilestoneType.Name = :milestoneName 
                                            AND completionDate = NULL LIMIT 1];
        System.debug('Case Milestone :: '+cmsToUpdate.size()+', Case Milestone Id :: '+cmsToUpdate);

        if(cmsToUpdate.isEmpty() == false){
            for (CaseMilestone cm : cmsToUpdate){
                cm.completionDate = complDate;
            }
            update cmsToUpdate;
        }
    }
5
  • Right, Milestone.Complete is common method written to complete the milestone. Please see updated code above. Also please post code based on my above code ? Commented Aug 11, 2017 at 11:56
  • If above given is your entire code, and your code have only one SOQL query than How Exception of SOQL 101 can occur? I believe this is not your entire code and still you need to manage your code according to Governor Limits Commented Aug 11, 2017 at 12:24
  • No, I have almost 70 such if else conditions. Please guide now Commented Aug 11, 2017 at 14:27
  • @user4567570, you are calling MilestoneUtils.completeMilestone in a loop, which must be avoided. Can you post your updated code, specially how it is looping. How you are getting cs value in the logic Commented Aug 13, 2017 at 16:08
  • @SantanuBoral - You're right. I just corrected that logic now. Commented Aug 13, 2017 at 20:10

1 Answer 1

0

I have corrected logic using below code. I hope its out of the for loop now :)

List<String> mileStoneList = ................. // get the list of active milestones on the case
String mileStoneName;
String isClosed = false;

for(Case c : cases){
    .................
    ..................
    if(cs.SubCategory__c == 'Auth'){

        if(cs.Status == XXXX && (cs.SubStatus__c == XXXX || cs.SubStatus__c == XXXX)){
            caseIds.add(cs.Id);
            if(cs.isAuthRequest == 'Yes'){
                mileStoneName = 'Milestone1';
            }else{
                mileStoneName = 'Milestone2';
            }
        }
        else if(cs.Status == YYYY && cs.SubStatus__c == MMMM){
            caseIds.add(cs.Id);
            mileStoneName = 'Milestone3';
        }
        else if(cs.Status == 'Closed'){
            caseIds.add(cs.Id);
            isClosed = true;  // closed all mileStones
        }
    }

    if(!String.isEmpty(milestoneName)){
        MilestoneUtils.completeMilestone(caseIds, milestoneName, System.now());
    }

    if(isClosed){
        MilestoneUtils.completeAllMilestone(caseIds, milestoneName, System.now());
        isClosed = false;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .