0

I have run the following code in salesforce apex which compiles correctly. However when the user enters the value for the amount_paid field, I get aa runtime error for the following line of code:

Construction_linked_schedule__c NextFlatid = allFlatids[i+1] ;

Basically what I want to do is capture the user input of amount paid for a given Flat_ id and search in runtime whether there are any other records with the same Flat_id.(This happens using an after Trigger in the test class) Post which data manipulation happens on the records of fields of the NextFlatid based on the result. This error happens when user input happens for a NewFlatid when there are no matching records for the given Flat_id. I know that it is possible to query the records using Dynamic SOSL or using an Iterator based on the user input - but wanted to know the syntax for this . Thanks

    public class TotalAmountUpdate {
          public static void TotalAmUpdate (Construction_Linked_Schedule__c [] TAPNEW)  {
           for (Construction_Linked_Schedule__c c :TAPNEW ) {

               List< Construction_linked_schedule__c > allFlatids =  [SELECT                 Amount_Paid__c, Total_Amount_Paid__c , Milestone_number__c ,Flat_Id__c , 
               Flat_Price__c FROM Construction_Linked_Schedule__c ];

                Integer i = 0 ;
                Integer x = allFlatids.size();

                while ((i < x) && (x >= 2)) {

                Construction_linked_schedule__c currentFlatid = allFlatids[i] ;
                Construction_linked_schedule__c NextFlatid = allFlatids[i+1] ;


                  IF ((currentFlatid.Flat_id__c  == NextFlatid.Flat_id__c) && (       NextFlatid.Milestone_number__c - currentFlatid.Milestone_number__c  == 1 ) && 
         (currentFlatid.Total_Amount_Paid__c < currentFlatid.Flat_Price__c) &&
        (currentFlatid.Total_Amount_Paid__c + NextFlatid.Total_Amount_Paid__c < NextFlatid.Flat_Price__c))
        {
            NextFlatid.Total_Amount_Paid__c = NextFlatid.Total_Amount_Paid__c + currentFlatid.Total_Amount_Paid__c ;
        }

          Update NextFlatid ;

           i = i + 1 ;

         } 

     }
   }
 }

1 Answer 1

0

I suggest you make the trigger bulk ready (no select within for) and avoid using indices.

Some years ago I made the following duplicate checker. Nowadays you can use Duplicate Management. But maybe it shows you the right direction.

public static void BeforeInsertUpdate(Contact[] newContacts) {
    final String errMsgEmail = 'Emailadres bestaat reeds voor een ander contact: ';

    // What do we have
    Set<String> emailSet = new Set<String>();
    Set<Id> idSet = new Set<Id>();

    For (Contact c : newContacts) {
        // Fill sets
        idSet.add(c.id);
        if (c.email != null) {
            emailSet.add(c.Email);
        }
    } 

    // Find other records with same emailadresses
    Map< String, Id > duplicateContactMap = new Map< String, Id >();
    for( Contact c : [select Id, Email from Contact where Email in :emailSet and Id not in :idSet] )
        duplicateContactMap.put( c.Email, c.Id );

    // Main check loop
    for( Contact c : newContacts ){
        // Check for duplicate email
        if (c.email != null) {
            Id duplicateContactId = duplicateContactMap.get( c.Email );
            if( duplicateContactId != null) {
                c.addError( errMsgEmail + duplicateContactId );
                continue;
            }
        }
    }           
}
3
  • Thanks Peter and Samuel .. definitely shows the direction. In this case once the duplicate is found we also need to perform data manipulation of the duplicate for which have used the while loop ! if the record is with a new flat_id it should not enter the while loop ! Apr 16, 2015 at 9:22
  • Also would appreciate if there is anyway we can guard against an infinite loop..ie the Data manipulation of the duplicates should happen only once. Tried to add conditions in the code to prevent an infinite loop but still not completely foolproof Apr 16, 2015 at 9:24
  • The above code only contains finite loops. Apr 16, 2015 at 10:00

You must log in to answer this question.

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