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
Nishant Shah 9Nishant Shah 9 

Not able to find silly mistake for batch apex

How to clone task related to the cases. The business is on the anniversary of an account, clone the case which is working. What is the thing needed to clone the task . Below is my code but not working.
 
public class Account_CloneCasesOnAnniversary implements   Database.Batchable<SObject>
{
    String errors = '';
    integer todayday = date.today().day();
    Integer currentmonth = date.today().month();

 public Database.QueryLocator start(Database.BatchableContext bc)
{
   return Database.getQueryLocator('SELECT Anniversary__c,(Select Id from cases order by CreatedDate),Id from Account WHERE DAY_IN_MONTH(Anniversary__c) =:todayday AND CALENDAR_MONTH(Anniversary__c ) =: currentmonth') ;
}

 public void execute(Database.BatchableContext bc, List<Account> scope)
 {
    if(scope.isEmpty()) return;
    List<Case> caseToCreateList = new List<Case>();
    List<Task> taskToCreateList = new List<Task>();

    system.debug('Scope is'+scope);

     for(account objS : scope){

         for(task objT : scope) {

             system.debug('Account with case'+objS);

        if(objS.cases.size() > 0 && objT.Task.size() >0 )
        {

            system.debug('aAccount.cases.size()'+objS.cases.size());
            list<Case> lstCase = new list<Case>();
            for(Case objcase : objS.cases){
                Case cloneCase = new Case();
                cloneCase  = objcase.clone();

                // collecting the Cases to create
                caseToCreateList.add(cloneCase);
            }

            for(Task objTask : objT.Task) {

                Task cloneTask = new cloneTask();
                cloneTask=objTask.clone();

                taskToCreateList.add(cloneTask);

            }

        }
         system.debug('caseToCreateList'+caseToCreateList);
     }
   }

       if(!caseToCreateList.isEmpty()) 
       { 

         Database.SaveResult[] srList = Database.insert(caseToCreateList, false);
         Integer index = 0;
         system.debug('---srList  first'+ srList );

         for(Database.SaveResult result : srList )
         {
            if(!result.isSuccess())
            {
                String errMsg = result.getErrors()[0].getMessage();

            } // End Inner If
            index++;
          } // End for
         } // End Outer If
       } // End try


public void finish(Database.BatchableContext context)
{

    }
   }
  }

 
SandhyaSandhya (Salesforce Developers) 
Hi,

As you are following this question on stackExchange, I will post the link here so that it will make available for others as a proper solution.

http://salesforce.stackexchange.com/questions/145240/how-to-clone-the-tasks-related-to-cases
 
Please mark it as solved once your issue is resolved so that it will be removed from the unanswered queue.

Thanks and Regards
Sandhya