You need to sign in to do that
Don't have an account?
Matias 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:
Handler:
It says: System.AssertException: Assertion Failed: Same value: null
Any idea?
Best regards!
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!
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;
}
}
}
}
Try this test class code: I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
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: