You need to sign in to do that
Don't have an account?
Glenn Soden 9
Stuck on test class for trigger
Can someone help me with this? I have a trigger that a member made for me to checc the lead box Do Not Call "true" if a task subject has words "Do Not Call" Having problem writing the test class for the trigger for the check box in particular. Does test class need to make a new lead? I get Boolean errors etc. I'm a bit new to the coding so could use some help. Here is the trigger I need test class for. And my beginnings of the test class.
trigger trgDoNotCall on Task (after update, after insert) {
List<Id> lstLeadIds = new List<Id>();
for (Task task : trigger.new){
system.debug('task.WhoId : ' + task.WhoId);
system.debug('task.Who.Type : ' + task.Who.Type);
if(task.WhoId != null){
String sSubject = task.subject;
System.debug('sSubject ' + sSubject);
System.debug('sSubject.indexOf ' + sSubject.indexOf('Do Not Call'));
if((sSubject.indexOf('Do Not Call') > -1) || (sSubject.indexOf('DoNotCall') > -1)){
lstLeadIds .add(task.WhoId);
system.debug('task.WhoId : ' + task.WhoId);
}
}
}
if(lstLeadIds.size() > 0){
List<Lead> lstLeads = [SELECT Id, DoNotCall FROM Lead WHERE Id =: lstLeadIds];
for( Lead lead : lstLeads){
lead.DoNotCall = true;
//lead.Description += 'Do Not Call';
System.debug('lead : ' + lead);
}
update lstLeads;
}
And the test class start
@IsTest
public class DoNotCallTest {
static testmethod void validateDoNotCall () {
Task t = new Task();
t.Subject = 'Do Not Call';
t.Priority = 'Normal';
insert t;
trigger trgDoNotCall on Task (after update, after insert) {
List<Id> lstLeadIds = new List<Id>();
for (Task task : trigger.new){
system.debug('task.WhoId : ' + task.WhoId);
system.debug('task.Who.Type : ' + task.Who.Type);
if(task.WhoId != null){
String sSubject = task.subject;
System.debug('sSubject ' + sSubject);
System.debug('sSubject.indexOf ' + sSubject.indexOf('Do Not Call'));
if((sSubject.indexOf('Do Not Call') > -1) || (sSubject.indexOf('DoNotCall') > -1)){
lstLeadIds .add(task.WhoId);
system.debug('task.WhoId : ' + task.WhoId);
}
}
}
if(lstLeadIds.size() > 0){
List<Lead> lstLeads = [SELECT Id, DoNotCall FROM Lead WHERE Id =: lstLeadIds];
for( Lead lead : lstLeads){
lead.DoNotCall = true;
//lead.Description += 'Do Not Call';
System.debug('lead : ' + lead);
}
update lstLeads;
}
And the test class start
@IsTest
public class DoNotCallTest {
static testmethod void validateDoNotCall () {
Task t = new Task();
t.Subject = 'Do Not Call';
t.Priority = 'Normal';
insert t;
Try like this,
Let us know if it helps
All Answers
Try like this,
Let us know if it helps
@IsTest
public class DoNotCallTest {
static testmethod void validateDoNotCall () {
Lead lObj=new Lead();
lObj.FirstName='Trigger1';
lObj.LastName='Test1';
lObj.Company='Trigger Test1';
lObj.DoNotCall=false;
insert lObj;
Task t = new Task();
t.Subject = 'Do Not Call';
t.Priority = 'Normal';
t.whoId= lObj.Id;
insert t;
}
}