I am setting a field using based on one other field value using Trigger when new record is created or existing record updated. The trigger works fine. I have written a Test class for the Trigger but somehow, it doesn't cover a single line of the Trigger. I have gone through almost all topics here but nothing seems to work.
Trigger:
trigger SMS_OPT_OUT on Account (before insert, before update) {
for (Account A : Trigger.new) {
if(Trigger.isInsert) {
A.PersonHasOptedOutOfSms__c = A.PersonHasOptedOutOfEmail;
} else if(Trigger.isUpdate) {
Account oldA = Trigger.oldMap.get(A.Id);
if (A.PersonHasOptedOutOfSms__c != oldA.PersonHasOptedOutOfSms__c || A.PersonHasOptedOutOfEmail != oldA.PersonHasOptedOutOfEmail) {
if (A.PersonHasOptedOutOfSms__c == oldA.PersonHasOptedOutOfSms__c && A.PersonHasOptedOutOfEmail != oldA.PersonHasOptedOutOfEmail) {
A.PersonHasOptedOutOfSms__c = A.PersonHasOptedOutOfEmail;
}
}
}
}
}
Test Class:
@isTest
public class SMS_OPT_OUT_TC{
static testmethod void triggerTest(){
RecordType personAccountRecordType = [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];
User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
System.runAs ( thisUser ) {
Account a = NEW Account();
a.FirstName='John';
a.LastName='DoeTest1';
a.PersonEmail = '[email protected]';
a.PersonHasOptedOutOfEmail = false;
a.PersonHasOptedOutOfSms__c = true;
a.RecordType = personAccountRecordType;
insert a;
a = [SELECT Id FROM Account WHERE Id =:a.Id];
a.PersonHasOptedOutOfSms__c = false;
update a;
}
}
}
What am I doing wrong here?!
PersonHasOptedOutOfEmail
is a Person Account field. You might try modifying your insert to include the recordtype for the Person Account type on it and see if the behavior changes. salesforce.stackexchange.com/a/29833/660