Skip to content

Instantly share code, notes, and snippets.

@mkorman
Last active March 18, 2016 12:05
Show Gist options
  • Select an option

  • Save mkorman/57bebde3f009d2d55b4c to your computer and use it in GitHub Desktop.

Select an option

Save mkorman/57bebde3f009d2d55b4c to your computer and use it in GitHub Desktop.
@isTest
public class TestCreateFollowupTaskTrigger
{
@isTest
public static void TestInitialCallTaskIsCreated ()
{
Lead l = new Lead (LastName = 'Test', Company = 'Acme');
insert l;
List<Task> createdTasks = [SELECT Subject, WhoId, ActivityDate FROM Task];
System.AssertEquals (1, createdTasks.size(), 'Expected 1 task to be created');
Task initialCallTask= createdTasks [0];
System.AssertEquals (Date.Today().AddDays(2), initialCallTask.ActivityDate, 'Expected initial call task to expire in 2 days');
System.AssertEquals ('Intial call', initialCallTask.Subject, 'Unexpected subject on initial call task');
System.AssertEquals (l.id, initialCallTask.WhoId, 'Initial call task should be associated with the newly created lead');
}
@isTest
public static void TestFollowupTaskIsCreated ()
{
Lead l = new Lead (LastName = 'Test', Company = 'Acme');
insert l;
System.AssertEquals (1, [SELECT Id FROM Task].size(), 'Expected 1 task to be created when creating the lead');
l.Status = 'Working - Contacted';
update l;
List<Task> createdTasks = [SELECT Subject, WhoId, ActivityDate FROM Task ORDER BY ActivityDate];
// We only want to look at the followup task
System.AssertEquals (2, createdTasks.size(), 'Expected 2 tasks to be created');
Task followupTask = createdTasks [1];
System.AssertEquals (Date.Today().AddDays(7), followupTask.ActivityDate, 'Expected followup task to expire in 7 days');
System.AssertEquals ('Send email', followupTask.Subject, 'Unexpected subject on followup task');
System.AssertEquals (l.id, followupTask.WhoId, 'Followup Task should be associated with the newly created lead');
}
@isTest
public static void TestNoTaskIsCreatedOnLeadConversion ()
{
Lead l = new Lead (LastName = 'Test', Company = 'Acme');
insert l;
System.AssertEquals (1, [SELECT Id FROM Task].size(), 'Expected 1 task to be created when creating lead');
convertLead (l);
System.AssertEquals (1, [SELECT Id FROM Task].size(), 'Expected no extra tasks to be created after converting lead');
}
@isTest
public static void TestNoTaskIsCreatedOnUpdatingStatus ()
{
Lead l = new Lead (LastName = 'Test', Company = 'Acme');
insert l;
System.AssertEquals (1, [SELECT Id FROM Task].size(), 'Expected 1 task to be created when creating lead');
l.Status = 'Closed - Not Converted';
update l;
System.AssertEquals (1, [SELECT Id FROM Task].size(), 'Expected no task to be created when closing lead');
}
@isTest
public static void TestBulkCreationCreatesTasks ()
{
Integer leadCount = 151;
List<Lead> allLeads = new List<Lead>();
for (Integer i = 0; i < leadCount; i++)
{
allLeads.Add (new Lead(LastName = 'Test', Company = 'Acme'));
}
insert allLeads;
System.AssertEquals (leadCount, [SELECT Id FROM Task].size(), 'Expected one task to be created for each new lead');
}
private static void convertLead (Lead leadToConvert)
{
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(leadToConvert.Id);
lc.setDoNotCreateOpportunity(false);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.convertLead(lc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment