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
cpo87cpo87 

Apex Trigger works in Sandbox but not in Eclipse

I have created a field on account that behaves like the Last Activity field except the task doesn't have to be marked with a status of Completed and the date populated on the account is the tasks Last Modified Date. The point of this field is to show the most recent time a task was worked on a given account. My code also takes into consideration tasks associated to an opportunity or a contact and roles it up to the account.

 

The issue I'm having is that the task and test code execute properly in my Sandbox, coverage of 100%, but when I execute it in Eclipse my system.assert code fails because it is saying that the acct.Last_Activity_Modified_Date__c is not equal to the task's last modified date. For some reason in Eclipse the acct.Last_Activity_Modified_Date__c is Null.

 

Why would the code work in sandbox but not Eclipse?

 

Please help! See trigger, unit tests, and debug log below.

 

trigger UpdateLastActivityDate on Task (after insert, after update) { Task task = Trigger.new[0]; Account acct = new Account(); //Determine if the task WhatId (Related To) field is populated if(task.WhatId!=null){ //Get the first 3 digits of the WhatId to determine which object it is referring to String tPrefix = task.WhatId; tPrefix = tPrefix.subString(0,3); //If the object is an account then update the account Last_Activity_Modified_Date to the task Last_Modified_Date if(tPrefix == '001'){ for(Account a:[Select Id, Last_Activity_Modified_Date__c from Account where Id = :task.whatId limit 1]){ acct = a; Datetime t = task.LastModifiedDate; Date d = Date.newInstance(t.year(),t.month(),t.day()); if(d > acct.Last_Activity_Modified_Date__c || acct.Last_Activity_Modified_Date__c == null) acct.Last_Activity_Modified_Date__c = d; update acct; } } //If the object is an opportunity then get the account id and then update the account Last_Activity_Modified_Date to the task Last_Modified_Date if(tPrefix == '006'){ for(Opportunity o:[Select AccountId from Opportunity]){ for(Account a:[Select Id, Last_Activity_Modified_Date__c from Account where Id = :o.AccountId limit 1]){ acct = a; Datetime t = task.LastModifiedDate; Date d = Date.newInstance(t.year(),t.month(),t.day()); if(d > acct.Last_Activity_Modified_Date__c || acct.Last_Activity_Modified_Date__c == null) acct.Last_Activity_Modified_Date__c = d; update acct; } } } } //Determine if the WhatId is not populated but the WhoID(Name) is (lead or Contact) if(task.WhatId==null && task.WhoId!=null){ //Get the first 3 digits of the WhoId to determine which object it is referring to String tPrefix2 = task.WhoId; tPrefix2 = tPrefix2.subString(0,3); //If the object is a contact then get the account id and then update the account Last_Activity_Modified_Date to the task Last_Modified_Date if(tPrefix2 == '003'){ for(contact c:[Select AccountId from Contact where Id = :task.WhoId]){ for(Account a:[Select Id, Last_Activity_Modified_Date__c from Account where Id = :c.AccountId limit 1]){ acct = a; Datetime t = task.LastModifiedDate; Date d = Date.newInstance(t.year(),t.month(),t.day()); if(d > acct.Last_Activity_Modified_Date__c || acct.Last_Activity_Modified_Date__c == null) acct.Last_Activity_Modified_Date__c = d; update acct; } } } } }

 

@isTest private class TESTLastActivityModifiedDate { static testMethod void testUpdateLastActivityDate(){ Account[] acct = new Account[]{ new Account(Name = 'Test cpoAccount0',Type = 'Vendor',Industry = 'Other',ERP_Acct_App__c = 'Other'), new Account(Name = 'Test cpoAccount1',Type = 'Vendor',Industry = 'Other',ERP_Acct_App__c = 'Other'), new Account(Name = 'Test cpoAccount2',Type = 'Vendor',Industry = 'Other',ERP_Acct_App__c = 'Other') }; insert acct; Date cd = Date.newInstance(2010,02,02); Opportunity oppty = new Opportunity(Name = 'Test cpoOppty', StageName = 'Suspect - 0%', CloseDate = cd, AccountId = acct[1].Id); insert oppty; Contact cont = new Contact(LastName = 'Test cpoContact', AccountId = acct[2].Id); insert cont; Task[] task = new Task[]{ new Task(Subject = 'cpoTest0', whatId = acct[0].Id), new Task(Subject = 'cpoTest1', whatId = oppty.Id), new Task(Subject = 'cpoTest2', whoId = cont.Id) }; Test.startTest(); insert task[0]; insert task[1]; insert task[2]; Test.stopTest(); task[0] = [SELECT ID, LastModifiedDate FROM Task WHERE Subject = 'cpoTest0']; Datetime t0 = task[0].LastModifiedDate; Date d0 = Date.newInstance(t0.year(),t0.month(),t0.day()); acct[0] = [SELECT ID, Last_Activity_Modified_Date__c FROM Account WHERE Name = 'Test cpoAccount0']; system.debug(d0); system.debug(acct[0].Last_Activity_Modified_Date__c); system.assert(acct[0].Last_Activity_Modified_Date__c == d0); task[1] = [SELECT ID, LastModifiedDate FROM Task WHERE Subject = 'cpoTest1']; Datetime t1 = task[1].LastModifiedDate; Date d1 = Date.newInstance(t1.year(),t1.month(),t1.day()); acct[1] = [SELECT ID, Last_Activity_Modified_Date__c FROM Account WHERE Name = 'Test cpoAccount1']; system.assert(acct[1].Last_Activity_Modified_Date__c == d1); task[2] = [SELECT ID, LastModifiedDate FROM Task WHERE Subject = 'cpoTest2']; Datetime t2 = task[2].LastModifiedDate; Date d2 = Date.newInstance(t2.year(),t2.month(),t2.day()); acct[2] = [SELECT ID, Last_Activity_Modified_Date__c FROM Account WHERE Name = 'Test cpoAccount2']; system.assert(acct[2].Last_Activity_Modified_Date__c == d2); } }

Debug Log:

*** Beginning Test 1: TESTLastActivityModifiedDate.static testMethod void testUpdateLastActivityDate()

20100115212535.035:Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 10, column 5: Insert: LIST:SOBJECT:Account
20100115212535.035:Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 14, column 5: Insert: SOBJECT:Opportunity
20100115212535.035:Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 17, column 5: Insert: SOBJECT:Contact
20100115212535.035:Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 26, column 5: Insert: SOBJECT:Task
20100115212535.035:Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 27, column 5: Insert: SOBJECT:Task
20100115212535.035:Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 28, column 5: Insert: SOBJECT:Task
20100115212535.035:Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 28, column 5:     DML Operation executed in 86 ms
20100115212535.035:Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 31, column 15: SOQL query with 1 row finished in 767 ms
20100115212535.035:Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 36, column 15: SOQL query with 1 row finished in 20 ms
20100115212535.035:Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 38, column 5: 2010-01-15 00:00:00
20100115212535.035:Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 39, column 5: null
System.Exception: Assertion Failed

Class.TESTLastActivityModifiedDate.testUpdateLastActivityDate: line 41, column 5
External entry point

 

pgraffmanpgraffman

Did you ever get this resolved? I am tackling the same trigger development and was wondering if you would be willing to share your code with me