+ Start a Discussion
ElectronElectron 

Execute correct, but testClass doesn't work for trigger which update Contact and User both sides

Hi I don't post code before, and thought that's lazy and waste other one's time.

But I really don't get it, it works very well in Sandbox, but testClass doesn't give green light.

 

1. What I have made. 

 

  • There is one look up field link to User on Contact page, the contact is staff of our Org.
  • There have 2 trigger which update the Phone number for both sides

       Such as when Admin update the phone number on Staff page, trigger update it to User's profile, and opposite way.

 

2. I finished the 2 trigger, and then write the testClass.

    I tried a lot, but it doesn't work, then I try to test it with real data in sandbox, nothing wrong, even Chatter Free User could work with these triggers. 

 

3. Then I try to comment the System.Assert part, I could still get 100 % cover, but I don't feel that's the correct way I should do.

 

     I also reviewed my code a lot, I still can't find how to make the testClass correct. 

 

Trigger 1

trigger ChattterToStaff on User (after update) {
	
	for(User SFUser: trigger.new)
	{
		User oldSFUser = Trigger.oldMap.get(SFUser.Id);
		List<Contact> staff = new List<Contact>();
		staff = [SELECT Id from Contact Where User__c=:SFUser.Id];
		
		If(staff.size()==1)
		{
			If(oldSFUser.Phone != SFUser.Phone)
			{
				staff[0].Phone = SFUser.Phone;
				update staff;
			}
			If(oldSFUser.MobilePhone != SFUser.MobilePhone)
			{
				staff[0].MobilePhone = SFUser.MobilePhone;
				update staff;
			}		
		}
	}
}

 

Trigger 2

 

trigger StaffToChatter on Contact (after update) {
	
	for(Contact Staff : trigger.new)
	{
		If(Staff.User__c !=null)
		{
			Contact oldStaff = Trigger.oldMap.get(Staff.Id);
			User SFUser = new User();
			SFUser.Id = Staff.User__c;
			If(oldStaff.Phone != Staff.Phone)
			{
				SFUser.Phone = Staff.Phone;
				update SFUser;
			}
			If(oldStaff.MobilePhone != Staff.MobilePhone)
			{
				SFUser.MobilePhone = Staff.MobilePhone;
				update SFUser;
			}
		}
	}
}

 

TestClass

 

@isTest
private class testChatterWithStaff {

    static testMethod void myUnitTest() {
        Profile pf = [select Id from Profile where Name='Standard User'];
        User testUser1 = new User(
        alias = 'test1', 
        email='test1@noemail.com',
        emailencodingkey='UTF-8', 
        lastname='Testing', 
        languagelocalekey='en_US',
        localesidkey='en_US', 
        profileid = pf.Id, 
        country='United States',
        timezonesidkey='America/Los_Angeles', 
        username='test1@noemail.com');
        insert testUser1;
        
        Id StaffRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Staff').getRecordTypeId();
        //initial test Contact
        Contact testContact = new contact(FirstName='test', LastName='Test', RecordTypeId = StaffRecordTypeId);
        insert testContact;
        testContact.User__c = testUser1.Id;
        update testContact;
        System.Debug('User is '+testContact.User__c);//correct
        
        testContact.Phone = '212-975-4321';
        update testContact;
        System.Debug('testContact Phone is'+testContact.Phone);
        System.Debug('testUser1 Phone is ' +testUser1.Phone); //User is not updated.
        System.assertEquals(testContact.Phone , testUser1.Phone);
        testContact.MobilePhone = ' 604-684-7221';
        update testContact;
        System.assertEquals(testContact.MobilePHone , testUser1.MobilePhone);  
        
        testUser1.Phone = '202-456-1121';
        update testUser1;
        System.assertEquals(testUser1.Phone , testContact.Phone);
        testUser1.MobilePhone = '202-456-9713';
        update testUser1;
        System.assertEquals(testUser1.MobilePhone , testContact.MobilePhone);   

    }
}

 

    

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

When your trigger runs and updates a contact or user, that won't change the record in your test class.  You'll need to query the results from the database afresh and confirm that the change has happened.  So rather than:

 

testUser1.Phone = '202-456-1121';
update testUser1;
System.assertEquals(testUser1.Phone , testContact.Phone);

 you'd have something like:

 

testUser1.Phone = '202-456-1121';
update testUser1;
Contact contactFromDb=[select Phone from Contact where id=:testContact.id];
System.assertEquals(testUser1.Phone , contactFromDb.Phone);

 

 

All Answers

bob_buzzardbob_buzzard

When your trigger runs and updates a contact or user, that won't change the record in your test class.  You'll need to query the results from the database afresh and confirm that the change has happened.  So rather than:

 

testUser1.Phone = '202-456-1121';
update testUser1;
System.assertEquals(testUser1.Phone , testContact.Phone);

 you'd have something like:

 

testUser1.Phone = '202-456-1121';
update testUser1;
Contact contactFromDb=[select Phone from Contact where id=:testContact.id];
System.assertEquals(testUser1.Phone , contactFromDb.Phone);

 

 

This was selected as the best answer
ElectronElectron

It's the second time I made same mistake, I am so stupid for it. 

 

Thank you Keir again, I will be careful, stop this idiot question forever.