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
Mayank.msMayank.ms 

Unable to create task in batch apex class

I have created a batch class that fetch records from opportunity objects and based on it creating a task. It is executing successfully but task not creating. It is working for few records but not woking for more records. 
I have getting th​e task id in the debug mode but when we are put these task id in the URL, its showing as deteted. 

Class:
global class CSportfoliosTask implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        datetime next30days = date.today().addDays(30);
        string next30daysformat = next30days.format('yyyy-MM-dd');
        String query = 'SELECT Id, Credit_Card_Expiration__c, Next_Billed_Date__c,Opportunity.Account.OwnerId  FROM opportunity WHERE  StageName = \'Closed Won\' and Susbcription_Status__c =\'Active\' and Next_Billed_Date__c='+next30daysformat;
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Opportunity> scope)
    {
            for(Opportunity o : scope)
            {
                if(o.Credit_Card_Expiration__c!=NULL){
                    String ccs = o.Credit_Card_Expiration__c;
                    String[] ccsplitted = ccs.split('-'); 
                    String ccmonth =  ccsplitted[0];
                    String ccyear =  ccsplitted[1];
                    String nexts = string.valueOfGmt(o.Next_Billed_Date__c);
                    String[] nextSplitted = nexts.split('-');
                    String nextYear = nextSplitted[0];
                    String nextMonth = nextSplitted[1];
                    if(ccyear<=nextYear){
                        if(Integer.ValueOf(ccmonth)<=Integer.ValueOf(nextMonth)){
                             CreaeTask('Alert - Subscription payment at risk - Expired Card on '+o.Credit_Card_Expiration__c, o.Id , o.Account.OwnerId);
                        }
                    }
                }   
            }
            
        List<Opportunity> opp2 = new List<Opportunity>();
        datetime next30days = date.today().addDays(30);
        string next30daysformat = next30days.format('MM/dd/YYYY');
        date next30daysdate = date.parse(next30daysformat);
        
        opp2= [SELECT Id, Next_Billed_Date__c, Opportunity.Account.OwnerId FROM opportunity WHERE  StageName = 'Closed Won' and Susbcription_Status__c ='Active' and Next_Billed_Date__c=:next30daysdate];
            for(Opportunity o2 : opp2)
            {  
               CreaeTask('Alert - Secure Subscription Renewal on '+o2.Next_Billed_Date__c, o2.Id, o2.Account.OwnerId);
            }
    }
    
    global void CreaeTask(String Subject,String Id, String OwnerId){
        Task t = new Task();
        t.Subject = Subject;
        t.Type = 'Outbound Call';
        t.Status = 'Open';
        t.Priority = 'Normal';
        t.ActivityDate= date.today();
        t.WhatId =Id;
        t.OwnerId = OwnerId;
        insert t;     
       } 
    
    global void finish(Database.BatchableContext BC)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
        mail.setToAddresses(new String[] {'ahmeds@example.com'});
        mail.setReplyTo('ahmeds@example.com');
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('Batch Process Early Warning System for CS portfolios create task Completed');
        mail.setPlainTextBody('Early Warning System for CS portfolios create task Batch Process has Completed');
 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
Scheduler Class
 
global class ScheduleCSportfoliosTask implements Schedulable
{
       global void execute(SchedulableContext sc) 
        {
        CSportfoliosTask d1 = new CSportfoliosTask();
        ID batchprocessid = Database.executeBatch(d1,200);           
        }
}
User-added image

I have aslo execute batch with 10 as well. i.e  ID batchprocessid = Database.executeBatch(d1,10); but its not the solution.

Also getting the too many sql query

User-added image

Can you guys please help me how to resolve this issue.

Thansk in advance. 
 
Amit Chaudhary 8Amit Chaudhary 8
Issue was coming in CreaeTask mathod because you was calling the same method inside for loop and every time you was doing the DML (insert)

Please update your code like below
global class CSportfoliosTask implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        datetime next30days = date.today().addDays(30);
        string next30daysformat = next30days.format('yyyy-MM-dd');
        String query = 'SELECT Id, Credit_Card_Expiration__c, Next_Billed_Date__c,Opportunity.Account.OwnerId  FROM opportunity WHERE  StageName = \'Closed Won\' and Susbcription_Status__c =\'Active\' and Next_Billed_Date__c='+next30daysformat;
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Opportunity> scope)
    {
		List<Task> lstTasktoInsert = new List<Task>();
	
            for(Opportunity o : scope)
            {
                if(o.Credit_Card_Expiration__c!=NULL)
				{
                    String ccs = o.Credit_Card_Expiration__c;
                    String[] ccsplitted = ccs.split('-'); 
                    String ccmonth =  ccsplitted[0];
                    String ccyear =  ccsplitted[1];
                    String nexts = string.valueOfGmt(o.Next_Billed_Date__c);
                    String[] nextSplitted = nexts.split('-');
                    String nextYear = nextSplitted[0];
                    String nextMonth = nextSplitted[1];
                    if(ccyear<=nextYear){
                        if(Integer.ValueOf(ccmonth)<=Integer.ValueOf(nextMonth))
						{
                             Task taskObj = CreaeTask('Alert - Subscription payment at risk - Expired Card on '+o.Credit_Card_Expiration__c, o.Id , o.Account.OwnerId);
							 
							 lstTasktoInsert.add(taskObj);
                        }
                    }
                }   
            }
            
        List<Opportunity> opp2 = new List<Opportunity>();
        datetime next30days = date.today().addDays(30);
        string next30daysformat = next30days.format('MM/dd/YYYY');
        date next30daysdate = date.parse(next30daysformat);
        
        opp2= [SELECT Id, Next_Billed_Date__c, Opportunity.Account.OwnerId FROM opportunity WHERE  StageName = 'Closed Won' and Susbcription_Status__c ='Active' and Next_Billed_Date__c=:next30daysdate];
            
		for(Opportunity o2 : opp2)
		{  
		    Task taskObj = CreaeTask('Alert - Secure Subscription Renewal on '+o2.Next_Billed_Date__c, o2.Id, o2.Account.OwnerId);
			lstTasktoInsert.add(taskObj);
		}
		
		if(lstTasktoInsert.size() > 0 )
		{
			insert lstTasktoInsert;
		}
    }
    
    global Task CreaeTask(String Subject,String Id, String OwnerId){
        Task t = new Task();
        t.Subject = Subject;
        t.Type = 'Outbound Call';
        t.Status = 'Open';
        t.Priority = 'Normal';
        t.ActivityDate= date.today();
        t.WhatId =Id;
        t.OwnerId = OwnerId;
        return t;     
       } 
    
    global void finish(Database.BatchableContext BC)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
        mail.setToAddresses(new String[] {'ahmeds@example.com'});
        mail.setReplyTo('ahmeds@example.com');
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('Batch Process Early Warning System for CS portfolios create task Completed');
        mail.setPlainTextBody('Early Warning System for CS portfolios create task Batch Process has Completed');
 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Let us know if above code will help you

 
Mayank.msMayank.ms
Hi Amit,

Thanks for the quick reply on it. I have tried with your given code, but still not creating the task. See the debug log as  below


User-added image

User-added image
Amit Chaudhary 8Amit Chaudhary 8
Update your class like below
global class CSportfoliosTask implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        datetime next30days = date.today().addDays(30);
        string next30daysformat = next30days.format('yyyy-MM-dd');
        String query = 'SELECT Id, Credit_Card_Expiration__c, Next_Billed_Date__c,Opportunity.Account.OwnerId  FROM opportunity WHERE  StageName = \'Closed Won\' and Susbcription_Status__c =\'Active\' and Next_Billed_Date__c='+next30daysformat;
        return Database.getQueryLocator(query);
    }
	
    global void execute(Database.BatchableContext BC, List<Opportunity> scope)
    {
		List<Task> lstTasktoInsert = new List<Task>();
	
		for(Opportunity o : scope)
		{
			if(o.Credit_Card_Expiration__c!=NULL)
			{
				String ccs = o.Credit_Card_Expiration__c;
				String[] ccsplitted = ccs.split('-'); 
				String ccmonth =  ccsplitted[0];
				String ccyear =  ccsplitted[1];
				String nexts = string.valueOfGmt(o.Next_Billed_Date__c);
				String[] nextSplitted = nexts.split('-');
				String nextYear = nextSplitted[0];
				String nextMonth = nextSplitted[1];
				
				if(ccyear<=nextYear)
				{
					if(Integer.ValueOf(ccmonth)<=Integer.ValueOf(nextMonth))
					{
						 Task taskObj = CreaeTask('Alert - Subscription payment at risk - Expired Card on '+o.Credit_Card_Expiration__c, o.Id , o.Account.OwnerId);
						 
						 lstTasktoInsert.add(taskObj);
					}
				}
				
			}
		}

		/*	
		
			List<Opportunity> opp2 = new List<Opportunity>();
			datetime next30days = date.today().addDays(30);
			string next30daysformat = next30days.format('MM/dd/YYYY');
			date next30daysdate = date.parse(next30daysformat);
			
			opp2= [SELECT Id, Next_Billed_Date__c, Opportunity.Account.OwnerId FROM opportunity WHERE  StageName = 'Closed Won' and Susbcription_Status__c ='Active' and Next_Billed_Date__c=:next30daysdate];
				
			for(Opportunity o2 : opp2)
			{  
				Task taskObj = CreaeTask('Alert - Secure Subscription Renewal on '+o2.Next_Billed_Date__c, o2.Id, o2.Account.OwnerId);
				lstTasktoInsert.add(taskObj);
			}
		
		*/
		
		if(lstTasktoInsert.size() > 0 )
		{
			insert lstTasktoInsert;
		}
		
    }
    
    global Task CreaeTask(String Subject,String Id, String OwnerId){
        Task t = new Task();
        t.Subject = Subject;
        t.Type = 'Outbound Call';
        t.Status = 'Open';
        t.Priority = 'Normal';
        t.ActivityDate= date.today();
        t.WhatId =Id;
        t.OwnerId = OwnerId;
        return t;     
       } 
    
    global void finish(Database.BatchableContext BC)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
        mail.setToAddresses(new String[] {'ahmeds@example.com'});
        mail.setReplyTo('ahmeds@example.com');
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('Batch Process Early Warning System for CS portfolios create task Completed');
        mail.setPlainTextBody('Early Warning System for CS portfolios create task Batch Process has Completed');
 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

I found below duplicate your in your code. So please comment below code and try
/*	
			List<Opportunity> opp2 = new List<Opportunity>();
			datetime next30days = date.today().addDays(30);
			string next30daysformat = next30days.format('MM/dd/YYYY');
			date next30daysdate = date.parse(next30daysformat);
			
			opp2= [SELECT Id, Next_Billed_Date__c, Opportunity.Account.OwnerId FROM opportunity WHERE  StageName = 'Closed Won' and Susbcription_Status__c ='Active' and Next_Billed_Date__c=:next30daysdate];
				
			for(Opportunity o2 : opp2)
			{  
				Task taskObj = CreaeTask('Alert - Secure Subscription Renewal on '+o2.Next_Billed_Date__c, o2.Id, o2.Account.OwnerId);
				lstTasktoInsert.add(taskObj);
			}
		*/

Please let us know if this will help you

Thanks
Amit Chaudhary
Mayank.msMayank.ms
Hi Amit,

I have update the code as below and when we set batch execution with 50 then it execute. i.e 
  ID batchprocessid = Database.executeBatch(d1,50);   
global class CSportfoliosTask implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        datetime next30days = date.today().addDays(30);
        string next30daysformat = next30days.format('yyyy-MM-dd');
        String query = 'SELECT Id, Credit_Card_Expiration__c, Next_Billed_Date__c,Opportunity.Account.OwnerId  FROM opportunity WHERE  StageName = \'Closed Won\' and Susbcription_Status__c =\'Active\' and Next_Billed_Date__c='+next30daysformat;
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Opportunity> scope)
    {
    List<Task> lstTasktoInsert = new List<Task>();
    LIST<Task> taskListToUpdate = new List<Task>();
      Set<Task> taskSet = new Set<Task>();
            for(Opportunity o : scope)
            {
                if(o.Credit_Card_Expiration__c!=NULL)
        {
                    String ccs = o.Credit_Card_Expiration__c;
                    String[] ccsplitted = ccs.split('-'); 
                    String ccmonth =  ccsplitted[0];
                    String ccyear =  ccsplitted[1];
                    String nexts = string.valueOfGmt(o.Next_Billed_Date__c);
                    String[] nextSplitted = nexts.split('-');
                    String nextYear = nextSplitted[0];
                    String nextMonth = nextSplitted[1];
                    if(ccyear<=nextYear){
                        if(Integer.ValueOf(ccmonth)<=Integer.ValueOf(nextMonth))
            {
                             Task taskObj = CreaeTask('Alert - Subscription payment at risk - Expired Card on '+o.Credit_Card_Expiration__c, o.Id , o.Account.OwnerId);
               
               lstTasktoInsert.add(taskObj);
                        }
                    }
                } 
                
                 Task taskObj = CreaeTask('Alert - Secure Subscription Renewal on '+o.Next_Billed_Date__c, o.Id, o.Account.OwnerId);
                lstTasktoInsert.add(taskObj);
            }
            
      
       try {
                 if(!lstTasktoInsert.IsEmpty()){
                   taskSet.addAll(lstTasktoInsert);
            taskListToUpdate.addAll(taskSet);
                  system.debug('Kamran3');
                  insert taskListToUpdate;
                   }      
        } catch (system.dmlexception e) {
            System.debug('Tasks not inserted: ' + e);
        }
    }
    
    global Task CreaeTask(String Subject,String Id, String OwnerId){
        Task t = new Task();
        t.Subject = Subject;
        t.Type = 'Outbound Call';
        t.Status = 'Open';
        t.Priority = 'Normal';
        t.ActivityDate= date.today();
        t.WhatId =Id;
        t.OwnerId = OwnerId;
        return t;     
       } 
    
    global void finish(Database.BatchableContext BC)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
        mail.setToAddresses(new String[] {'ahmeds@webgility.com'});
        mail.setReplyTo('ahmeds@webgility.com');
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('Batch Process Early Warning System for CS portfolios create task Completed');
        mail.setPlainTextBody('Early Warning System for CS portfolios create task Batch Process has Completed');
 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
But I think it is not the solution. I have similarly another class which has more than 1700 records, when I am using as executeBatch as 50 then it will not execute.
Can you please look into it.

Thanks for your help.