function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sunil2018sunil2018 

error in looping

i am writing a code  that iterate  and  add the first rate it finds, then replace it with any record that’s Current in Ag or Current in LA or Ag. So if a pending row happens to be before the Current in LA row (and there is no Current in Ag record), the pending rate will be sent to the service instead of the Current in LA row.
 Logic is missing to specifically look for "Current in Loan Accounting" row next if CurrentinAg is not there.  please provide any suggestions on it.



Map<Id, Rate_Stream__c> ratesForTakedown = new Map<Id, Rate_Stream__c>();
        for(Rate_Stream__c rateRecord : rates){
           
            if(ratesForTakedown.get(rateRecord.Loan__c) == null){
                ratesForTakedown.put(rateRecord.Loan__c, rateRecord);
            }
            else if(rateRecord.Pending_State__c == CurrentInAg ||
                rateRecord.Pending_State__c == CurrentInLAandAg){
                Integer rateIter = 0;
                While(rateIter < requestData.Rates.size()){
                    if(requestData.Rates.get(rateIter).TakedownId == rateRecord.Loan__c){
                        requestData.Rates.remove(rateIter);
                        break;
                    }
                    rateIter++;
                }                                    
            }
            else{
                continue; //We already have a rate for this takedown
            }
               
            RateRequest rate = new RateRequest();
            rate.TakedownId = rateRecord.Loan__c;
            rate.RateId = rateRecord.Id;
 
SubratSubrat (Salesforce Developers) 
Hello Sunil ,

To implement the logic of specifically looking for "Current in Loan Accounting" row next if CurrentInAg is not there, you can modify the existing code to include an additional check after the first "else if" condition.

Here is one way to implement this logic:
 
Map<Id, Rate_Stream_c> ratesForTakedown = new Map<Id, Rate_Stream_c>();
for (Rate_Stream__c rateRecord : rates) {

    if (ratesForTakedown.get(rateRecord.Loan__c) == null) {
        ratesForTakedown.put(rateRecord.Loan__c, rateRecord);
    } else if (rateRecord.Pending_State__c == CurrentInAg ||
               rateRecord.Pending_State__c == CurrentInLAandAg) {
        Integer rateIter = 0;
        while (rateIter < requestData.Rates.size()) {
            if (requestData.Rates.get(rateIter).TakedownId == rateRecord.Loan__c) {
                requestData.Rates.remove(rateIter);
                break;
            }
            rateIter++;
        }
    } else if (rateRecord.Pending_State__c == CurrentInLA) {
        // Check if we already have a rate for this takedown from CurrentInAg
        // If not, replace the existing rate with the CurrentInLA rate
        if (ratesForTakedown.get(rateRecord.Loan_c).Pending_State_c != CurrentInAg) {
            ratesForTakedown.put(rateRecord.Loan__c, rateRecord);
        }
    } else {
        continue; // We already have a rate for this takedown
    }

    RateRequest rate = new RateRequest();
    rate.TakedownId = rateRecord.Loan__c;
    rate.RateId = rateRecord.Id;
}

Hope this helps !
Thank you.​​​​​​​