3

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.

1
  • Could you indicate - with comments - from where in the code you want to jump to where? There are several if-statements, it's not clear to me which one you mean and where you want to go from there. Commented Mar 17, 2015 at 10:58

1 Answer 1

1

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.

3
  • If Vamsi's answer works, you can mark it as resolved to help other people.
    – SF_user
    Commented Mar 17, 2015 at 15:41
  • Hi Vamsi, i hv update on this requirement that if a sales rep is deleted then split record should also get deleted. so i hv added code if(i==0 && shed.Sales_Rep__c == null){ toDeleteSplits.add(sp);} which is working fine also, but then again updating will not create a new record. Please guide
    – user12051
    Commented Mar 18, 2015 at 6:31
  • is der any way in which i can compare old value of sales Rep and new value of Sales Rep on Revenue schedule ?
    – user12051
    Commented Mar 18, 2015 at 7:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .