You need to sign in to do that
Don't have an account?
Rahavan Arumugam Alamelu
ExecuteBatchApex
Am unable to execute the apex batch class. Getting error as "Method does not exist or incorrect signature: void executeBatch(UserDeactivation_batchable) from the type Database"
Apex batchable class:
global class UserDeactivation_Batchable implements Database.Batchable<sObject>
{
Batch_Job_Settings__c batchJobSettings = Batch_Job_Settings__c.getInstance('Batch Job Settings');
Decimal DAYS_IN_THE_PAST = batchJobSettings.UD_Days_After_Last_Modified__c;
String ERROR_EMAIL= batchJobSettings.batchJobSettings;
String PROFILE_NAME = batchJobSettings.UD_Profile_Name__c;
String INACTIVE_TEXT = batchJobSettings.UD_Inactive_Text__c;
Date thresholdDate = System.today().addDays(-DAYS_IN_THE_PAST.intValue());
Integer year = thresholdDate.year();
String month;
if (thresholdDate.month() < 10) {month = '0' + thresholdDate.month();}
else
{
month = thresholdDate.month().format();
}
String day;
if (thresholdDate.day() < 10) {
day = '0' + thresholdDate.day();
} else {
day = thresholdDate.day().format();
}
String thresholdDateString = year +'-'+ month +'-'+ day + 'T23:59:59Z';
query='SELECT Id, isActive, ProfileId, Email, UserName, CommunityNickname, FederationIdentifier, Federation_Id_2__c User WHERE Profile.Name = \'' + PROFILE_NAME + '\ AND isActive = true AND LastModifiedDate <= ' + thresholdDateString;
global Database.QueryLocator start(Database.BatchableContext BC)
{
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: START - query: ' + query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<User> users)
{
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: execute begin. Number of users to process: ' + users.size());
for (User inactiveProfileUser : users)
{
inactiveProfileUser.isActive=false;
inactiveProfileUser.Username=INACTIVE_TEXT + inactiveProfileUser.UserName;
}
System.debug(System.LoggingLevel.DEBUG,'##DEBUG: Database Update List of Users: ' + users.size());
List<Database.SaveResult> userSaveResults=Database.update(users, false);
System.debug(System.LoggingLevel.DEBUG, 'Loop through list of saved results'+ userSaveResults.size());
List<String> userDeactivationUpdateErrors =new List<String>();
for(Integer counter=0;counter<userSaveResults.size();counter++)
{
Database.SaveResult userSaveResult=userSaveResults[counter];
if(!userSaveResult.isSuccess())
System.debug(System.LoggingLevel.DEBUG,'##DEBUG: Errors exist for this userSave: ' + userSaveResult);
for(Database.Error error:userSaveResult.getErrors())
{
String errorMessageAndCode = 'User Deactivation Errors for User: ' + users[counter].Id + ': ';
errorMessageAndCode += error.getStatusCode() +':'+ error.getMessage();
userDeactivationUpdateErrors.add(errorMessageAndCode);
}
}
if(!userDeactivationUpdateErrors.isEmpty())
{
System.debug('##DEBUG: userDeactivationUpdateErrors exist: ' + userDeactivationUpdateErrors);
Messaging.SingleEmailmessage mail=new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {ERROR_EMAIL});
mail.setSubject('User Deactivation Batch Errors:' +System.now()+':Batch Job Id:' + BC.getJobId());
String messageHtmlBody='<h2>User Deactivation Errors</h2> <br/>';
for (String userDeactivationUpdateError:userDeactivationUpdateErrors)
{
messageHtmlBody+= userDeactivationUpdateError + '<br/>'
}
mail.setHtmlBody('<p>The User Deactivation batch Apex job ' + BC.getJobId() +' had the following errors:</p>' + messageHtmlBody);
Messaging.SingleEmailMessage[mail];
}
}
global void finish(Database.BatchableContext BC) {}
}
Kindly advise.
Thanks
Rahavan
Apex batchable class:
global class UserDeactivation_Batchable implements Database.Batchable<sObject>
{
Batch_Job_Settings__c batchJobSettings = Batch_Job_Settings__c.getInstance('Batch Job Settings');
Decimal DAYS_IN_THE_PAST = batchJobSettings.UD_Days_After_Last_Modified__c;
String ERROR_EMAIL= batchJobSettings.batchJobSettings;
String PROFILE_NAME = batchJobSettings.UD_Profile_Name__c;
String INACTIVE_TEXT = batchJobSettings.UD_Inactive_Text__c;
Date thresholdDate = System.today().addDays(-DAYS_IN_THE_PAST.intValue());
Integer year = thresholdDate.year();
String month;
if (thresholdDate.month() < 10) {month = '0' + thresholdDate.month();}
else
{
month = thresholdDate.month().format();
}
String day;
if (thresholdDate.day() < 10) {
day = '0' + thresholdDate.day();
} else {
day = thresholdDate.day().format();
}
String thresholdDateString = year +'-'+ month +'-'+ day + 'T23:59:59Z';
query='SELECT Id, isActive, ProfileId, Email, UserName, CommunityNickname, FederationIdentifier, Federation_Id_2__c User WHERE Profile.Name = \'' + PROFILE_NAME + '\ AND isActive = true AND LastModifiedDate <= ' + thresholdDateString;
global Database.QueryLocator start(Database.BatchableContext BC)
{
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: START - query: ' + query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<User> users)
{
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: execute begin. Number of users to process: ' + users.size());
for (User inactiveProfileUser : users)
{
inactiveProfileUser.isActive=false;
inactiveProfileUser.Username=INACTIVE_TEXT + inactiveProfileUser.UserName;
}
System.debug(System.LoggingLevel.DEBUG,'##DEBUG: Database Update List of Users: ' + users.size());
List<Database.SaveResult> userSaveResults=Database.update(users, false);
System.debug(System.LoggingLevel.DEBUG, 'Loop through list of saved results'+ userSaveResults.size());
List<String> userDeactivationUpdateErrors =new List<String>();
for(Integer counter=0;counter<userSaveResults.size();counter++)
{
Database.SaveResult userSaveResult=userSaveResults[counter];
if(!userSaveResult.isSuccess())
System.debug(System.LoggingLevel.DEBUG,'##DEBUG: Errors exist for this userSave: ' + userSaveResult);
for(Database.Error error:userSaveResult.getErrors())
{
String errorMessageAndCode = 'User Deactivation Errors for User: ' + users[counter].Id + ': ';
errorMessageAndCode += error.getStatusCode() +':'+ error.getMessage();
userDeactivationUpdateErrors.add(errorMessageAndCode);
}
}
if(!userDeactivationUpdateErrors.isEmpty())
{
System.debug('##DEBUG: userDeactivationUpdateErrors exist: ' + userDeactivationUpdateErrors);
Messaging.SingleEmailmessage mail=new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {ERROR_EMAIL});
mail.setSubject('User Deactivation Batch Errors:' +System.now()+':Batch Job Id:' + BC.getJobId());
String messageHtmlBody='<h2>User Deactivation Errors</h2> <br/>';
for (String userDeactivationUpdateError:userDeactivationUpdateErrors)
{
messageHtmlBody+= userDeactivationUpdateError + '<br/>'
}
mail.setHtmlBody('<p>The User Deactivation batch Apex job ' + BC.getJobId() +' had the following errors:</p>' + messageHtmlBody);
Messaging.SingleEmailMessage[mail];
}
}
global void finish(Database.BatchableContext BC) {}
}
Kindly advise.
Thanks
Rahavan
Database.executeBatch(new UserDeactivation_Batchable());
But i do not think the class is proper. You can try running below code.
global class UserDeactivation_Batchable implements Database.Batchable < sObject > {
global String query = '';
List < String > userDeactivationUpdateErrors = new List < String > ();
// constructor
pubic UserDeactivation_Batchable() {
Batch_Job_Settings__c batchJobSettings = Batch_Job_Settings__c.getInstance('Batch Job Settings');
Decimal DAYS_IN_THE_PAST = batchJobSettings.UD_Days_After_Last_Modified__c;
// below line is wrong batchJobSettings.batchJobSettings
String ERROR_EMAIL = batchJobSettings.batchJobSettings;
String PROFILE_NAME = batchJobSettings.UD_Profile_Name__c;
String INACTIVE_TEXT = batchJobSettings.UD_Inactive_Text__c;
Date thresholdDate = System.today().addDays(-DAYS_IN_THE_PAST.intValue());
Integer year = thresholdDate.year();
String month;
if (thresholdDate.month() < 10) {
month = '0' + thresholdDate.month();
} else {
month = thresholdDate.month().format();
}
String day;
if (thresholdDate.day() < 10) {
day = '0' + thresholdDate.day();
} else {
day = thresholdDate.day().format();
}
String thresholdDateString = year + '-' + month + '-' + day + 'T23:59:59Z';
query = 'SELECT Id, isActive, ProfileId, Email, UserName, CommunityNickname, FederationIdentifier, Federation_Id_2__c User WHERE Profile.Name = \'' + PROFILE_NAME + '\ AND isActive = true AND LastModifiedDate <= ' + thresholdDateString;
}
global Database.QueryLocator start(Database.BatchableContext BC) {
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: START - query: ' + query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List < User > users) {
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: execute begin. Number of users to process: ' + users.size());
for (User inactiveProfileUser: users) {
inactiveProfileUser.isActive = false;
inactiveProfileUser.Username = INACTIVE_TEXT + inactiveProfileUser.UserName;
}
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: Database Update List of Users: ' + users.size());
List < Database.SaveResult > userSaveResults = Database.update(users, false);
System.debug(System.LoggingLevel.DEBUG, 'Loop through list of saved results' + userSaveResults.size());
// moved as global so that can access in finish
//List < String > userDeactivationUpdateErrors = new List < String > ();
for (Integer counter = 0; counter < userSaveResults.size(); counter++) {
Database.SaveResult userSaveResult = userSaveResults[counter];
if (!userSaveResult.isSuccess())
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: Errors exist for this userSave: ' + userSaveResult);
for (Database.Error error: userSaveResult.getErrors()) {
String errorMessageAndCode = 'User Deactivation Errors for User: ' + users[counter].Id + ': ';
errorMessageAndCode += error.getStatusCode() + ':' + error.getMessage();
userDeactivationUpdateErrors.add(errorMessageAndCode);
}
}
}
global void finish(Database.BatchableContext BC) {
// building email body
String messageHtmlBody = '<h2>User Deactivation Errors</h2> <br/>';
if (!userDeactivationUpdateErrors.isEmpty()) {
System.debug('##DEBUG: userDeactivationUpdateErrors exist: ' + userDeactivationUpdateErrors);
for (String userDeactivationUpdateError: userDeactivationUpdateErrors) {
messageHtmlBody += userDeactivationUpdateError + '<br/>'
}
// Sending email
Messaging.SingleEmailmessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {
ERROR_EMAIL
});
mail.setSubject('User Deactivation Batch Errors:' + System.now() + ':Batch Job Id:' + BC.getJobId());
mail.setHtmlBody('<p>The User Deactivation batch Apex job ' + BC.getJobId() + ' had the following errors:</p>' + messageHtmlBody);
Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}
}
}
DataBase.executeBatch(new UserDeactivation_Batchable() );
Please check below post for more information
1) http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html
Let us know if this will help you
global class UserDeactivation_Batchable implements Database.Batchable < sObject > {
global String query = '';
List < String > userDeactivationUpdateErrors = new List < String > ();
Batch_Job_Settings__c batchJobSettings = Batch_Job_Settings__c.getInstance('Batch Job Settings');
Decimal DAYS_IN_THE_PAST = batchJobSettings.UD_Days_After_Last_Modified__c;
String ERROR_EMAIL = batchJobSettings.UD_Error_Email__c;
String PROFILE_NAME = batchJobSettings.UD_Profile_Name__c;
String INACTIVE_TEXT = batchJobSettings.UD_Inactive_Text__c;
String month;
// constructor
global UserDeactivation_Batchable() {
// below line is wrong batchJobSettings.batchJobSettings
Date thresholdDate = System.today().addDays(- DAYS_IN_THE_PAST.intValue());
Integer year = thresholdDate.year();
if (thresholdDate.month() < 10) {
month = '0' + thresholdDate.month();
} else {
month = thresholdDate.month().format();
}
String day;
if (thresholdDate.day() < 10) {
day = '0' + thresholdDate.day();
} else {
day = thresholdDate.day().format();
}
String thresholdDateString = year + '-' + month + '-' + day + 'T23:59:59Z';
query = 'SELECT Id, isActive, ProfileId, Email, UserName, CommunityNickname, FederationIdentifier, Federation_Id_2__c FROM User WHERE Profile.Name = \' '+ PROFILE_NAME + '\' AND isActive = true AND LastModifiedDate <= ' + thresholdDateString;
}
global Database.QueryLocator start(Database.BatchableContext BC) {
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: START - query: ' + query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List < User > users) {
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: execute begin. Number of users to process: ' + users.size());
for (User inactiveProfileUser: users) {
inactiveProfileUser.isActive = false;
inactiveProfileUser.Username = INACTIVE_TEXT + inactiveProfileUser.UserName;
}
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: Database Update List of Users: ' + users.size());
List < Database.SaveResult > userSaveResults = Database.update(users, false);
System.debug(System.LoggingLevel.DEBUG, 'Loop through list of saved results' + userSaveResults.size());
// moved as global so that can access in finish
//List < String > userDeactivationUpdateErrors = new List < String > ();
for (Integer counter = 0; counter < userSaveResults.size(); counter++) {
Database.SaveResult userSaveResult = userSaveResults[counter];
if (!userSaveResult.isSuccess())
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: Errors exist for this userSave: ' + userSaveResult);
for (Database.Error error: userSaveResult.getErrors()) {
String errorMessageAndCode = 'User Deactivation Errors for User: ' + users[counter].Id + ': ';
errorMessageAndCode += error.getStatusCode() + ':' + error.getMessage();
userDeactivationUpdateErrors.add(errorMessageAndCode);
}
}
}
global void finish(Database.BatchableContext BC) {
// building email body
String messageHtmlBody = '<h2>User Deactivation Errors</h2> <br/>';
if (!userDeactivationUpdateErrors.isEmpty()) {
System.debug('##DEBUG: userDeactivationUpdateErrors exist: ' + userDeactivationUpdateErrors);
for (String userDeactivationUpdateError: userDeactivationUpdateErrors) {
messageHtmlBody += userDeactivationUpdateError + '<br/>';
}
// Sending email
Messaging.SingleEmailmessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[]{ERROR_EMAIL});
mail.setSubject('User Deactivation Batch Errors:' + System.now() + ':Batch Job Id:' + BC.getJobId());
mail.setHtmlBody('<p>The User Deactivation batch Apex job ' + BC.getJobId() + ' had the following errors:</p>' + messageHtmlBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
I have values for the fields of custom settings as below:
Batchsize = 2000
UD - Days After Last Modified = 7
Am getting below error when trying to execute the batch apex in ananymous window
System.NullPointerException: Attempt to de-reference a null object
Please advise.
Thanks
Rahavan
try{
Database.executeBatch(new UserDeactivation_Batchable());
}catch(Exception e){
// please check in log on which line Nullpointer thrown
System.debug('Error:'+e);
}
2. does this field have any value populated?
try{
Batch_Job_Settings__c batchJobSettings = Batch_Job_Settings__c.getInstance('Batch Job Settings');
// below should not print null
System.debug('Queried batchJobSettings:'+batchJobSettings);
Database.executeBatch(new UserDeactivation_Batchable());
}catch(Exception e){
// please check in log on which line Nullpointer thrown
System.debug('Error:'+e);
}
Please replace below above line with below statement.
// Please add other field in select
Batch_Job_Settings__c batchJobSettings = [SELECT id,UD_Days_After_Last_Modified__c, "Other Required Field" FROM Batch_Job_Settings__c limit 1];
System.debug(System.LoggingLevel.DEBUG, '##DEBUG: START - query: ' + query);
User Deactivation Errors
'; if (!userDeactivationUpdateErrors.isEmpty()) { System.debug('##DEBUG: userDeactivationUpdateErrors exist: ' + userDeactivationUpdateErrors); for (String userDeactivationUpdateError: userDeactivationUpdateErrors) { messageHtmlBody += userDeactivationUpdateError + '
'; } // Sending email Messaging.SingleEmailmessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new String[]{ERROR_EMAIL}); mail.setSubject('User Deactivation Batch Errors:' + System.now() + ':Batch Job Id:' + BC.getJobId()); mail.setHtmlBody('
The User Deactivation batch Apex job ' + BC.getJobId() + ' had the following errors:
' + messageHtmlBody); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } } } Thanks Rahavan