Requirement: When an email is synced to Salesforce from Outlook, whatever Lead or Contact is associated with it needs to have the custom field Email_Opt_In__c checked. That part is working just fine. The only issue is testing the class I already created. This is the trigger that I use with the @Future Method.
trigger TaskTrigger on Task (after insert,after update) {
List<Lead> LList = new List<Lead>();
List<Contact> LContact = new List<Contact>();
Set<Id> LeadIds = new Set<Id>();
Set<Id> ContactIds = new Set<Id>();
Set<Id> TaskIds = new Set<Id>();
List<Id> LeadId= new List<Id>();
List<Id> ContId= new List<Id>();
List<Id> TaskId= new List<Id>();
for (Task t:Trigger.new)
{
if(t.whoId !=Null)
{
string whoid= t.whoId;
if (whoId.startsWith('00Q'))
{
LeadIds.add(t.whoId);
TaskIds.add(t.Id);
System.debug('Task Id in Mark:' + TaskIds);
}
else
{
ContactIds.add(t.whoId);
TaskIds.add(t.Id);
}
}
}
Map<Id,Lead> Leadmap = new Map<Id,Lead>([SELECT Id,Email_Opt_In__c,Email FROM Lead WHERE Id In :LeadIds]);
Map<Id,Contact> Contactmap = new Map<Id,Contact>([SELECT Id,Email_Opt_In__c,LeadSource,Email FROM Contact WHERE Id In :ContactIds]);
LList=Leadmap.values();
LContact=Contactmap.values();
LeadId.addAll(LeadIds);
ContId.addAll(ContactIds);
TaskId.addAll(TaskIds);
Util.AnalyzeTask(LeadId,ContId,TaskId);
For (Task t: Trigger.new)
if ( t.whoId!=Null)
{
string whoid = t.whoId;
boolean IsLead = whoid.startsWith('00Q');
boolean IsContact = whoid.startsWith('003');
if (IsLead && (t.Type=='Phone Call' || t.Type=='Meeting'))
{
for (integer i=0;i<LList.size();i++)
{
LList[i].Email_Opt_In__c = True;
}
} else
if (IsContact && (t.Type=='Phone Call' || t.Type=='Meeting'))
{
for (integer i=0;i<LContact.size();i++)
{
LContact[i].Email_Opt_In__c = True;
}
}else
if (IsContact)
{
for (integer i=0;i<LContact.size();i++)
{
if (LContact[i].LeadSource=='Broker')
{
LContact[i].Email_Opt_In__c = True;
}
}
}
}Update LContact;Update LList;
}
And this is the @Future method:
public class Util {
@Future
public static void AnalyzeTask(List<Id> LeadId, List<Id> ContId,List<Id> TaskId){
List<Lead> LList = new List<Lead>();
List<Contact> LContact = new List<Contact>();
Map <Id,Lead> LeadMap = new Map<Id,Lead>([SELECT Id,Email_Opt_In__c,Email FROM Lead WHERE Id In :LeadId]);
Map<Id,Contact> ContMap = new Map<Id,Contact>([SELECT Id,Email_Opt_In__c,LeadSource,Email FROM Contact WHERE Id In :ContId]);
Map<Id,Task> TaskMap=new Map<Id,Task>([SELECT Id,Whoid,Type,Subject,Description from Task WHERE Id In:TaskId]);
System.Debug('Task Id Size' + TaskId.size());
For (Task t:TaskMap.values())
{
if ( t.whoId!=Null)
{
string whoid = t.whoId;
boolean IsLead = whoid.startsWith('00Q');
boolean IsContact = whoid.startsWith('003');
// Is my task an Email ?
if (t.Type=='Email')
{
string subject= t.Subject;
if (subject.startsWith('Email:'))
if (IsLead)
{
LList= Leadmap.values();
//An Email that Sync from Outlook into Salesforce.
for (integer i=0;i<LList.size();i++)
{
string description = t.Description;
List<string> Ldesc= new List<string>();
Ldesc = description.split('\r\n',2);
System.debug('Description:' + Ldesc[0]);
System.debug('Leads Email:' + LList[i].Email);
if (Ldesc[0].contains(LList[i].Email)&& Ldesc[0].startsWith('From:'))
{
LList[i].Email_Opt_In__c = True;
}
}
}
else if(IsContact)
{ System.Debug('It was a Contact:' + IsContact);
LContact=ContMap.values();
for (integer i=0;i<LContact.size();i++)
{
string description = t.Description;
List<string> Ldesc= new List<string>();
Ldesc = description.split('\r\n',2);
System.debug('Description:' + Ldesc[0]);
if (Ldesc[0].contains(LContact[i].Email)&& Ldesc[0].startsWith('From:'))
{
LContact[i].Email_Opt_In__c = True;
}
} Update LContact;
}
}
}
}
Update LList;
}
}
And this is my test class:
@isTest
public class TaskTriggerTestLead {
public static testMethod void Tasktest(){
Lead l=sObjectCreator.createLead();
l.Email='[email protected]';
insert l;
Task[] TtoCreate = new Task[]{};
for (integer i=0;i<200;i++){
Task t=sObjectCreator.createTask();
t.type='Email';
t.Subject='Email:';
t.Description='From:[email protected]';
t.WhoId=l.id;
TtoCreate.add(t);
}
Test.startTest();
insert TtoCreate;
Test.stopTest();
update TtoCreate;
System.assertEquals(True,l.Email_Opt_In__c);
}
}
Now, if I put the Update LList in my @future method any higher, it gets inside the Loop and I run into DML statements limits. If I put the Update LList outside of the for loop, my records don't get updated.
Any help would be awesome. Thanks!