I want to distribute custom object records to some user. Theres 2 users inside 'assignmentList' and I
m 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
andcontinue
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:
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: