0

I've been searching the Salesforce Developer forum and StackExchange and can't figure out what the issue is with my deployment to production that keeps failing. When I run the test in the sandbox, it passes but when trying to deploy the code into production, it continues to fail on this line:

system.assertEquals('cmlam.com,sky.com', cynthiaMNYC.Email_Domains__c); 

I have put *** around the line below to help you see. I know that querying the object is necessary for System.AssertEquals so I've already done that. Any other ideas?

I am still new to coding so please forgive me for any rookie mistakes.

Test Class

@isTest
public class ContactTrigger_tests {

    public static testmethod void updateAccountEmailDomains_test(){
        
        Id pubAccountRTId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Publisher').getRecordTypeId();
        Id pubContactRTId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Publisher Contact').getRecordTypeId();
        
        //initialize data
        Account cynthiaMNYC = new Account(Name = 'Anthem', BillingCountry = 'US', BillingState = 'CA',Email_Domain_Exception_Account__c = false, RecordTypeId = pubAccountRTId, 
                                          Website = 'www.cynthiamnyc.com', Account_Tier__c = 'Non-Key', Customer_Segment__c = 'Elastic', Email_Domains__c = '');
        insert cynthiaMNYC;
        Account skyCompany = new Account(Name = 'Synergy', BillingCountry = 'US', BillingState = 'CA', Email_Domain_Exception_Account__c = false, RecordTypeId = pubAccountRTId,
                                        Website = 'www.skycompany.com', Account_Tier__c = 'Non-Key', Customer_Segment__c = 'Elastic', Email_Domains__c = '');
        insert skyCompany;        
        
        test.startTest();

        Contact c = new Contact(LastName = 'CM Lam', Email = 'cmlam@cmlam.com', AccountId = cynthiaMNYC.Id, MailingCountry = 'US', MailingState = 'CA', RecordTypeId = pubContactRTId);
        Contact c2 = new Contact(LastName = 'Stephanie Kry', Email = 'skry@sky.com', AccountId = cynthiaMNYC.Id, MailingCountry = 'US', MailingState = 'CA', RecordTypeId = pubContactRTId);        
        List<Contact> contactList = new List<Contact>();
        contactList.add(c);
        contactList.add(c2);
        insert contactList;
        
        c = [SELECT Email FROM Contact WHERE Id = :c.id];
        c2 = [SELECT Email FROM Contact WHERE Id = :c2.id];
        
        cynthiaMNYC = [SELECT Email_Domains__c FROM Account WHERE Id = :cynthiaMNYC.Id];
        ***system.assertEquals('cmlam.com,sky.com', cynthiaMNYC.Email_Domains__c);***
        
        
        //test delete case
        delete c2;
        cynthiaMNYC = [SELECT Email_Domains__c FROM Account WHERE Id = :cynthiaMNYC.Id];
        system.assertEquals('cmlam.com', cynthiaMNYC.Email_Domains__c);
        
        //test undelete case
        undelete c2;
        cynthiaMNYC = [SELECT Email_Domains__c FROM Account WHERE Id = :cynthiaMNYC.Id];
        system.assertEquals('cmlam.com,sky.com', cynthiaMNYC.Email_Domains__c);
        
        //test update email case
        c.Email = 'test12345@testdomain789.com';
        update c;
        cynthiaMNYC = [SELECT Email_Domains__c FROM Account WHERE Id = :cynthiaMNYC.Id];
        system.assertEquals('testdomain789.com,sky.com', cynthiaMNYC.Email_Domains__c);        
        
        //test update parent account case        
        c2.AccountId = skyCompany.Id;
        update c2;
        cynthiaMNYC = [SELECT Email_Domains__c FROM Account WHERE Id = :cynthiaMNYC.Id];
        system.assertEquals('testdomain789.com', cynthiaMNYC.Email_Domains__c);                
        skyCompany = [SELECT Email_Domains__c FROM Account WHERE Id = :skyCompany.Id];
        system.assertEquals('sky.com', skyCompany.Email_Domains__c);  
        
        test.stopTest();
    }        
    
}

Trigger

trigger ContactTriggers on Contact (after insert, after update, after delete, after undelete) {

    if(trigger.isAfter){
        if(trigger.isInsert){
            Contact_Trigger_Handlers.processContactEmailDomains(trigger.new);
        }
        else if(trigger.isUpdate){
            List<Contact> contactEmailsUpdateList = new List<Contact>();
            Set<Id> otherAccountIds = new Set<Id>(); //track accounts that have their contacts reassigned
            for(Contact c: trigger.new){
                if(c.Email != NULL && c.Email_Domain_Exception__c == false && c.Email_Domain__c != trigger.oldMap.get(c.Id).Email_Domain__c ){
                    contactEmailsUpdateList.add(c);
                }
                else if(c.Email != NULL && c.Email_Domain_Exception__c == false && c.AccountId != trigger.oldMap.get(c.Id).AccountId){
                    otherAccountIds.add(trigger.oldMap.get(c.Id).AccountId);
                    otherAccountIds.add(c.accountId);
                }                                       
            }
            if(!contactEmailsUpdateList.isEmpty()){
                Contact_Trigger_Handlers.processContactEmailDomains(contactEmailsUpdateList);                
            }
            if(!otherAccountIds.isEmpty()){
                Contact_Trigger_Handlers.updateAccountEmailDomains(otherAccountIds);
            }
        }
        else if(trigger.isDelete){
            Contact_Trigger_Handlers.processContactEmailDomains(trigger.old);
        }
        else if(trigger.isUndelete){
            Contact_Trigger_Handlers.processContactEmailDomains(trigger.new);        
        }                        
    }


}

Trigger Handler

    public class Contact_Trigger_Handlers {

    public static void processContactEmailDomains(List<Contact> contactList){
        Set<Id> accountIdSet = new Set<Id>();
        for(Contact c: contactList){
            accountIdSet.add(c.AccountId);
        }
        if(!accountIdSet.isEmpty()){
            updateAccountEmailDomains(accountIdSet);
        }
    }

    public static void updateAccountEmailDomains(Set<Id> accountIdSet){
        Map<String, Excluded_Email_Domains__c> excludedDomainsMap = Excluded_Email_Domains__c.getAll();
        List<Account> accountsToUpdate = new List<Account>();        
        for(Account a: [SELECT Id, Email_Domains__c,
                       (SELECT Email_Domain__c, Email_Domain_Exception__c, Email FROM Contacts WHERE Email != null) 
                        FROM Account WHERE Id IN :accountIdSet]){
            Set<string> emailDomainSet = new Set<string>();
            for(Contact c: a.Contacts){
                if(c.Email != NULL && c.Email_Domain_Exception__c == false && !excludedDomainsMap.containsKey(c.Email_Domain__c) ){
                    emailDomainSet.add(c.Email_Domain__c);
                }                    
            }
            if(!emailDomainSet.isEmpty()){
                string emailDomainJoined = stringJoinSet(emailDomainSet);
                if (a.Email_Domains__c != emailDomainJoined) {
                    a.Email_Domains__c = emailDomainJoined;
                    accountsToUpdate.add(a);
                }
            }                                           
        }
        if(!accountsToUpdate.isEmpty()){
            try{
                update accountsToUpdate;
            }
            catch(Exception e){
                system.debug('Exception occurred attempting to update Account Email Domains---'+ e.getMessage());
            }
        }        
    }
    
    private static String stringJoinSet(Set<String> values) {
        List<String> valueCopy = new List<String>(values);
        if(valueCopy.isEmpty())
        return null;
        String result = valueCopy[0];
        valueCopy.remove(0);
        while(!valueCopy.isEmpty()) {
            result += ',' + valueCopy[0];
            valueCopy.remove(0);
        }
        return result;
    }  
    
    
}

New Test Class Separated


@isTest
public class ContactTrigger_tests {
    
    @testSetup static void initialSetup(){
        
        Id pubAccountRTId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Publisher').getRecordTypeId();
        Id pubContactRTId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Publisher Contact').getRecordTypeId();
        
        //initialize data
        Account cynthiaMNYC = new Account(Name = 'Anthem', BillingCountry = 'US', BillingState = 'CA',Email_Domain_Exception_Account__c = false, RecordTypeId = pubAccountRTId, 
                                          Website = 'www.cynthiamnyc.com', Account_Tier__c = 'Non-Key', Customer_Segment__c = 'Elastic', Email_Domains__c = '');
        
        Account skyCompany = new Account(Name = 'Synergy', BillingCountry = 'US', BillingState = 'CA', Email_Domain_Exception_Account__c = false, RecordTypeId = pubAccountRTId,
                                         Website = 'www.skycompany.com', Account_Tier__c = 'Non-Key', Customer_Segment__c = 'Elastic', Email_Domains__c = '');
        
        List<Account> testAccts = new List<Account>();
        testAccts.add(cynthiaMNYC);
        testAccts.add(skyCompany);
        insert testAccts; 
        
        Contact c = new Contact(LastName = 'CM Lam', Email = 'cmlam@cmlam.com', AccountId = cynthiaMNYC.Id, MailingCountry = 'US', MailingState = 'CA', RecordTypeId = pubContactRTId);
        Contact c2 = new Contact(LastName = 'Stephanie Kry', Email = 'skry@sky.com', AccountId = cynthiaMNYC.Id, MailingCountry = 'US', MailingState = 'CA', RecordTypeId = pubContactRTId);        
        
        List<Contact> contactList = new List<Contact>();
        contactList.add(c);
        contactList.add(c2);
        insert contactList;
    }
    
    @isTest static void updateTestMethod() {
        // Get the first test account by using a SOQL query
        
        Contact ctc = [SELECT Id, Email FROM Contact WHERE Name = 'CM Lam']; 
        Account acct = [SELECT Id, Email_Domains__c FROM Account WHERE Name = 'Anthem'];
        
        //ctc.Email = 'cmlam@nativo.com';
        //update ctc;
       
        try { 
            // for inside
             for (Contact con : [SELECT Id, Email FROM Contact WHERE Name = 'CM Lam']) {
                Contact newCon = new Contact(
                Id = con.Id,
                Email = 'cmlam@nativo.com');
                update newCon ;
            }
        } catch (DmlException e) {
            System.debug(e.getMessage());
        }

        system.debug(ctc.Email);
        
        system.debug(acct.Email_Domains__c);
        
        system.assertEquals('nativo.com,sky.com', acct.Email_Domains__c);
    }
    
    @isTest static void deleteundeleteTestMethod1(){
        
        Contact ctc = [SELECT Id, Email FROM Contact WHERE Name = 'CM Lam'];
        Account acct = [SELECT Id, Email_Domains__c FROM Account WHERE Name = 'Anthem'];
        
        delete ctc; 
        system.assertEquals('sky.com', acct.Email_Domains__c);
        
        undelete ctc; 
        system.assertEquals('cmlam.com,sky.com', acct.Email_Domains__c);
    }
    
    @isTest static void initialtestMethod(){
        Account acct = [SELECT Id, Email_Domains__c FROM Account WHERE Name = 'Anthem'];
        
        system.assertEquals('cmlam.com,sky.com', acct.Email_Domains__c);
    }
    @isTest static void reparenttestMethod(){
        Account acct = [SELECT Id, Email_Domains__c FROM Account WHERE Name = 'Synergy'];
        Contact ctc = [SELECt Id, Email FROM Contact WHERE Name = 'CM Lam'];
        Contact ctc2 = [SELECT Id, Email FROM Contact WHERE Name = 'Stephanie Kry'];
        
        ctc.AccountId = acct.id; 
        update ctc; 
        
        system.assertEquals('cmlam.com,sky.com', acct.Email_Domains__c);
    }
}


11
  • 1
    what does the trigger look like which populates Email_Domains__c on Account? Did you also include this trigger on Contact in your deployment to production?
    – Mark Pond
    Oct 28 '20 at 22:53
  • Have Sandbox and Prod the same Validation Rules, Processes in PB, maybe Record Triggered Flows?, Also can you please provide your trigger code.
    – AjFmO
    Oct 28 '20 at 23:32
  • in the assertion error - what is the value of cynthiaMNYC.Email_Domains__c?
    – cropredy
    Oct 29 '20 at 3:46
  • @MarkPond I included the trigger in my deployment. I edited my original question to include the Trigger and Trigger Handler so you can see my Trigger.
    – pluginbaby
    Oct 29 '20 at 18:20
  • @AjFmO The trigger code has been updated.
    – pluginbaby
    Oct 29 '20 at 18:21
1

The changes are being made after insert/updates/deletes, but you are comparing with the Account values before those changes are committed, you would need to change the order, get the account after the changes, and compare against this updated Account.

To help you a bit more here's the updated code.

public class ContactTrigger_tests {

    @TestSetup
    static void initialSetup() {

        Id pubAccountRTId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Publisher').getRecordTypeId();
        Id pubContactRTId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Publisher Contact').getRecordTypeId();

        //initialize data
        Account cynthiaMNYC = new Account(
                Name = 'Anthem',
                BillingCountry = 'US',
                BillingState = 'CA',
                Email_Domain_Exception_Account__c = false,
                RecordTypeId = pubAccountRTId,
                Website = 'www.cynthiamnyc.com',
                Account_Tier__c = 'Non-Key',
                Customer_Segment__c = 'Elastic',
                Email_Domains__c = ''
        );

        Account skyCompany = new Account(
                Name = 'Synergy',
                BillingCountry = 'US',
                BillingState = 'CA',
                Email_Domain_Exception_Account__c = false,
                RecordTypeId = pubAccountRTId,
                Website = 'www.skycompany.com',
                Account_Tier__c = 'Non-Key',
                Customer_Segment__c = 'Elastic',
                Email_Domains__c = ''
        );

        List<Account> testAccts = new List<Account>();
        testAccts.add(cynthiaMNYC);
        testAccts.add(skyCompany);
        insert testAccts;

        Contact c = new Contact(
                LastName = 'CM Lam',
                Email = 'cmlam@cmlam.com',
                AccountId = cynthiaMNYC.Id,
                MailingCountry = 'US',
                MailingState = 'CA',
                RecordTypeId = pubContactRTId
        );
        Contact c2 = new Contact(
                LastName = 'Stephanie Kry',
                Email = 'skry@sky.com',
                AccountId = cynthiaMNYC.Id,
                MailingCountry = 'US',
                MailingState = 'CA',
                RecordTypeId = pubContactRTId
        );

        List<Contact> contactList = new List<Contact>();
        contactList.add(c);
        contactList.add(c2);
        insert contactList;
    }

    @IsTest
    static void updateTestMethod() {
        // Get the first test account by using a SOQL query

        Contact con = [SELECT Id, Email FROM Contact WHERE Name = 'CM Lam'];
        con.Email = 'cmlam@nativo.com';
        update con;

        Account acct = [SELECT Id, Email_Domains__c FROM Account WHERE Name = 'Anthem'];
        System.assertEquals('nativo.com,sky.com', acct.Email_Domains__c);
    }

    @IsTest
    static void deleteUndeleteTestMethod1() {
        Contact ctc = [SELECT Id, Email FROM Contact WHERE Name = 'CM Lam'];

        delete ctc;
        Account acct1 = [SELECT Id, Email_Domains__c FROM Account WHERE Name = 'Anthem'];
        System.assertEquals('sky.com', acct1.Email_Domains__c);

        undelete ctc;
        Account acct2 = [SELECT Id, Email_Domains__c FROM Account WHERE Name = 'Anthem'];
        System.assertEquals('cmlam.com,sky.com', acct2.Email_Domains__c);
    }


    @IsTest
    static void initialTestMethod() {
        Account acct = [SELECT Id, Email_Domains__c FROM Account WHERE Name = 'Anthem'];
        System.assertEquals('cmlam.com,sky.com', acct.Email_Domains__c);
    }

    @IsTest
    static void reparentTestMethod() {
        Account acctB4 = [SELECT Id, Email_Domains__c FROM Account WHERE Name = 'Synergy'];
        Contact ctc = [SELECT Id, Email FROM Contact WHERE Name = 'CM Lam'];
        Contact ctc2 = [SELECT Id, Email FROM Contact WHERE Name = 'Stephanie Kry'];

        List<Contact> contacts2Update = new List<Contact>();
        ctc.AccountId = acctB4.Id;
        ctc2.AccountId = acctB4.Id;
        contacts2Update.add(ctc);
        contacts2Update.add(ctc2);
        update contacts2Update;

        Account acctAfter = [SELECT Id, Email_Domains__c FROM Account WHERE Name = 'Synergy'];
        System.assertEquals('cmlam.com,sky.com', acctAfter.Email_Domains__c);
    }
}
-1

Based on the code I am seeing I am only seeing you set Email_Domains__c to ''. You create cynthiaMNYC account beneath comment //initialize data and then use that same variable id to get the data you just inserted before the assert so I am not seeing how this even would work on a sandbox.¯_(ツ)_/¯

As a side note I suggest you use @testsetup as described at below link to separate your concerns of preparation of data to your actual tests FYI.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm

0

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.