You need to sign in to do that
Don't have an account?
Jason in China
Start-Excute-Finish didn't run in Batch Apex code
Hi,
I am facing a strange issue now. Dose anyone have idea about this?
By requrirement, I created a batch apex to send email to some user, according some special condition.
But when i tested it in console, it seems the Start-Excute-Finish block donot run.
Below detail apex for reference:
global class D_Not_AA implements Database.Batchable<sObject>,Database.Stateful { List <String> L_Ps = new String[] {'D_P','SSO_D_P'}; List <String> L_CP_Status = new String[] {'Approved','Submitted'}; Set<Id> S_CP_Owner = new set<Id>(); List<Id> L_toMail_Ps = new List<Id>(); // List<Id> L_toMail_P_Mgrs = new List<Id>(); Map <Id,List<Id>> M_Manager_P = new Map <Id,List<Id>>(); global D_Not_AA () { // Get the detail Cycle Plan Owners on current period system.debug(LoggingLevel.INFO,' Get Cycle Plan info.'); for (Cycle_Plan_vod__c J_CP_Owner :[SELECT OwnerId FROM Cycle_Plan_vod__c where Status_D__c in :L_CP_Status and Start_Date_vod__c < today and End_Date_vod__c > today]) { S_CP_Owner.add(J_CP_Owner.OwnerId); system.debug('[J] Cycle Plan OwnerId: '+J_CP_Owner.OwnerId); } system.debug(LoggingLevel.INFO,' Get Cycle Plan info_End.'); // L_toMail_P_Mgrs.addAll(M_Manager_P.keySet()); } global Database.QueryLocator start(Database.BatchableContext BC) { system.debug(LoggingLevel.INFO,' Batch Start'); For (User U: [SELECT Id,ManagerId from User where isActive = true and ProfileId in ( SELECT Id FROM Profile where Name in :L_Ps)]) { if (S_CP_Owner.contains (U.Id)) { // the P have already create Cycle Plan } else { // the P did not create Cycle Plan yet L_toMail_Ps.add(U.Id); if (M_Manager_P.keySet().Contains(U.ManagerId)) { M_Manager_P.get(U.ManagerId).add(U.Id); } else { M_Manager_P.put(U.ManagerId,new List <Id> {U.Id}); } system.debug('[J] P Manager Users : '+U.ManagerId); system.debug('[J] P Users : '+U.Id); } } // Get all users to be send email String query = 'Select id, name, email FROM User where id in' + M_Manager_P.keySet(); return Database.getQueryLocator(query); } global void Execute(Database.BatchableContext BC, List<sObject> Scope) { system.debug(LoggingLevel.INFO,' Batch Execute'); for (Sobject s : Scope) { User ur = (User)s; if (M_Manager_P.containsKey(ur.Id)) { List<User> L_P_User = [SELECT ID,NAME,EMAIL FROM USER WHERE ID IN :M_Manager_P.get(ur.Id)]; String body = ''; body = BodyFormat(L_P_User); // Mail Setting Messaging.Singleemailmessage mail = new messaging.Singleemailmessage(); String[] toAddresses = new String[] {ur.Email}; mail.setToAddresses (toAddresses); mail.setSubject('System Message:'); mail.setHtmlBody('Hi ' + ur.Name + ', Details of Cases logged today is as follows : ' + User + ' Thanks'); } } } public String BodyFormat(List<User> P) { system.debug(LoggingLevel.INFO,' Batch Body'); String str = '' ; for(User U_P : P) { str += '<tr><td>'+ U_P.Name +'</td>'+'<td>'+ U_P.Username +'</td>'+'</tr>' ; } str = str.replace('null' , '') ; String finalStr = '' ; finalStr = '<table border="1"> <td> P </td> <td> User Name </td> '+ str +'</table>' ; return finalStr ; } global void Finish (Database.BatchableContext BC) { system.debug(LoggingLevel.INFO,' Batch Finish'); } }
I run it with
D_Not_AA controller = new D_Not_AA () ; Integer batchSize = 1; database.executebatch(controller , batchSize);
Dose anybody have idea about this?
I realy apprical for any response on this.
Thanks in advance.
I pasted the debug logs.
Is there any idea on it ?
thx