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
Jason CorumJason Corum 

Different Logic Based on Positive vs. Negative Integers

Hello! I'm working on an Apex Class that gets called from a Trigger to update some related custom objects. I want different behavior based on whether an Integer variable is zero, positive or negative, however it seems like the variable is processed as positive even if it is negative. I've attempted to slim down what I have code-wise for the forum, so there may be some typos in what I include here, but this should give folks the gist. I'd appreciate some more eyes on this to see what I'm missing.
 
public class PopulateCustomObjects {

  public static void addDates(Set<Id> parentId){

      List<CustomObjects__c> customObjectsToUpdate = new List<CustomObjects__c>();

      // Get a List of CustomObjects Associated with the Parent

      for (List<CustomObjects__c> objects : [SELECT Id, Due_Date__c, Start_Date__c, Parent__r.Close_Date__c
                                              FROM CustomObjects__c
                                              WHERE CustomObjects__c_Lookup__c IN :parentId]){

        for(CustomObject__c object : objects){

        // Re-populate the Start Date and Due Date based on the days integer variable

          Integer days = object.Start_Date__c.daysBetween(object.Due_Date__c);

          if(days == 0) {

            object.Start_Date__c = date.today();
            object.Due_Date__c = object.Parent__r.Close_Date__c;

            customObjectsToUpdate.add(object);

          } else if(days > 0) {

            object.Start_Date__c = date.today();
            object.Due_Date__c = BusinessDaysClass.AddBusinessDays(date.today(), days);

            customObjectsToUpdate.add(object);
          } else {

            object.Start_Date__c = BusinessDaysClass.AddBusinessDays(object.Parent__r.Close_Date__c, days);
            object.Due_Date__c = object.Parent__r.Close_Date__c;

            customObjectsToUpdate.add(object);
          }
        }
      update customObjectsToUpdate;
    }
  }
}



 
YuchenYuchen
The code looks fine to me. So you are saying that "the variable is processed as positive even if it is negative", did you mean that when the days is negative it should enter the else block and update the Start Date and Due Date but it did not? Could you put some debug logs and see which block it enters and what is the value it is setting? I saw some methos "BusinessDaysClass.AddBusinessDays", not sure how that method is defined.
SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Hi Jason,

I would recommend adding a few debug statements to try and determine what is happening once you hit that inner loop. Also, I would probably recommend making sure that you are looping through records in which the 2 Custom dates are not null.
Jason CorumJason Corum
Thanks for replies Yuchen and Luis Luciani. Yuchen, you are correct that when days is negative it should enter the else block and update Start_Date__c and Due_Date__c. Just to isolate things, I first commented out all the field updates for Start_Date__c and Due_Date__c and through debug statements saw that the "days" integer was entering the right conditional statement. All good there.

Next, I replaced the "BusinessDaysClass.AddBusinessDays" method in the field updates with a simple "date.today().addDays(days)" to rule out anything from there going screwy and the issue was back. Then I just commented out the field updates in the else if conditional (my current test case actually has no "days" integer that should trigger it) and the field updates worked correctly.

Finally, re-instated the field updates in the else if conditional and set a Checkpoint on the "update customObjectsToUpdate;". The dates looked correct for a single instance of "CustomObjects__c" in "customObjectsToUpdate," but when I accessed it in the sandbox, the dates were incorrect and appeared as if they had been processed by the else if conditional - adding the days to "date.today().addDays(days)" as if they were a positive integer.

Any additional thoughts?