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
Ishan Singh 4Ishan Singh 4 

Can anyone suggest what is wrong in my test class

batchClass

global class BirthdayCard implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc)
     {
        return Database.getQueryLocator([SELECT dateOfBirth__c,Id,Possible_Followup_Date__c FROM Lead WHERE dateOfBirth__c =  NEXT_N_DAYS:7 AND Possible_Followup_Date__c != null]);
    }
    global void execute(Database.BatchableContext bc, List<Task> scope)
    {
        for(Task l : scope)
        {
            Task B = new Task();
            B.Subject= 'Send Birthday Card';
            B.ActivityDate = date.today();
            B.OwnerId = l.OwnerId;
            B.WhoId=l.Id;
            
                   
       }
        insert scope;
    }
    global void finish(Database.BatchableContext bc){}
    
}

testClass:

@isTest
public class BirthdayCardTest{
    @isTest
    public static void BirthdayCard(){
        List<Lead> leadList = new List<Lead>();
        {
            Lead ld = new Lead();
            ld.lastname = 'test';
            ld.dateOfBirth__c = date.today().addDays(7);
            ld.Status = 'Unqualified';
            ld.State__c='Test';
            ld.Policy_DB_Amount__c=100000;
            ld.Company='Mirketa';
            ld.Possible_Followup_Date__c=date.today()
            
           leadList.add(ld);  
        }
        insert leadList;
        Test.startTest();
        Database.executeBatch(new BirthdayCard());
        Test.stopTest();
    }
}

 
Dhanik L SahniDhanik L Sahni
Hello Ishan, 
Please tell us your requirement and is your batch class is giving propr output?

You are getting data from Lead object and you are referring Task object in execute parameter. Please check this.

Thank You,
Dhanik
Ishan Singh 4Ishan Singh 4
I want to insert 'Sent birthday Card'  task for lead owner before one week of bithday.
Dhanik L SahniDhanik L Sahni
Hello Ishan,

You can use below approachs for this requirement
1. https://success.salesforce.com/answers?id=9063A000000pYCnQAM for your requirement.
2. https://help.salesforce.com/articleView?id=000328974&language=en_US&type=1&mode=1

Thank You,
Dhanik Sahni
SalesforceCodex.com