+ Start a Discussion

System.AssertException: Assertion Failed

Hi, I am getting error as ''System.AssertException: Assertion Failed'. Please help.

Apex Trigger: 

  trigger FirstActivityDate on Task (before insert, before update) {

    List<Id> leadIds=new List<Id>();
    List<Id> ContatcIds=new List<Id>();
    for(Task t:trigger.new){
    // Checking the condition for the trigger to fire 
        if(t.Status=='Completed' && t.Created_by_Role__c == TRUE && (t.whoId != null)){
    //Querying the related Lead and Contact based on whereId on Task
    List<Lead> leadsToUpdate=[SELECT Id, First_Activity_Date__c FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE AND First_Activity_Date__c=NULL];
    List<Contact> contactToUpdate=[SELECT Id, First_Activity_Date__c FROM Contact WHERE Id IN :ContatcIds AND First_Activity_Date__c=NULL];
     // updating the Lead and contact First Activity Date field
    For (Lead l:leadsToUpdate){
        l.First_Activity_Date__c = date.today();
    For (Contact c:contactToUpdate){
        c.First_Activity_Date__c = date.today();
        update leadsToUpdate;
          update contactToUpdate;
            }catch(DMLException e){
        system.debug('Leads were not all properly updated.  Error: '+e);

Apex Class :

public class FirstActivityDateTest {
    static testMethod void myUnitTest() {
        //Create a test Lead
        Lead ld = new Lead();
            ld.FirstName = 'TestFirstName';
            ld.LastName = 'TestLastName';
            ld.Company = 'TestCompany';
            ld.CountryCode = 'US';
        insert ld;
        //Assert that First_Activity_Date__c = Null
        System.assertEquals(ld.First_Activity_Date__c, null);

        //Create a test user
        list<Profile> p1 = [SELECT Id FROM Profile WHERE Name like 'System Administrator'];
        User u1 = new User();
            u1.FirstName = 'fName';
            u1.LastName = 'lName';
            u1.Email = 'Test1234567@mail.com';
            u1.Username = 'Test1234567@mail.com';
            u1.Alias ='Test123';
            u1.CommunityNickname = 't123';
            u1.TimeZoneSidKey ='America/New_York';
            u1.LocaleSidKey = 'en_US';
            u1.EmailEncodingKey = 'UTF-8';
            u1.ProfileId = p1[0].id;
            u1.LanguageLocaleKey = 'en_US';
            u1.Country = 'United Kingdom';
        insert u1;

        //Create a test Contact
        Contact ct = new Contact();
            ct.FirstName = 'TestFirstName';
            ct.LastName = 'TestLastName';
        insert ct;
       //Assert that First_Activity_Date__c = Null
        System.assertEquals(ct.First_Activity_Date__c, null);

        //Create a test Task
        Task tk = new Task();
            tk.Subject = 'TestSubject';
            tk.ActivityDate = date.today();
            tk.OwnerId = u1.Id;
            tk.Status = 'Completed';
            tk.WhoId = ld.Id;
        insert tk;
         //Create a test Task
        Task t= new Task();
            t.Subject = 'TestSubject';
            t.ActivityDate = date.today();
            t.OwnerId = u1.Id;
            t.Status = 'Completed';
            t.WhoId = ct.Id;
        insert t;

        //Assert that First_Activity_Date__c != null
        ld = [SELECT Id, First_Activity_Date__c FROM Lead WHERE Id = :ld.Id limit 1];
        System.assertEquals(ld.First_Activity_Date__c != null);
Marco RodriguezMarco Rodriguez

if you modify a record during a trigger execution it won't modify the copy of the object being referenced by the class, so altough you assigned date.today() to First_Activity_Date__c, this change is not "visible" from the copy referenced by the class, what you need to do is run a SOQL query to retrieve the modified version from the Database.

The only field that's updated in the class referenced copy of the object when performing a DML is the Id field during an insert operation.

Hope that helps.

Hi Marco,

I didn't quite understand it. What should be my code look like?