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
Matias Ramirez 2Matias Ramirez 2 

Before update test class.

Hello everyone!
I have a problem.
I'd like to prevent removing data in the Account's phone field, by using Apex code. I know you can do this in the UI, but I need to do it this way.
I've already done this:

Trigger:
trigger AccountTrigger2 on Account (before update) {
    AccountTriggerHandler handler = new AccountTriggerHandler();
 
    if (Trigger.IsUpdate && Trigger.IsBefore) {
        handler.onBeforeUpdate(Trigger.New, Trigger.OldMap);
    }
}


Handler:
Public class AccountTriggerHandler {

    public void onBeforeUpdate(List<Account> accountList, Map<Id, Account> accountOldMap) {
        
        for(Account aItem : accountList) {
            Account oldAccountItem = accountOldMap.get(aItem.Id);
            if(aItem.phone == null && oldAccountItem.phone != null) {
                aItem.phone = oldAccountItem.phone;
            }
        }
    }

}
This works well when testing in the UI, but the problems come with the test:
@isTest
public class AccountTriggerHandlerTest {

    @isTest
    public static void onBeforeUpdateTest(){
        Account test = new Account(name='Test 1', phone='1234');
        insert test;
        
        test.phone = NULL;
        update test;
        
        System.assertNotEquals(NULL, test.phone);
    }
    
}
I'd expect the phone to have 1234, but it has NULL, failing the test.
It says: System.AssertException: Assertion Failed: Same value: null
Any idea?

Best regards!
Harvey JethroHarvey Jethro
Hi Matias,
I'd like to believe that you need the addError() to prevent your phone field from being deleted/removed. I've made some changes to your code and acheived 100%. I wasnt able to look at it thoroughly. If it isnt giving you want u want pls explore the adderror().


@isTest
public class AccountTriggerHandlerTest {

    @isTest
    public static void onBeforeUpdateTest(){
        Account test1 = new Account(name='Test 1', phone='1234');
        Account test2 = new Account(name='Test 2', phone='null');
        insert test1;
        insert test2;
        
        test2.phone = test1.phone;
        update test1;
        
        System.assertNotEquals(NULL, test1.phone);
    }
    
}



Apex
Public class AccountTriggerHandler {

    public void onBeforeUpdate(List<Account> accountList, Map<Id, Account> accountOldMap) {
        
        for(Account aItem : accountList) {
            Account NewAccountItem = new Account ();
            Account oldAccountItem = accountOldMap.get(aItem.Id);
            if(NewAccountItem.phone == null && oldAccountItem.phone != null) {
                aItem.phone = oldAccountItem.phone;
            }
        }
    }

}

 
Raj VakatiRaj Vakati
Use this code 
 
@isTest
public class AccountTriggerHandlerTest {

    @isTest
    public static void onUpdateTrigger(){
        Account test = new Account(name='Test 1', phone='1234');
        insert test;
		
        test.phone = null;
        update test;
        
    }
    
}

 
Ajay K DubediAjay K Dubedi
Hi Matias,
Try this test class code:
@isTest
public class AccountTriggerHandlerTest {

    @isTest
    public static void onBeforeUpdateTest(){
        Account test = new Account(name='Test 1', phone='1234');
        insert test;
        
        test.phone = '';
        update test;
        
        System.assertNotEquals(NULL, test.phone);
    }
    
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Matias Ramirez 2Matias Ramirez 2
Thank you all for your answers.
I made a little bit research, and the problem was that in the test I was comparing with the object in memory, not the Account sObject in SF. So, I had to re-query to get the updated sObject.
Just in case someone else needs, here is the code for the test:
@isTest
public class AccountTriggerHandlerTest {

    @isTest
    public static void onBeforeUpdateTest(){
        Account test = new Account(name='Test 1', phone='1234');
        insert test;
        
        test.phone = NULL;//Trying to delete phone. It shouldn't.
        update test;
        
        //Re-query to get updated sObject.
        Account a = [SELECT Id, phone FROM Account WHERE Id = :test.Id];
        
        System.assertNotEquals(NULL, a.phone);
    }
    
}