[SalesForce] how to break from a if statement

I have two objects, Revenue Schedule and revenue schedule split. Every Revenue Schedule will have 3 revenue schedule split records. For Revenue Schedule object I have 3 fields: sales rep 1, sales rep 2, and sales rep 3.

But I have only one field for Revenue schedule split object which is sales rep. Now on updating revenue schedule I want to update split records. Below is my code –

if(trigger.IsUpdate){

        for(Revenue_Schedule__c shed: Trigger.new){

        Double percentRemain = 100.0;
        percentRemain -= (Double) shed.Split_Percent_2__c;
        percentRemain -= (Double) shed.Split_Percent_3__c;

            for(Rev_Schedule_Split__c sp: revshedtoSplits.get(shed.id)){
            //split 1 calculation
                if(shed.Sales_Rep_1__c != null){
                    sp.Sales_Rep__c = shed.Sales_Rep_1__c;
                    sp.Amount_LC__c  = percentRemain * shed.Revenue_LC__c  * .01;
                    sp.Amount_USD__c = percentRemain * shed.Revenue_USD__c * .01;

                }
            //split 2 calculation
                if(shed.Sales_Rep_2__c != null){
                    sp.Sales_Rep__c = shed.Sales_Rep_2__c;
                    sp.Amount_LC__c  = shed.Split_Percent_2__c * shed.Revenue_LC__c  * .01;
                    sp.Amount_USD__c = shed.Split_Percent_2__c * shed.Revenue_USD__c * .01; 

                }
            //split 3 calculation
                if(shed.Sales_Rep_3__c != null){
                    sp.Sales_Rep__c = shed.Sales_Rep_3__c;
                    sp.Amount_LC__c  = shed.Split_Percent_3__c * shed.Revenue_LC__c  * .01;
                    sp.Amount_USD__c = shed.Split_Percent_3__c * shed.Revenue_USD__c * .01; 

                }
               toUpdatesplits.add(sp); 
            }

        }
        if(toUpdatesplits != null && toUpdatesplits.size()>0){
            update toUpdatesplits;
        }
    }

where revshedtoSplits is a map which stores Revenue Schedule id and corresponding split records. Above code is updating the split records with the sales_rep_3 for all split records. I want to update each split record by sales rep 1,2 and 3. I tried using break statement, but it is breaking from for loop itself. I just want to break from if loop. Please guide.

Best Answer

If you have all 3 sales rep fields populated on the Revenue Schedule object, all 3 conditions inside the for loop will match and the last condition will obviously overwrite the first 2.

one alternate i can think of is to run the loop with index based on the split record size and assign the reps to each of the split records in order..

something like below

if(trigger.IsUpdate){

    for(Revenue_Schedule__c shed: Trigger.new){

    Double percentRemain = 100.0;
    percentRemain -= (Double) shed.Split_Percent_2__c;
    percentRemain -= (Double) shed.Split_Percent_3__c;

    List<Rev_Schedule_Split__c> spList = revshedtoSplits.get(shed.id);
    if(spList == null || spList.isEmpty())
        continue;

        for(Integer i=0; i < spList.size(); i++){
            Rev_Schedule_Split__c sp
        //split 1 calculation
            if(i==0 && shed.Sales_Rep_1__c != null){
                sp.Sales_Rep__c = shed.Sales_Rep_1__c;
                sp.Amount_LC__c  = percentRemain * shed.Revenue_LC__c  * .01;
                sp.Amount_USD__c = percentRemain * shed.Revenue_USD__c * .01;

            }
        //split 2 calculation
            if(i==1 && shed.Sales_Rep_2__c != null){
                sp.Sales_Rep__c = shed.Sales_Rep_2__c;
                sp.Amount_LC__c  = shed.Split_Percent_2__c * shed.Revenue_LC__c  * .01;
                sp.Amount_USD__c = shed.Split_Percent_2__c * shed.Revenue_USD__c * .01; 

            }
        //split 3 calculation
            if(i==2 && shed.Sales_Rep_3__c != null){
                sp.Sales_Rep__c = shed.Sales_Rep_3__c;
                sp.Amount_LC__c  = shed.Split_Percent_3__c * shed.Revenue_LC__c  * .01;
                sp.Amount_USD__c = shed.Split_Percent_3__c * shed.Revenue_USD__c * .01; 

            }
           toUpdatesplits.add(sp); 
        }

    }
    if(toUpdatesplits != null && toUpdatesplits.size()>0){
        update toUpdatesplits;
    }
}

or if you have any other identifiers on the split records that can be used to pick the sales rep from the parent object, you can use that in the if statements.

Related Topic