[SalesForce] Apex Loop break and continue

I want to distribute custom object records to some user. Theres 2 users inside 'assignmentList' and Im trying to distribute each records to these 2 users based on the inputted max amount of call line they can handle. If their (inputted max amount – total distributed record amount) > 0.

I tried to loop the records and for each record, I loop the assignmentList, which is the users. But when the user amount reach its maximum amount, the 'continue' never skip to the next iteration of the loop, so the records only assigned to the 1st user. How should I skip the iteration?

map <String,tempUserAssignment> mapByManualAmount = new map <String,tempUserAssignment>();
        Decimal tempClAmount = 0;
        Decimal tempMaxAmount = 0;
        for(Call_Line__c dis : callLineList){
            tempClAmount = dis.APE_Total_Child__c;
            for(AgentAssignment assignm : assignmentList){
                tempMaxAmount = assignm.apeDistributed;
                if(mapByManualAmount.containsKey(assignm.agentId)){
                    tempUserAssignment tua = new tempUserAssignment();
                    tua.totalPolicy = mapByManualAmount.get(assignm.agentId).totalPolicy + 1;
                    tua.totalAPE = mapByManualAmount.get(assignm.agentId).totalAPE + dis.APE_Total_Child__c;

                    if ((tempMaxAmount - tua.totalApe) > 0){
                        system.debug('***tua.totalAPE:'+tua.totalAPE);
                        tosaveCallLine = new Call_Line__c();
                        tosaveCallLine.id = dis.id;
                        tosaveCallLine.Agent__c = assignm.agentId;
                        tosaveCallLine.ownerId = assignm.agentId; 
                        //tosaveCallLineList.add(tosaveCallLine);   
                        mapByManualAmount.put(assignm.agentId, tua);
                        break;
                    }
                    else {
                        continue;
                    }
                }
                else{
                    tempUserAssignment tua = new tempUserAssignment();
                    tua.totalPolicy = 1;
                    tua.totalAPE = dis.APE_Total_Child__c;

                    mapByManualAmount.put(assignm.agentId, tua);
                    break;
                }    
            }
        }

Best Answer

break and continue work on the innermost loop.

Your logic is complicated, and can be greatly reduced by prewarming the map.

Also, you should check to see if the item should be created before modifying the tempUserAssignment object.

All of the optimizations so far end up looking like this:

Map<String,tempUserAssignment> mapByManualAmount = new map <String,tempUserAssignment>();
for(AgentAssignment assignm: assignmentList) {
    tempUserAssignment tua = new tempUserAssignment();
    tua.totalPolicy = 0.0;
    tua.totalAPE = 0.0;
    mapByManualAmount.put(assignmentList.agentId, tua);
}

for(Call_Line__c dis : callLineList){
    for(AgentAssignment assignm : assignmentList) {
        tempUserAssignment tua = mapByManualAmount.get(assignm.agentId);
        if(tua.totalApe + dis.APE_Total_Child__c <= assignm.apeDistributed) {
            tua.totalPolicy += 1.0;
            tua.totalAPE += dis.APE_Total_Child__c;
            tosaveCallLineList.add(new Call_Line__c(Id=dis.Id, Agent__c=assignm.agentId, OwnerId=assignm.agentId));
            break;
        }
    }
}

However, if you're looking for more of a balanced distribution system, you can also rework the code so that each person gets first shot at a record:

for(Call_Line__c dis : callLineList){
    for(AgentAssignment assignm : assignmentList) {
        // code from above //
    }
    // The first becomes last, and the second becomes first, a "rotate-left" operation.
    assignmentList.add(assignmentList.remove(0));
}
Related Topic