1

Trigger and test class works in Sandbox with 100% code coverage (trigger code listed below)

I have checked the following: 1.My code coverage in Production is 98% and Sandbox is 94% according to Salesforce's "Developer Console" 2. I have turned off all Validation and Workflow rule in production to make sure it's not an issue with these rules before deployment (same result of 38% overall coverage)

I have reached with the conclusion that my trigger below is somehow breaking Apex Test Classes that are in place when I try to deploy, because along with the "38%" error message, it tells me that there are 5 other triggers (that are successfully deployed previously in production and is in production with 100% coverage) with 0% test coverage during deployment.

Please see my trigger below and please help, thanks! (P.S. This trigger had worked before but I added MAP near the bottom of this new trigger hoping that this would resolve any issues when our users use CRM tools like Dataloader to mass update/insert old/new Contact records).

trigger primaryLiaisonCount2 on Contact(after update) {
    if (checkRecursive.runOnce()) {
        List < Account > accList = new List < account > ();
        List < Account > accListToUpdate = new List < account > ();
        Set < Account > accSet = new Set < Account > ();
        set < String > allAccIds = new Set < String > ();

        for (Contact c1: trigger.new) {
            if (c1.Contact_ID_18_Characters__c != null) {
                allAccIds.add(c1.AccountID);
            }
        }


        decimal amount1 = [SELECT count() FROM Contact WHERE AccountID IN: allAccIds AND Primary_Liaison__c != null AND Marketing_Status__c != 'Gone From Company'
            LIMIT 25
        ];
        LIST < Account > acc1 = [SELECT ID FROM Account WHERE ID IN: allAccIds AND recordtypeID = '012000000000j8S'
            LIMIT 25
        ];

        MAP < String, Account > firstMap = new MAP < String, Account > ();
        for (Account acc2: acc1) {
            firstMap.put(acc2.id, acc2);
        }

        for (Contact c1: trigger.new) {
            Account acc3 = firstMap.get(c1.accountId);
            acc3.Primary_Liaisons__c = amount1;
            accList.add(acc3);
        }

        accSet.addall(accList);
        accListToUpdate.addall(accSet);
        Update accListToUpdate;
    }
}
2

Do any of your test cases use the isTest(SeeAllData=true) or are set to API 23 or earlier? If either of these are true your tests are influenced by existing data in an org and so are more fragile and you may get different results in different orgs.

Your trigger looks like it could easily cause a NullPointerException with this line acc3.Primary_Liaisons__c = amount1; as the Account IDs that are queried are only for contacts that have Contact_ID_18_Characters__c true and you also have the LIMIT 25 which may arbitrarily mean Accounts are missing. Adding a null guard would avoid that problem:

for (Contact c1: trigger.new) {
    Account acc3 = firstMap.get(c1.accountId);
    if (acc3 != null) {
        acc3.Primary_Liaisons__c = amount1;
        accList.add(acc3);
    }
}

But while this might up the code coverage, your trigger looks logically incorrect for the bulk case. For example, if you have two Contacts related to two different Accounts, you are setting both Accounts to the same decimal amount1 = ... value which is the count of the matching Contacts on both Accounts rather than having counts of the matching Contacts for each separate Account.

You also need to remove the LIMIT 25 to get correct answers.

4
  1. Turning off workflows before deployment most likely will cause some test methods that previously passed to fail (If they were written correctly). It is bad practice to turn off workflows during deployment

  2. Prior to deployment, run all test in production to verify that all test still pass before you deploy

  3. In sandbox, run all test and ensure they pass with your new code in place.

If 1-3 all pass and you still are having issues, you will have to evaluate the difference between production and sandbox to see what may be causing the descrepiencies.

At this point, your code is not much help until you can answer 1-3 as the issue is most likely not your code at this point.

  • Hey Eric, Thanks for your response. Yeah 1-3 all passed before that's why I tried to turn off all workflow/validation. Glad that I shouldn't have done that. Our sandbox has very little records compared to production... I think Keith (below) may have solved the mystery though! Thanks again! – sfdcIndicat0r Nov 17 '14 at 1:38

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.