You need to sign in to do that
Don't have an account?
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 the task id in the debug mode but when we are put these task id in the URL, its showing as deteted.
Class:

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
.
Can you guys please help me how to resolve this issue.
Thansk in advance.
I have getting the 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); } }
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
.
Can you guys please help me how to resolve this issue.
Thansk in advance.
Please update your code like below
Let us know if above code will help you
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
I found below duplicate your in your code. So please comment below code and try
Please let us know if this will help you
Thanks
Amit Chaudhary
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); 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.