function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zach Lucas 2Zach Lucas 2 

Test Coverage of Apex Trigger good in test but fails in production

I have a frustrating bug where another system is inserting leads as task if the contact already exsists in out system. I wrote up a quick tigger to intercept these tasks and I use the info to create a new lead instead. this works 80% of the time but there are a few issues I am still trying to work out. Now I am attempting to push a change to live to fix a bunch of errors but my test cases now fail where as before they worked just fine. I have ensured that the hard coded ID exsist and are correct in both test and live. Below is my code.

trigger ChangeNewTaskToNewLead on Task (before  insert) {
    Set<id>parentIds = new Set<id>();
    for(Task atask:trigger.new){
        if (atask.WhoId != null) {
            parentIds.add(atask.WhoId);
        }
    }
    for(Task atask:trigger.new){
        if(atask.Subject.contains('HOT LEAD') || atask.Subject.contains('Scored Lead')){
            try {
                if(atask.WhoId == null){
                    throw new IllegalArgumentException('Null task ID');
                }
                Contact[] possibleParents = [select FirstName, LastName, Phone, Fax, Email, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry, AccountId, HasOptedOutOfEmail ,Web_Inquiry__c,    LeadSource, DoNotCall,(select id,CreatedDate,CampaignId from CampaignMembers)from Contact where id in :parentIds];
                for(Contact parent:possibleParents){
                    CreateNewLeadFromTask.makeNewLead(parent, atask);
                }
            }catch(Exception e){
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {*************', '*****************'};
                mail.setToAddresses(toAddresses);
                mail.setSenderDisplayName('Error Support');
                mail.setSubject('Error on task to lead conversion');
                String mailBody = ''+ e;
                mail.setPlainTextBody(mailBody);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
                    mail
                });
            }
        }
    }
}

@isTest
public class TestTaskToLeadConversion {
    @isTest static boolean testTaskConversionScoredLead() {
        try{
            Task test = new Task();
            test.Subject = 'Scored Lead';
            test.WhoId = '005C0000003WgMMIA0';
            insert test;
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    @isTest static boolean testTaskConversionNull() {
        try{
            Task test = new Task();
            test.Subject = 'HOT LEAD';
            test.WhoId = null;
            insert test;
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    @isTest static boolean testTaskConversion() {
        try{
            Task test = new Task();
            test.Subject = 'HOT LEAD';
            test.WhoId = '005C0000003WgMMIA0';
            insert test;
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    @isTest static boolean testLeadCreationScoredLead() {
        try{
            Contact testContact = new Contact();
            testContact.FirstName = 'testFirstname';
            testContact.LastName = 'testLastName';
            testContact.Phone = '1234567890';
            testContact.Email = 'Email';
            testContact.MailingStreet = 'MailingStreet';
            testContact.MailingCity = 'MailingCity';
            testContact.MailingCountry = 'MailingCountry';
            testContact.MailingState = 'MailingState';
            testContact.MailingPostalCode = 'MailingPostalCode';
            testContact.AccountId = '0011A00001PO84cQAD';
            //database.insert(testContact);
            Task test = new Task();
            test.Subject = 'Scored Lead';
            test.WhoId = testContact.Id;
            CreateNewLeadFromTask.makeNewLead(testContact, test, true);
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    @isTest static boolean testLeadCreation() {
        try{
            Contact testContact = new Contact();
            testContact.FirstName = 'testFirstname';
            testContact.LastName = 'testLastName';
            testContact.Phone = '1234567890';
            testContact.Email = 'Email';
            testContact.MailingStreet = 'MailingStreet';
            testContact.MailingCity = 'MailingCity';
            testContact.MailingCountry = 'MailingCountry';
            testContact.MailingState = 'MailingState';
            testContact.MailingPostalCode = 'MailingPostalCode';
            testContact.AccountId = '0011A00001PO84cQAD';
            //database.insert(testContact);
            Task test = new Task();
            test.Subject = 'HOT LEAD';
            test.WhoId = testContact.Id;
            CreateNewLeadFromTask.makeNewLead(testContact, test, true);
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    @isTest static boolean testLeadCreationAndRepurose1() {
        try{
            Contact testContact = new Contact();
            testContact.FirstName = 'testFirstname';
            testContact.LastName = 'testLastName';
            testContact.Phone = '1234567890';
            testContact.Email = 'Email';
            testContact.MailingStreet = 'MailingStreet';
            testContact.MailingCity = 'MailingCity';
            testContact.MailingCountry = 'MailingCountry';
            testContact.MailingState = 'MailingState';
            testContact.MailingPostalCode = 'MailingPostalCode';
            testContact.AccountId = '0011A00001PO84cQAD';
            Lead newLead = new Lead();
            newLead.City = testContact.MailingCity;
            newLead.FirstName = testContact.FirstName;
            newLead.LastName = testContact.LastName;
            newLead.Status = 'Qualified';
            newLead.ConvertedContactId = testContact.Id;
            newLead.ConvertedAccountId = testContact.AccountId;
            newLead.IsConverted = True;
            newLead.ConvertedDate = system.today();
            //database.insert(newLead);
            Task test = new Task();
            test.Subject = 'HOT LEAD';
            test.WhoId = testContact.Id;
            CreateNewLeadFromTask.repourposeOldLead(newLead, test);
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    @isTest static boolean testLeadCreationAndRepurose2() {
        try{
            Contact testContact = new Contact();
            testContact.FirstName = 'testFirstname';
            testContact.LastName = 'testLastName';
            testContact.Phone = '1234567890';
            testContact.Email = 'Email';
            testContact.MailingStreet = 'MailingStreet';
            testContact.MailingCity = 'MailingCity';
            testContact.MailingCountry = 'MailingCountry';
            testContact.MailingState = 'MailingState';
            testContact.MailingPostalCode = 'MailingPostalCode';
            testContact.AccountId = '0011A00001PO84cQAD';
            Lead newLead = new Lead();
            newLead.City = testContact.MailingCity;
            newLead.FirstName = testContact.FirstName;
            newLead.LastName = testContact.LastName;
            newLead.Status = 'Qualified';
            newLead.ConvertedContactId = testContact.Id;
            newLead.ConvertedAccountId = testContact.AccountId;
            newLead.IsConverted = True;
            newLead.ConvertedDate = system.today();
            Task test2 = new Task();
            test2.Subject = 'Scored Lead';
            test2.WhoId = testContact.Id;
            CreateNewLeadFromTask.repourposeOldLead(newLead, test2);
           
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    @isTest static boolean testLeadCreationAndRepurose3() {
        try{
            Contact testContact = new Contact();
            testContact.FirstName = 'testFirstname';
            testContact.LastName = 'testLastName';
            testContact.Phone = '1234567890';
            testContact.Email = 'Email';
            testContact.MailingStreet = 'MailingStreet';
            testContact.MailingCity = 'MailingCity';
            testContact.MailingCountry = 'MailingCountry';
            testContact.MailingState = 'MailingState';
            testContact.MailingPostalCode = 'MailingPostalCode';
            testContact.AccountId = '0011A00001PO84cQAD';
            Lead newLead = new Lead();
            newLead.City = testContact.MailingCity;
            newLead.FirstName = testContact.FirstName;
            newLead.LastName = testContact.LastName;
            newLead.Status = 'Qualified';
            newLead.ConvertedContactId = testContact.Id;
            newLead.ConvertedAccountId = testContact.AccountId;
            newLead.IsConverted = True;
            newLead.ConvertedDate = system.today();
            newLead.Status = 'Attempted';
            Task test3 = new Task();
            test3.Subject = 'HOT LEAD';
            test3.WhoId = testContact.Id;
            CreateNewLeadFromTask.repourposeOldLead(newLead, test3);
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    @isTest static boolean testLeadCreationAndRepurose4() {
        try{
            Contact testContact = new Contact();
            testContact.FirstName = 'testFirstname';
            testContact.LastName = 'testLastName';
            testContact.Phone = '1234567890';
            testContact.Email = 'Email';
            testContact.MailingStreet = 'MailingStreet';
            testContact.MailingCity = 'MailingCity';
            testContact.MailingCountry = 'MailingCountry';
            testContact.MailingState = 'MailingState';
            testContact.MailingPostalCode = 'MailingPostalCode';
            testContact.AccountId = '0011A00001PO84cQAD';
            Lead newLead = new Lead();
            newLead.City = testContact.MailingCity;
            newLead.FirstName = testContact.FirstName;
            newLead.LastName = testContact.LastName;
            newLead.Status = 'Qualified';
            newLead.ConvertedContactId = testContact.Id;
            newLead.ConvertedAccountId = testContact.AccountId;
            newLead.IsConverted = True;
            newLead.ConvertedDate = system.today();
            //database.insert(newLead);
            Task test4 = new Task();
            test4.Subject = 'Scored Lead';
            test4.WhoId = testContact.Id;
            CreateNewLeadFromTask.repourposeOldLead(newLead, test4);
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    @isTest static boolean testCheckSaveResultsTrue() {
        try{
            Contact testContact = new Contact();
            testContact.FirstName = 'testFirstname';
            testContact.LastName = 'testLastName';
            testContact.Phone = '1234567890';
            testContact.Email = 'Email';
            testContact.MailingStreet = 'MailingStreet';
            testContact.MailingCity = 'MailingCity';
            testContact.MailingCountry = 'MailingCountry';
            testContact.MailingState = 'MailingState';
            testContact.MailingPostalCode = 'MailingPostalCode';
            testContact.AccountId = '0011A00001PO84cQAD';
            Task test = new Task();
            test.Subject = 'HOT LEAD';
            test.WhoId = testContact.Id;
            Lead newLead = new Lead();
            Database.Error errortest1;
            Database.Error errortest2;
            List<Database.Error> errorlist = new List<Database.Error>();
            errorlist.add(errortest1);
            errorlist.add(errortest2);
            CreateNewLeadFromTask.checkSaveResults(testContact, test, true, newLead,errorlist);
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    @isTest static boolean testCheckSaveResultsFalse() {
        try{
            Contact testContact = new Contact();
            testContact.FirstName = 'testFirstname';
            testContact.LastName = 'testLastName';
            testContact.Phone = '1234567890';
            testContact.Email = 'Email';
            testContact.MailingStreet = 'MailingStreet';
            testContact.MailingCity = 'MailingCity';
            testContact.MailingCountry = 'MailingCountry';
            testContact.MailingState = 'MailingState';
            testContact.MailingPostalCode = 'MailingPostalCode';
            testContact.AccountId = '0011A00001PO84cQAD';
            Task test = new Task();
            test.Subject = 'HOT LEAD';
            test.WhoId = testContact.Id;
            Lead newLead = new Lead();
            Database.Error errortest1;
            Database.Error errortest2;
            List<Database.Error> errorlist = new List<Database.Error>();
            errorlist.add(errortest1);
            errorlist.add(errortest2);
            CreateNewLeadFromTask.checkSaveResults(testContact, test, false, newLead,errorlist);
            return true;
        }catch (Exception e) {
            return false;
        }
    }
    /*@isTest static boolean testStopEmails() {
        CreateNewLeadFromTask.stopEmails();
        return true;
    }*/
}

Best Answer chosen by Zach Lucas 2
Zach Lucas 2Zach Lucas 2
I have found the issue. There was a trigger for tasks that would assign a task to an adminstrator if the something failed as the task never got assigned to anyone. There was a mistake in that trigger that cuased this issue. 

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Zach,

Greetings!

Have you captured the debug logs while running the test class to see,which error or code is causing the issue here.If not,I would suggest you to try to debug the issue by capturing the debug logs.

Also,if this is occurring while deploying then I would suggest you to go with the RunSpecifiedTests since you have 80% code coverage.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Zach Lucas 2Zach Lucas 2
I have already done RunSpecifiedTests and still get a 0% code coverage in live. I will go over the debug and see if anything shows up in test
Sarah JamesSarah James

thanks for provding valued answer. 

9 Amazing Different Breast Shapes In Women (https://womenhealthome.com/breast-shapes/)
Pregnancy Symptoms Week 5 – with Nausea (https://womenhealthome.com/early-pregnancy-symptoms-week-5/)
Amazing Benefits Of Prenatal Yoga Online Second Trimester For Moms-to-Be (https://womenhealthome.com/prenatal-yoga-online-second-trimester/)

Zach Lucas 2Zach Lucas 2
I have found the issue. There was a trigger for tasks that would assign a task to an adminstrator if the something failed as the task never got assigned to anyone. There was a mistake in that trigger that cuased this issue. 
This was selected as the best answer