+ Start a Discussion
theitdeptrockstheitdeptrocks 

Field returning incorrect number (398.0 instead of 0)

Hi all,

 

Off of our Opportunity object, we have a related object named "Distributions."  I am building a trigger so that as each Distribution record reaches a certain stage, it can be added to a count on the related Plan object.  I have it to the point where if manually triggered, the count increases correctly.  Originally I was having issues with bulk updates, but have that most of the way worked out except for this issue.

 

In my test file, I have 299 Distribution records for the same Opportunity.  Prior to updating them, that Opportunity's count is set to 0.  Upon uploading the update through data loader, I would expect my final count to be 299.  However, it is returning 595.

 

It looks like when it grabs the existing Diagnostic Count from the Opportunity, it isn't seeing 0, but is seeing 398.0.

 

Here is the first few records' output from the debug log:

15:54:00.465|USER_DEBUG|[33]|DEBUG|Was an update!!!
15:54:00.465|METHOD_EXIT|[33]|System.debug(ANY)
15:54:00.465|METHOD_ENTRY|[35]|MAP.get(ANY)
15:54:00.466|METHOD_EXIT|[35]|MAP.get(ANY)
15:54:00.466|METHOD_ENTRY|[44]|System.debug(ANY)
15:54:00.466|USER_DEBUG|[44]|DEBUG|Met the criteria!!!
15:54:00.466|METHOD_EXIT|[44]|System.debug(ANY)
15:54:00.466|METHOD_ENTRY|[46]|System.debug(ANY)
15:54:00.466|USER_DEBUG|[46]|DEBUG|The current plan id is:  0066000000FO4sDAAT
15:54:00.466|METHOD_EXIT|[46]|System.debug(ANY)
15:54:00.466|METHOD_ENTRY|[50]|MAP.containsKey(ANY)
15:54:00.466|METHOD_EXIT|[50]|MAP.containsKey(ANY)
15:54:00.467|METHOD_ENTRY|[56]|MAP.containsKey(ANY)
15:54:00.467|METHOD_EXIT|[56]|MAP.containsKey(ANY)
15:54:00.467|METHOD_ENTRY|[57]|System.debug(ANY)
15:54:00.467|USER_DEBUG|[57]|DEBUG|Key was not found and the Plans QDC is:  398.0
15:54:00.467|METHOD_EXIT|[57]|System.debug(ANY)
15:54:00.467|METHOD_ENTRY|[59]|System.debug(ANY)
15:54:00.467|USER_DEBUG|[59]|DEBUG|Before the if/else, the current count is:  398.0
15:54:00.467|METHOD_EXIT|[59]|System.debug(ANY)
15:54:00.467|METHOD_ENTRY|[69]|MAP.put(ANY, ANY)
15:54:00.467|METHOD_EXIT|[69]|MAP.put(ANY, ANY)
15:54:00.467|METHOD_ENTRY|[70]|System.debug(ANY)
15:54:00.467|USER_DEBUG|[70]|DEBUG|Went the route of plan count not 0 or null and was 398.0
15:54:00.467|METHOD_EXIT|[70]|System.debug(ANY)
15:54:00.467|METHOD_ENTRY|[71]|System.debug(ANY)
15:54:00.468|METHOD_ENTRY|[71]|MAP.get(ANY)
15:54:00.468|METHOD_EXIT|[71]|MAP.get(ANY)
15:54:00.468|USER_DEBUG|[71]|DEBUG|399.0
15:54:00.468|METHOD_EXIT|[71]|System.debug(ANY)
15:54:00.468|METHOD_ENTRY|[35]|MAP.get(ANY)
15:54:00.468|METHOD_EXIT|[35]|MAP.get(ANY)
15:54:00.468|METHOD_ENTRY|[44]|System.debug(ANY)
15:54:00.468|USER_DEBUG|[44]|DEBUG|Met the criteria!!!
15:54:00.468|METHOD_EXIT|[44]|System.debug(ANY)
15:54:00.468|METHOD_ENTRY|[46]|System.debug(ANY)
15:54:00.469|USER_DEBUG|[46]|DEBUG|The current plan id is:  0066000000FO4sDAAT
15:54:00.469|METHOD_EXIT|[46]|System.debug(ANY)
15:54:00.469|METHOD_ENTRY|[50]|MAP.containsKey(ANY)
15:54:00.469|METHOD_EXIT|[50]|MAP.containsKey(ANY)
15:54:00.469|METHOD_ENTRY|[52]|System.debug(ANY)
15:54:00.469|USER_DEBUG|[52]|DEBUG|Key was already found
15:54:00.469|METHOD_EXIT|[52]|System.debug(ANY)
15:54:00.469|METHOD_ENTRY|[53]|MAP.put(ANY, ANY)
15:54:00.469|METHOD_ENTRY|[53]|MAP.get(ANY)
15:54:00.469|METHOD_EXIT|[53]|MAP.get(ANY)
15:54:00.469|METHOD_EXIT|[53]|MAP.put(ANY, ANY)
15:54:00.469|METHOD_ENTRY|[54]|System.debug(ANY)
15:54:00.469|METHOD_ENTRY|[54]|MAP.get(ANY)
15:54:00.469|METHOD_EXIT|[54]|MAP.get(ANY)
15:54:00.469|USER_DEBUG|[54]|DEBUG|Current count at:  400.0
15:54:00.469|METHOD_EXIT|[54]|System.debug(ANY)
15:54:00.469|METHOD_ENTRY|[35]|MAP.get(ANY)
15:54:00.470|METHOD_EXIT|[35]|MAP.get(ANY)
15:54:00.470|METHOD_ENTRY|[44]|System.debug(ANY)
15:54:00.470|USER_DEBUG|[44]|DEBUG|Met the criteria!!!
15:54:00.470|METHOD_EXIT|[44]|System.debug(ANY)
15:54:00.470|METHOD_ENTRY|[46]|System.debug(ANY)
15:54:00.470|USER_DEBUG|[46]|DEBUG|The current plan id is:  0066000000FO4sDAAT
15:54:00.470|METHOD_EXIT|[46]|System.debug(ANY)
15:54:00.470|METHOD_ENTRY|[50]|MAP.containsKey(ANY)
15:54:00.470|METHOD_EXIT|[50]|MAP.containsKey(ANY)
15:54:00.470|METHOD_ENTRY|[52]|System.debug(ANY)
15:54:00.470|USER_DEBUG|[52]|DEBUG|Key was already found
15:54:00.470|METHOD_EXIT|[52]|System.debug(ANY)
15:54:00.470|METHOD_ENTRY|[53]|MAP.put(ANY, ANY)
15:54:00.470|METHOD_ENTRY|[53]|MAP.get(ANY)
15:54:00.471|METHOD_EXIT|[53]|MAP.get(ANY)
15:54:00.471|METHOD_EXIT|[53]|MAP.put(ANY, ANY)
15:54:00.471|METHOD_ENTRY|[54]|System.debug(ANY)
15:54:00.471|METHOD_ENTRY|[54]|MAP.get(ANY)
15:54:00.471|METHOD_EXIT|[54]|MAP.get(ANY)
15:54:00.471|USER_DEBUG|[54]|DEBUG|Current count at:  401.0
...
This continues to 497.0.  The final count on the field is 595?

 Here is the code:

...
map<string,decimal> OppIDsMap = new map<string, decimal>();
if(trigger.isUpdate){
    system.debug('Was an update!!!');
    for(Distribution_Tracking__c d:trigger.new){
        Distribution_Tracking__c beforeUpdate = System.Trigger.oldMap.get(d.Id);
            if(beforeUpdate.Status__c != 'Check Requested' && 
	       d.Status__c == 'Check Requested' && 
	       d.Do_Not_Bill__c == false && 
	       d.Count_on_Invoice__c == false &&
	       (
                    d.Distribution_Check_Type__c == 'Beneficiary/Deceased' || 
                    d.Distribution_Check_Type__c == 'Hardship' || 
		 d.Distribution_Check_Type__c == 'Loan' || 
		 d.Distribution_Check_Type__c == 'Termination')){
		    system.debug('Met the criteria!!!');
		    string currentplanid = d.Plan__c;
		    system.debug('The current plan id is:  ' + d.Plan__c);
			   		
                    if(OppIDsMap.containsKey(d.Plan__c)==true){
                    system.debug('Key was already found');
                    OppIDsMap.put(d.Plan__c,OppIDsMap.get(d.Plan__c) +1);
                    system.debug('Current count at:  ' + OppIdsMap.get(d.Plan__c));
                    }
                    
                    else if(OppIDsMap.containsKey(d.Plan__c) != true){
                    system.debug('Key was not found and the Plans QDC is:  ' +
                     d.Plan_s_Current_Quarterly_Dist_Count__c);
		decimal currentcount = d.Plan_s_Current_Quarterly_Dist_Count__c;
		system.debug('Before the if/else, the current count is:  ' + currentcount);
		if(currentcount == 0 || currentcount == null){
		    system.debug(d.Plan__c);
		    system.debug(d.Plan_s_Current_Quarterly_Dist_Count__c);
		    OppIDsMap.put(d.Plan__c,1);
		    system.debug('Went the route of plan count = 0 or null:' + currentcount);
		}
			   			
                    else if(currentcount >= 1){
                        OppIDsMap.put(d.Plan__c,currentcount+1);
                        system.debug('Went the route of plan count not 0 or null and was ' + currentcount);
                        system.debug(OppIDsMap.get(d.Plan__c));
                    }
			   			
                    d.Count_on_Invoice__c = true;
}
}
}
		
		Set <String> planSet = new Set<String>();
		planSet = OppIDsMap.keySet();
		list<Opportunity> theAffectedPlans = new list<Opportunity>([Select Id, Quarterly_Distribution_Count__c From Opportunity where ID in :planSet]);
		
		for(Opportunity o:theAffectedPlans){	
			o.Quarterly_Distribution_Count__c = OppIDsMap.get(o.ID);
		}
		update theAffectedPlans;
		system.debug('Plans updated!!!');
}

 

Does anyone see where I went wrong? 

 

 I created a formula field, d.Plan_s_Current_Quarterly_Dist_Count__c, because d.Plan__r.Quarterly_Distribution_Count__c was returning null.

The formula is 'Plan__r.Quarterly_Distribution_Count__c' and is set to a number with 2 decimal places.  The Opportunity's Quarterly_Distribution_Count__c field is a number with 0 decimal places.  Could this be it?  I'll change the formula to 0 decimal places and see what happens in the mean time, but does anyone have any ideas of where I went wrong?


Thanks!

theitdeptrockstheitdeptrocks

Changing the formula to have 0 decimal places yielded the same result.  So I must be doing something incorrectly above.  Any ideas? 

theitdeptrockstheitdeptrocks

I just noticed that when I mass update those 299 records, two log files are being generated.  Within the first one, I can actually see where the initial Quarterly Distribution Count is indeed 0, and the then the correct path is take and I see all of the entries, 1 through 100 in this log, then it goes onto detail about another trigger.


When I open the 2nd log file, I see debug information making me thing that the code was triggered again... I see my  "Was an Update!!" debug message at the top.  Interestingly enough though, the first Current Quarterly Distribution Count it grabs is 398.  I then see it iterate through all the way up to 497 and then the rest of the log is unrelated.

 

The plot thickens.

Was an update!!!
Was an update!!!