You need to sign in to do that
Don't have an account?
Jason 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; } } }
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.
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?