You need to sign in to do that
Don't have an account?
Ryley Hayes
Update in Apex working Intermittently
I have a trigger on task, after insert and update, which is supposed to then go to a related Case and update a "last activity" field on Case.
The code seems to work. The Last Activity field on Case does get updated... usually. Maybe about 5% of the time, it doesn't happen. Trigger code below:
As you can see, I have some custom debugging in there. An example debug log from a case that did not update:
Debug logs from successes look the same. There doesn't seem to be any pattern to the instances where the update doesn't take. The same users have successes and failures alike. There isn't another process running after this updating the field again back to blank... this trigger and its test class are the only places the field is referenced. I'm stumped.
The code seems to work. The Last Activity field on Case does get updated... usually. Maybe about 5% of the time, it doesn't happen. Trigger code below:
trigger NewTaskTouchParentObject on Task (after insert, after update) { for (Task tsk:System.Trigger.new) { if(tsk.WhatId != null){ system.debug(tsk.WhatId); String whatId = tsk.WhatId; String whatIdPrefix = whatId.substring(0,3); ApexDebug__c Debugger = new ApexDebug__c(Log__c= 'WhatID = '+ WhatID + '\n' + 'WhatIdPrefix = ' + WhatIDPrefix + '\n' + 'Status = ' + tsk.Status + '\n' + 'AutoResponse = ' + tsk.Is_Autoresponse__c +'\n'); if (whatidPrefix.equalsIgnoreCase('500') && tsk.Status == 'Completed' && tsk.Is_Autoresponse__c == False) { //Updates case field when an activity is added Task MostRecentTask= [select ActivityDate, Type from Task where WhatId = :tsk.WhatID AND Status = 'Completed' ORDER BY ActivityDate DESC NULLS LAST LIMIT 1]; Debugger.Log__c = Debugger.Log__c + 'MostRecentTask.ActivityDate = ' + MostrecentTask.ActivityDate + '\n' + 'MostRecentTask.Type = ' + MostRecentTask.Type + '\n'; Integer TaskCount = [select COUNT() FROM Task where WhatId = :tsk.WhatID AND Status = 'Completed' AND Is_Autoresponse__c = false]; Debugger.Log__c = Debugger.Log__c + 'TaskCount = ' + TaskCount + '\n'; If(tsk.ActivityDate >= MostRecentTask.ActivityDate || MostRecentTask.ActivityDate==null){ Case acc = new Case(Id=tsk.WhatId, Last_Activity__c=tsk.ActivityDate, Last_Activity_Type__c = tsk.Type, Number_of_Complete_Activities__c = TaskCount); //casesToUpdate.put(tsk.WhatId, acc); Database.SaveResult srs = database.update(acc); Debugger.Log__c = Debugger.Log__c + 'acc.id = ' + acc.id + '\n' + 'acc.Last_Activity__c = ' + acc.Last_Activity__c + '\n' + 'If Statement = 1' +'\n' +srs.success +'\n' +srs.errors; } else{ Case acc = new Case(Id=tsk.WhatId, Last_Activity_Type__c = MostRecentTask.Type, Number_of_Complete_Activities__c = TaskCount); Database.SaveResult srs = database.update(acc); //casesToUpdate.put(tsk.WhatId, acc); Debugger.Log__c = Debugger.Log__c + 'acc.id = ' + acc.id + '\n' + 'acc.Last_Activity__c = ' + acc.Last_Activity__c + '\n' + 'If Statement = 2' +'\n' +srs.success +'\n' +srs.errors;} } insert Debugger; } } }
As you can see, I have some custom debugging in there. An example debug log from a case that did not update:
WhatID = 500d000000OsEfzAAF WhatIdPrefix = 500 Status = Completed AutoResponse = false MostRecentTask.ActivityDate = 2015-01-23 00:00:00 MostRecentTask.Type = Email TaskCount = 1 acc.id = 500d000000OsEfzAAF acc.Last_Activity__c = 2015-01-23 00:00:00 If Statement = 1 true ()As you can see, acc has both an id and date populated. The 'true' at the end is from the update SaveResults.Success, the '()' is from the update SaveResults.Errors... so the values are present in the sObject being updated, and the update is apparently occuring successfully with no errors. And yet, it isn't actually happening... the Last Activity field remains blank on the case.
Debug logs from successes look the same. There doesn't seem to be any pattern to the instances where the update doesn't take. The same users have successes and failures alike. There isn't another process running after this updating the field again back to blank... this trigger and its test class are the only places the field is referenced. I'm stumped.
Also, does the field have field history tracking enabled, so we may understand if it actually got updated and cleared out?