-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
1Questions
-
2Replies
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;
}*/
}
- Zach Lucas 2
- December 29, 2021
- Like
- 0
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;
}*/
}
- Zach Lucas 2
- December 29, 2021
- Like
- 0