0

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?!

8
  • Just curious...I could be wrong here...after inserting "a"...have you tried querying the record back and set the "personhasoptedoutofsms__c" field ?
    – Varun
    Commented Oct 8, 2015 at 0:55
  • 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
    – Mark Pond
    Commented Oct 8, 2015 at 1:14
  • This might sound stupid but have you executed your test class? By clicking run test? If yes, is there any error you receive while inserting Account? Does the test passed
    – manjit5190
    Commented Oct 8, 2015 at 3:15
  • @ZenSeeker: yes, I tried that as well. Didn't worked.
    – Neerav
    Commented Oct 8, 2015 at 3:40
  • @MarkPond: I tried that approach. Not working.
    – Neerav
    Commented Oct 8, 2015 at 3:41

1 Answer 1

0

Not sure why but when I run test on 'Apex Test Execution' page, it works and covers Trigger code. Weird that its not working when I run test from Developer console. Anyway, I am happy that I can finally deploy it. Thanks @manjit_singh for your valuable direction.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .