+ Start a Discussion
KD_1978KD_1978 

Apex Trigger Succeeds but field not Updated.

I have created a trigger which simply fails to update a field and I can't see why.

 

The tirgger looks at a new task coming in and compares the phone number on the task (t.Five9__Five9DNIS__c) with a phone number on the contact.  If they don't match.  Then it sets a custom field on the task.

 

I have have set up a test.   When I run it I use debug statements in the trigger to verify:  

* the trigger correctly identifies the mismatch

* the value of the field is changed.

 

However, the assertEquals (the final line in the test) fails!

 

What gives?

 

The Trigger:

 

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

    list<id> contactIds = new list<id>();
    for (Task t : trigger.new)
    {
        contactIds.add(t.whoid);
    }
    
    map<id,contact> contactMap = new map<id,contact>([Select Id, FirstName, LastName, DonorId__c, Phone from contact Where Id in :contactIds ]);
    // list<task> updatedTasks = new list<task>();
    
    for (Task t : trigger.new)
    {
        contact c = contactMap.get(t.whoid);
        
        System.Debug('/n/n*******       Phone ' + c.Phone + ' DNIS: ' + t.Five9__Five9DNIS__c + '       ********');
        
        
        if (c != null)
        {                        
            if (t.Five9__Five9DNIS__c != c.Phone)
            {
                System.Debug('DNIS DOES NOT MATCH');
                t.MismatchPhone__c = true;
                System.Debug('VAL IS: ' + t.MismatchPhone__c);                
            }                                    
        }
    }
}

 

The Test (only the final block of code is relevant):

 

@istest
private class TriggerTests {

   static testMethod void contactBeforeUpdate()
   {
     contact c = new contact();
     c.lastname = 'Smith';
     insert c;
     
     c.address1__c = '123 Main Street';
     c.email = 'test@company.com';
     c.Phone = '1234567890';
     update c;

        
     user u2 = [select id from user where isactive = true limit 1];
   
     task t = new task();
     t.ownerid = u2.id;
     t.whoid = c.id;
     t.subject = 'test';
     t.CallDisposition = 'Yes - Gift Info Acquired';
     t.Five9__Five9DNIS__c = '1234567890';
     
     insert t; 
          
     System.assertEquals(false, t.MismatchPhone__c);

     <OMITTING IRRELEVANT TESTS>               
     
     task t3 = new task();
     t3.ownerid = u2.id;
     t3.whoid = c.id;
     t3.subject = 'test2';
     t3.CallDisposition = 'Callback';   
     t3.Five9__Five9DNIS__c = '0987654321';
 
     insert t3;
     
     // THIS LINE FAILS!!!!!
     System.assertEquals(true, t3.MismatchPhone__c);
 
   }

}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

In the context of your Test case, the value of T3 hasn't changed.  Yes, the trigger was fired, but your test case need to re-query T3 in order to see the new values after the insert.

 

If you add the following just before your Asserts, it might fix it up.

 

t3 = [select MismatchPhone__c from task where id = :t3.id];

 

 

You need to do the same for the previous case, t, because it's not really testing that the trigger didn't change the value.

 

Best, Steve.

All Answers

SRKSRK

This is happening only with test casr or with real cases also

if it just with tase case create u different test methods in the same test class

SteveBowerSteveBower

In the context of your Test case, the value of T3 hasn't changed.  Yes, the trigger was fired, but your test case need to re-query T3 in order to see the new values after the insert.

 

If you add the following just before your Asserts, it might fix it up.

 

t3 = [select MismatchPhone__c from task where id = :t3.id];

 

 

You need to do the same for the previous case, t, because it's not really testing that the trigger didn't change the value.

 

Best, Steve.

This was selected as the best answer
Rahul SharmaRahul Sharma

Yes Steve is correct, you need to check the value from Database as the value in your variable t and t3 would not be updated.

Just add a Query to fetch the exact value of field to be asserted.