I need to write a unit test on some business logic that happens after the Contact IsEmailBounced field = true. The field is not writeable though. My question is how can I create a test contact with IsEmailBounced = true?

///update I used JSON.deserialize to IsEmailBounced value. But it does not seem to persist when I update the value to pass into the Trigger. When the contact record hits the business logic is showing up as IsEmailBounced = false;

Test method

    static void contactHardBounceTest() {
        Contact c = [
                SELECT Id, IsEmailBounced
                FROM Contact
                LIMIT 1
        Contact bouncedContact = (Contact) JSON.deserializeStrict(
                '{"IsEmailBounced" : "true"}',
        bouncedContact.Id = c.Id;
        System.assertEquals(true, bouncedContact.IsEmailBounced);
        System.debug('bounced contact id ' + bouncedContact.Id);
        System.debug('bounced contact email status ' + bouncedContact.IsEmailBounced);

        update bouncedContact;
        CampaignMember updatedMember = [
                SELECT Id, Status
                FROM CampaignMember
                WHERE ContactId = :bouncedContact.Id
                LIMIT 1
        System.assertEquals('Bounced', updatedMember.Status, 'Failed assertion on updated member status');

Business logic

public static void updateContacts(List<Contact> newContacts,  Map<Id, Contact> oldContacts){
        List<Contact> unsubContacts = new List<Contact>();
        List<Contact> bouncedContacts = new List<Contact>();
        for(Contact c : newContacts){
            System.debug('Contact Id ' + c.Id);
            System.debug('Email Status in loop ' + c.IsEmailBounced);
            Contact oldContact = oldContacts.get(c.Id);
            if(c.HasOptedOutOfEmail == true && oldContact.HasOptedOutOfEmail == false){
            }if(c.IsEmailBounced == true){
        if (!bouncedContacts.isEmpty()) {

Often when you need to test against fields which are not writeable, serialization provides a good workaround.

Contact record = (Contact)JSON.deserialize(JSON.serialize(new Map<String, Object> {
    'IsEmailBounce' => true
}, Contact.class);
| improve this answer | |
  • Thank you Adrian, I will have to take a look. I have not used serialization much. – Brooks Johnson Aug 22 '19 at 16:35
  • I updated the original question I was able to use the JSON to set the IsEmailBounced field but it does not seem to persist after I call update on it. – Brooks Johnson Aug 22 '19 at 18:33
  • 1
    Just because you can create a record in memory doesn't mean you can update it with non-writable fields... You need to just call your service with this record. – Adrian Larson Aug 22 '19 at 18:34
  • Got it, thank you Adrian. – Brooks Johnson Aug 22 '19 at 18:37
  • And tests passed!! – Brooks Johnson Aug 22 '19 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.