Skip to main content
Rahavan Arumugam Alamelu 님이 #Apex에 질문했습니다
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

 
답변 18개
  1. 2017년 8월 24일 오후 1:35

    Please try below code to execute

    UserDeactivation_Batchable obj = new UserDeactivation_Batchable();

    DataBase.executeBatch(obj);

    NOTE:- you need to pass object of Batch job in executeBatch

    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