1

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;
                }    
            }
        }
3

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));
}
  • The 1st code is working as expected, Thanks! I`ll try to rework the code for more balanced records distribution. so basically, List.add(List.remove()); will remove the 1st but insert it to the last at the same time? – Arief Gunawan Tjiptorahardja Jan 29 at 4:47
  • 1
    @AriefGunawanTjiptorahardja Correct. List.remove(Integer) removes the zero-indexed element and returns it, which we then pass to List.add(T) (where T refers to the type of the List), which then adds the item after the last index. – sfdcfox Jan 29 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.