How do you skip the iteration when the Apex loop breaks and then continue?

134    Asked by Dannasahi in Salesforce , Asked on Aug 30, 2023

I need to distribute several object records to users. I want to spread each record to two users who are inside ‘assignmentList’ based on the inputted maximum call line they can manage. My condition is if their inputted max amount - total distributed record amount is greater than 0. I tried to loop the records and looped the assignmentList for each record. But the problem is whenever the user hits the maximum amount, the continue does not skip. As a result, the record is fixed to the first user. So, my question is, how can I skip the iteration happening in ‘continue’?  

Answered by David Edmunds

First of all, continue, and Apex break main activity is in the innermost loop, which is making the calculation more complicated. Also, observe if you can create the item before changing the tempUserAssignment object. You can also check out a different chunk of code if you want a more balanced distribution system. The code looks the following way:

Map mapByManualAmount = new map ();
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));

Your Answer


Parent Categories