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
Glenn Soden 9Glenn 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;
Best Answer chosen by Glenn Soden 9
Balayesu ChilakalapudiBalayesu Chilakalapudi
Yes, your test class need lead. it does not access your organizational data unless you specify @isTest(SeeAllData=true).

Try like this,
@IsTest
public class DoNotCallTest  {
    static testmethod void validateDoNotCall () { 
    Lead lObj=new Lead();
    lObj.name='test lead';
    insert lObj;  
    
   Task t = new Task(); 
    t.Subject = 'Do Not Call'; 
    t.Priority = 'Normal';
    t.whoId= lObj.Id;
    insert t;
   }
}

Let us know if it helps

All Answers

Balayesu ChilakalapudiBalayesu Chilakalapudi
Yes, your test class need lead. it does not access your organizational data unless you specify @isTest(SeeAllData=true).

Try like this,
@IsTest
public class DoNotCallTest  {
    static testmethod void validateDoNotCall () { 
    Lead lObj=new Lead();
    lObj.name='test lead';
    insert lObj;  
    
   Task t = new Task(); 
    t.Subject = 'Do Not Call'; 
    t.Priority = 'Normal';
    t.whoId= lObj.Id;
    insert t;
   }
}

Let us know if it helps
This was selected as the best answer
Glenn Soden 9Glenn Soden 9
That makes more sense,  but now won't compile as "Field is not Writable  Lead.Name"  line 5 column5
Balayesu ChilakalapudiBalayesu Chilakalapudi
change line 05, like this,
​lObj.DoNotCall =false;

 
Glenn Soden 9Glenn Soden 9
Thank you Bala.  A few more line "required fields" had to be added but got it working and good coverage.  Appreciate the help and education as this makes sense now.  Especially referencing back to the new Lead with the new task insert.

@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;
   }
}